Programming interviews evaluate the reasoning behind code as much as the final answer. A good explanation states the input contract, identifies edge cases, chooses a data structure and checks complexity. These questions are common practice themes rather than a guaranteed list from a specific employer. For each example below, make your assumptions explicit and test the answer with small inputs first.
Question: when would you use a set rather than a list?
A list preserves order and can contain duplicates. A set is useful for membership and uniqueness checks. If a service must list signups in chronological order, keep ordered records; if it must detect repeated identifiers, an auxiliary set may help. Explain insertion costs, memory use and whether ordering matters in your chosen language.
Question: detect duplicates in an array
This Python implementation returns True as soon as it encounters a previously seen value. It assumes the items are hashable; lists and dictionaries cannot be keys in a set without conversion. The expected average time grows linearly, with additional memory proportional to distinct values.
def has_duplicate(items):
seen = set()
for item in items:
if item in seen:
return True
seen.add(item)
return False
print(has_duplicate([2, 5, 2])) # True
print(has_duplicate([])) # False
Question: explain stack versus queue
A stack removes the most recently added item first; a queue processes earlier arrivals first, subject to scheduling and priority policy. Use a stack for parser state or undo history; use a queue for pending jobs. In a production queue, acknowledge processing and account for redelivery instead of assuming “exactly once” is automatic.
Question: what does a hash table buy you?
Hash tables support average-case near-constant lookup for suitable keys. Explain collision handling, resizing, hashing cost and memory trade-offs. They do not guarantee constant performance for every input. For ordered range queries a balanced tree or database index may be better.
Question: reason about a binary search
Binary search requires an input ordered according to the same comparator used by the search. Explain why the midpoint halves the remaining range and why wrong boundary updates can create infinite loops. Try absent values, the first and last positions, an empty array and duplicate values.
def contains(sorted_values, target):
low, high = 0, len(sorted_values)
while low < high:
mid = low + (high - low) // 2
if sorted_values[mid] < target:
low = mid + 1
else:
high = mid
return low < len(sorted_values) and sorted_values[low] == target
print(contains([1, 3, 5, 9], 5)) # True
Question: how do you handle a failing API call?
Start with timeouts, appropriate retries for idempotent operations, validation of response status and bounded input size. Do not blindly retry non-idempotent payments or writes. Describe how you would log a correlation ID and protect credentials in error messages. A strong answer acknowledges uncertainty instead of inventing a successful response.
Question: explain a race condition
Two workers can read the same value and both write a conflicting update. Describe how a database transaction, atomic operation or carefully scoped lock protects the invariant. Unit tests may miss timing-dependent races, so use stress tests and supported race detectors where applicable.
Practice method: explain, implement, challenge
For every question, describe the contract verbally, write a small implementation, state complexity and create two tests that could disprove your first idea. Keep a record of why you changed the approach. This demonstrates reasoning across interview formats without relying on memorized scripts.