Skip to content
Tech Interview Prep
Technical interview guide

Core Data Structures

Lists, tuples, dicts, and sets: their underlying implementations and when each is the right choice.

Read
48 min
Practice MCQs
25
Interview Q&A
25
Edition
v2
Editorial status
Reviewed

Scope: Python 3.14; notes distinguish language guarantees from CPython implementation behavior.

Curated: · Written: · Reviewed:

25 questions

MCQ-1After `backup = records`, calling `records.append(3)` also changes `backup`. What is the root cause?
MCQ-4An API requires a one-element tuple containing `user_id`, and the client validates `isinstance(payload, tuple)` before sending. Which expression supplies that shape?
MCQ-8Which composite value is eligible to be a dictionary key?
MCQ-2A worker builds `job = (7, [])` and passes the tuple to a helper. The helper runs `job[1].append('start')`. The worker then reads `job[1]`. Which statement about this code is accurate?
MCQ-17What does this code print? ```python row = [1, 2] grid = [row, row] snapshot = grid[:] snapshot[0][1] = 9 print(grid) ```
MCQ-18Why does `rows = [[0] * width] * height` produce coupled rows?
MCQ-19When defining a function like `collect(x, items=[])`, subsequent calls to the function may retain state from previous calls. Which statement correctly explains this behavior?
MCQ-20Two calls to this function print `[1]` and then `[1, 2]`: ```python def add(x, items=[]): items.append(x) return items print(add(1)) print(add(2)) ``` Which explanation accounts for the second call seeing the first call's element?
MCQ-6A parser runs `host, *ports, zone = line.split(':')`, and downstream code calls `ports.append(443)` and tests `if not ports:`. Lines may carry zero, one, or several port fields. What does `ports` bind to?
MCQ-5A pipeline function sorts a list of order rows in place: ```python def rank(rows): rows.sort(key=lambda r: r["pay"], reverse=True) return rows.sort(key=lambda r: r["dept"]) ``` A caller runs `result = rank(rows)` and then `result[0]`, which raises `TypeError: 'NoneType' object is not subscriptable`. What order is the caller's `rows` list in after the call?
MCQ-21A reporting job sorts records whose key field holds an `int` for one source system and a `str` for another, and `sorted(records, key=lambda r: r['k'])` raises `TypeError: '<' not supported between instances of 'str' and 'int'`. The report must sort deterministically, and within each type the values must keep their natural order (`9` before `10`, `'a'` before `'b'`). Which design response is most robust?
MCQ-9A config loader builds `settings` by inserting keys in file order, then a later pass runs `settings['timeout'] = 30` for a key that was already inserted. The loader then iterates `settings` to write the file back out. What does that reassignment do to `'timeout'`'s position in the iteration order?
MCQ-11A settings loader reads `value = flags.get(name)` and falls back to a default whenever the result is `None`, but operators may explicitly set a flag to `None` to disable it. The loader must distinguish an explicitly disabled flag from one that was never set. Which check does that?
MCQ-12A handler merges per-request settings over module defaults with `merged = defaults | overrides`; `timeout` is set in both dicts, and `defaults` is read by other handlers after this line runs. Which statement describes the result?
MCQ-10Code stores `view = config.items()`; a writer then updates one key in `config`, deletes and re-adds another, and inserts a new key, while a health check compares `config` against a freshly parsed dict with the same pairs. What should a reviewer expect?
MCQ-7A class gives every instance the same hash and separates keys only by an id field: class Key: def __init__(self, k): self.k = k def __hash__(self): return 0 def __eq__(self, other): return isinstance(other, Key) and other.k == self.k d = {Key(1): 'one', Key(2): 'two'} The lookups d[Key(1)] and d[Key(2)] use freshly constructed objects and return 'one' and 'two'. What does each lookup do to tell the two entries apart?
MCQ-16A nested loop evaluates `if candidate in allowed_ids:` about 10^6 times. `allowed_ids` is a 200,000-element list built once at import and never mutated, and the same value is passed to a helper wrapped in `functools.lru_cache`. Profiling shows the membership test dominates the runtime. Which change removes that cost?
MCQ-13A test asserts `list(permissions) == ['read', 'write', 'admin']` for a set built from those three strings; it passes locally and fails in CI. Which explanation is correct?
MCQ-14Two Python sets `a` and `b` hold integer IDs. A job must produce a new set containing every element that is in one set but not the other, elements in both are already matched and must be excluded, and neither input set may be mutated. Which of these produces that set?
MCQ-15A service derives a cache key from a user's stable permissions. Two users with the same permissions must produce the same key regardless of the order the permissions were granted in, and a permission listed twice must not change the key. Which built-in value should that key be?
MCQ-22A Python workload performs frequent append-left and pop-left operations on a sequence. Which container is the better default?
MCQ-3A queue is backed by a large Python list and repeatedly removes the element at index 0; FIFO order must be preserved. Which redesign best addresses the dominant cost?
MCQ-23A worker keeps pending jobs in a list: it enqueues with `jobs.append(job)`, dequeues with `jobs.pop(0)`, and a dashboard reads `jobs[i]` by index and `jobs[-50:]` for the recent-jobs panel. A reviewer proposes swapping in `collections.deque`, citing its O(1) appends and pops at both ends. Which assumption behind the swap is unsafe?
MCQ-25A pipeline processes 2 million events, each carrying a `user_id`. The dedup stage drops events whose `user_id` was already seen and emits the surviving ids in first-seen order for a downstream report. Today it keeps `seen = []` and runs `if uid in seen: continue`; the stage takes 41 minutes. Swapping `seen` for a `set` cuts that to 90 seconds, but the report now lists ids in a different order on every run. Python 3.11. Which change restores first-seen order without giving back the speedup?
MCQ-24A usage-metering service keeps one running balance per customer for a monthly report. Refunds may leave a customer below zero, a customer who has been seen must keep its row and its first-seen position in the report, and a customer who has never been seen must read as 0 without gaining a row. Which state object satisfies all three requirements?