Python · Interview

Python interview questions

Five Python interview questions that move from data structures to a realistic stock-report problem. Predict the result before running code, then explain the edge cases and trade-offs aloud. The runnable answers are checked against CPython during the build.

Every example verified5 questions · 2 concept · 1 predict the output · 1 coding · 1 scenario
ConceptJunior

A warehouse records repeated changes to each item. When would you use a dictionary instead of a list of pairs?

Answer

Use a dictionary when the main operation is finding and updating a total by item code: each code maps to one current quantity, so an update is usually constant-time. A list of pairs is useful if event order or duplicate records must be retained, but repeatedly searching that list for a code takes linear time. Keep the input events separately if an audit trail is required; a summary dictionary should not pretend to be that audit trail.

Predict the outputMid-level

What does this sorting code print, and why does the tie not preserve the input order?

 Python · what does this print?
items = [('tea', 5), ('flour', 5), ('rice', 8)]
print(sorted(items, key=lambda item: (-item[1], item[0])))

Answer

It prints rice first because -8 sorts before -5, then flour before tea because the second key component compares codes alphabetically. The tie does not retain tea before flour: the key explicitly asks for alphabetical ordering, which overrides input order. Python's stable sort matters only when complete sort keys compare equal. This is a useful way to state a business tie-break without a second pass.

It prints

[('rice', 8), ('flour', 5), ('tea', 5)]
CodingMid-level

Write a function that returns a non-negative restock shortfall from current units, daily demand and lead days; show the result for (7, 3, 4) and (20, 2, 5).

Answer

Required stock is daily demand multiplied by lead days. Subtract current stock and clamp at zero: an item with more than enough stock should not have a negative order quantity. The first item needs twelve units but has seven, so its shortfall is five; the second needs ten but has twenty, so it needs none. In production, validate non-negative inputs and decide how to handle demand forecasts that change over time.

 Python · reference answer
def shortfall(current, daily, days):
    return max(0, daily * days - current)

print(shortfall(7, 3, 4))
print(shortfall(20, 2, 5))

Output

5
0
ScenarioSenior

One warehouse row has a missing lead time. Should a restock report silently assume zero, skip the row or stop?

Answer

Do not silently turn missing lead time into zero: that makes an urgent item appear fully stocked. Agree on a data-quality policy with the report owner. A useful default is to reject or quarantine the row, report its item code and reason, and still compute the valid rows; a critical procurement workflow may instead fail the whole report. Keep rejected-row counts visible, log enough detail for correction, and never mix unknown values with genuine zero-day delivery.

ConceptMid-level

What is the time cost of building a dictionary of N stock records and then sorting its items for a report?

Answer

Accumulating N records in a dictionary is O(N) on average, assuming ordinary hash-table behaviour. Sorting K distinct item codes costs O(K log K), so the combined cost is O(N + K log K), with O(K) summary storage. Do not claim that dictionary iteration itself is an alphabetical report: even if insertion order is retained, that order describes input, not the desired ranking. The sort key should also specify how equal shortages are resolved.

How this page was checked. Every program on it was run with CPython 3.11 at build time; runs in your browser on CPython 3.14 (Pyodide) by the publishing checks, and the output shown is what it printed. The Run buttons execute the same code on your device; nothing you type is sent anywhere.