Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 12

Flow Control Practice — Combined Interview Programs

Final flow control revision: dedupe marks, list chains, set algebra, nested dict CRUD, hashability checks, and 10 interview-style problems.

Beginner30 min

Revision workflow

Predict Run Check Classify

Interview habit: Predict the output before executing the code.

Program 1 — Unique sorted marks

python
marks = [85, 90, 85, 78, 90, 92]
unique = sorted(set(marks))
print(unique)  # [78, 85, 90, 92]

Time complexity: O(n log n)

Program 2 — List method chain

python
lst = [10, 20, 30]
lst.append(40)
lst.insert(1, 15)
lst.extend([50, 60])
lst.sort()
print(lst)
print(lst.count(10), lst.index(15))

Output: [10, 15, 20, 30, 40, 50, 60] then 1 1

Program 3 — Set algebra

python
cricket = {"A", "B", "C", "D"}
tennis = {"C", "D", "E", "F"}

print(cricket | tennis)   # union
print(cricket & tennis)   # intersection
print(cricket - tennis)   # difference
print(cricket ^ tennis)   # symmetric difference

Program 4 — Nested dictionary CRUD

python
student = {
    "sno": 101,
    "name": "RS",
    "IntMarks": {"Math": 18, "Sci": 17},
    "ExtMarks": {"Math": 72, "Sci": 70},
}

student["IntMarks"]["Eng"] = 19
student["ExtMarks"]["Eng"] = 68

print(student["IntMarks"].get("Math"))
print(list(student.keys()))

# Create
student["IntMarks"]["DBMS"] = 20
# Update
student["ExtMarks"]["Math"] = 75
# Delete
student["IntMarks"].pop("Sci")

Program 5 — Hashability checker

python
samples = [[1, 2], (1, 2), {1, 2}, frozenset({1, 2}), {"a": 1}]

for obj in samples:
    try:
        {obj}
        print(type(obj).__name__, "Hashable")
    except TypeError:
        print(type(obj).__name__, "Not Hashable")
Object Result
list Not hashable
tuple Hashable
set Not hashable
frozenset Hashable
dict Not hashable

10 interview practice problems

# Question Difficulty
1 Remove duplicates preserving order ⭐
2 Reverse a tuple ⭐
3 Find common elements of two lists ⭐
4 Count frequency using dict ⭐⭐
5 Merge two dictionaries ⭐⭐
6 Find highest scoring student ⭐⭐
7 Group students by department ⭐⭐⭐
8 Nested employee CRUD ⭐⭐⭐
9 Validate hashability of objects ⭐⭐⭐
10 Convert list → set → tuple pipeline ⭐⭐⭐

Knowledge check

Q1: Best way to remove duplicates while preserving order?
Answer: list(dict.fromkeys(lst))

Q2: Which collection is immutable and hashable?
Answer: frozenset

Q3: Safest way to read a missing dictionary key?
Answer: d.get("key", default)

Q4: Which object can be a set element?
Answer: (1, 2)

Final takeaway

Master these five themes for collection + flow control interviews:

  1. Deduplication (set, dict.fromkeys)
  2. List manipulation
  3. Set algebra
  4. Nested dictionary CRUD
  5. Hashability rules

Module complete

Overview: Flow Control — File 01

Next module: Functions (coming soon)

Loading playground…