Prepare Python Dictionaries Interview Questions

Prepare with structured review, scenarios and interview practice.

Python Interview Prep · tied to the dictionaries tutorial

Explain dictionary decisions, not just dictionary syntax.

Use these as speaking drills after the tutorial. Answer aloud before revealing the model. Strong answers name the requirement, trace the state, explain failure behavior, and make the trade-off explicit.

Foundation · Question 1

What problem does a Python dictionary model better than a list?

Speak first.

Give your answer before opening the model response. State the invariant or execution state that drives your choice.

Reveal model answer

A dictionary models key → value lookup when a stable identity such as SKU, user ID, or composite key should retrieve associated data. A list models sequence/position and may allow duplicates.

What the interviewer is testing

Name the operation first. Interviewers want to hear why the structure fits the requirement, not “dict is faster.”

Foundation · Question 2

Why can a tuple be a dictionary key while a list normally cannot?

Speak first.

Give your answer before opening the model response. State the invariant or execution state that drives your choice.

Reveal model answer

Dictionary keys must be hashable. A tuple can be hashable when all of its elements are hashable; a list is mutable and therefore not hashable.

What the interviewer is testing

The important idea is stable lookup identity. Avoid drifting into CPython implementation details unless asked.

Intermediate · Question 3

When would you prefer d[key] over d.get(key)?

Speak first.

Give your answer before opening the model response. State the invariant or execution state that drives your choice.

Reveal model answer

Use d[key] when absence violates an invariant and should surface as KeyError. Use get when absence is expected and has an explicit fallback.

What the interviewer is testing

This tests error semantics. Silently defaulting required data can hide broken assumptions.

Debugging · Question 4

A cache stores index 0, but if cache.get(key): says the key is missing. Explain the bug.

Speak first.

Give your answer before opening the model response. State the invariant or execution state that drives your choice.

Reveal model answer

The code is testing truthiness, not presence. Zero is a legitimate stored value but is falsy. Use key in cache or an explicit sentinel depending on the requirement.

What the interviewer is testing

Good debugging answers distinguish missing, present-with-None, and present-with-a-falsy-value.

Debugging · Question 5

Why does groups.get(category, []).append(item) lose the item when category is absent?

Speak first.

Give your answer before opening the model response. State the invariant or execution state that drives your choice.

Reveal model answer

get returns the fallback list but does not insert it. append mutates that temporary list, which is then discarded. Use setdefault, defaultdict, or an explicit branch when the collection should become owned by the mapping.

What the interviewer is testing

Trace object ownership and mutation instead of memorizing one replacement method.

Intermediate · Question 6

Does changing an existing dictionary value move its key to the end?

Speak first.

Give your answer before opening the model response. State the invariant or execution state that drives your choice.

Reveal model answer

No. Current Python preserves insertion order, and updating an existing key retains its position. Deleting and reinserting the key creates a new insertion position.

What the interviewer is testing

Insertion order is a language guarantee, but it is not the same as sorted order.

Scenario · Question 7

You need to count API errors by error code. How would you model the state?

Speak first.

Give your answer before opening the model response. State the invariant or execution state that drives your choice.

Reveal model answer

Use error code → frequency, for example counts[code] = counts.get(code, 0) + 1 or collections.Counter when counting is the central operation.

What the interviewer is testing

State the invariant: each key identifies a category; the value records how many events belong to it.

Scenario · Question 8

A dictionary is used as an in-process cache of database rows. What design questions remain unsolved by choosing dict?

Speak first.

Give your answer before opening the model response. State the invariant or execution state that drives your choice.

Reveal model answer

You still need a source of truth, invalidation/expiry, acceptable staleness, cache-miss behavior, memory bounds, concurrency assumptions, and what happens after process restart.

What the interviewer is testing

A mapping solves lookup representation; it does not automatically solve cache consistency or lifecycle.

Trade-off · Question 9

When is a set better than a dictionary?

Speak first.

Give your answer before opening the model response. State the invariant or execution state that drives your choice.

Reveal model answer

When the requirement is unique membership only and there is no meaningful value associated with each key. A set communicates that invariant directly.

What the interviewer is testing

Prefer the structure that makes the business operation obvious, not one that merely can be made to work.

Trade-off · Question 10

A deeply nested dictionary is shared across a large codebase and small shape changes break many callers. What would you change?

Speak first.

Give your answer before opening the model response. State the invariant or execution state that drives your choice.

Reveal model answer

Consider a domain type or helper API that centralizes the schema and invariants, using dictionaries internally only where their flexibility remains useful.

What the interviewer is testing

This tests whether you recognize the cost of representation leakage. Flexibility is valuable until every caller becomes coupled to five levels of ad-hoc keys.