← 30 Days of Python
Day 29 / 30Coding Practice

Python Coding Challenges for Analysts

Some interviews give you a small coding problem. These are not LeetCode algorithmic puzzles — they are practical data manipulation tasks. Practice these patterns until they feel automatic.

1
Medium

Write a function to find the second highest salary from a list.

Python Answer
def second_highest(salaries):
    unique = sorted(set(salaries), reverse=True)
    if len(unique) < 2:
        return None
    return unique[1]

print(second_highest([50000, 80000, 80000, 60000, 75000]))  # 75000
💡

Using set() removes duplicates — important because the highest salary may appear multiple times. sorted(reverse=True) puts highest first. The second item (index 1) is the answer.

2
Easy

Write a function to count word frequency in a string.

Python Answer
from collections import Counter

def word_frequency(text):
    words = text.lower().split()
    return Counter(words)

text = "data analyst data science data engineer"
print(word_frequency(text))
# Counter({"data": 3, "analyst": 1, "science": 1, "engineer": 1})
💡

Counter is built for this — it counts any iterable. .lower() ensures "Data" and "data" are counted together. This pattern is used in text analysis, sentiment detection, and customer feedback mining.

3
Medium

Write a function to flatten a nested list.

Python Answer
def flatten(nested):
    result = []
    for item in nested:
        if isinstance(item, list):
            result.extend(flatten(item))
        else:
            result.append(item)
    return result

print(flatten([[1, [2, 3]], [4, [5, [6]]]]))  # [1, 2, 3, 4, 5, 6]

# One-liner for one level deep:
flat = [x for sublist in [[1,2],[3,4]] for x in sublist]
💡

Recursive flattening handles any nesting depth. isinstance(item, list) checks if the element is itself a list. This pattern appears when JSON API responses return nested arrays that need to be flattened before creating a DataFrame.

4
Medium

Write a function to remove duplicates from a list while preserving order.

Python Answer
def remove_dupes(lst):
    seen = set()
    result = []
    for item in lst:
        if item not in seen:
            result.append(item)
            seen.add(item)
    return result

remove_dupes([3, 1, 4, 1, 5, 3, 9])  # [3, 1, 4, 5, 9]

# Python 3.7+ dict preserves order:
list(dict.fromkeys([3, 1, 4, 1, 5, 3, 9]))  # [3, 1, 4, 5, 9]
💡

Using a set for seen enables O(1) lookup — much faster than if item not in result (O(n)). dict.fromkeys() is the modern one-liner. Note: list(set(lst)) does NOT preserve order — a common mistake.

5
Hard

Write a function to group a list of dictionaries by a key.

Python Answer
from collections import defaultdict

data = [
    {"name": "Rahul", "dept": "Sales"},
    {"name": "Priya", "dept": "HR"},
    {"name": "Aman", "dept": "Sales"},
    {"name": "Neha", "dept": "HR"},
]

def group_by(lst, key):
    groups = defaultdict(list)
    for item in lst:
        groups[item[key]].append(item)
    return dict(groups)

result = group_by(data, "dept")
# {"Sales": [{Rahul}, {Aman}], "HR": [{Priya}, {Neha}]}
💡

defaultdict(list) creates an empty list for any new key automatically — no need to check if the key exists. This is a common interview pattern and the Python equivalent of SQL GROUP BY on a list of records.

EVIKA ACADEMY · PYTHON FOR DATA ANALYTICS

Want to master Python with live practice?

Join our Python for Data Analysis course — live classes in Noida and online across India.

Book Free Demo Class →
← PREVIOUSDay 28: Virtual Environments & Python Best PracticesNEXT →Day 30: Python Mock Interview — Real Questions & Model Answers
Best Data Analytics Course in Noida Delhi NCR | EVIKA ACADEMY