To reverse an array in place, the standard approach is the two-pointer technique, where you swap elements from the outside in until the pointers meet. This avoids allocating extra memory for a new array, keeping the operation O(1) in space complexity.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
How It Works
Left Pointer: Starts at the beginning of the array (index 0).
Right Pointer: Starts at the end of the array (index n-1).
Swap: Exchange the values at the left and right pointers.
Move Inward: Increment the left pointer and decrement the right pointer.
Termination: Stop when the pointers cross or meet.
Step-by-Step Example
Given arr = [1, 2, 3, 4, 5]: 1. left=0, right=4 → swap arr[0] and arr[4] → [5, 2, 3, 4, 1] 2. left=1, right=3 → swap arr[1] and arr[3] → [5, 4, 3, 2, 1] 3. left=2, right=2 → loop condition left < right false, stop.
Complexity Analysis
Time: O(n) — You perform roughly n/2 swaps, so linear time regardless of input order.
Space: O(1) — Only a few variables are used; the input array is modified directly. The recursion stack is not used since this is iterative.
Comparison
Approach
Time Complexity
Space Complexity
In-Place?
Two-pointer swap
O(n)
O(1)
Yes
New array copy
O(n)
O(n)
No
Built-in reverse()
O(n)
O(1)
Yes
Code Example
def reverse_array(arr: list) -> None:
"""Reverse the input list in place using the two-pointer technique."""
left, right = 0, len(arr) - 1
while left < right:
arr[left], arr[right] = arr[right], arr[left]
left += 1
right -= 1
# Example call:
# nums = [1, 2, 3, 4, 5]
# reverse_array(nums)
# print(nums) # Output: [5, 4, 3, 2, 1]
Common Pitfalls
Off-by-one errors: Using left <= right will swap the middle element with itself (harmless) or cause index errors depending on implementation.
Ignoring edge cases: The loop naturally handles empty arrays and single-element arrays because 0 < -1 and 0 < 0 are both false.
Using built-ins that aren’t in-place: reversed() returns an iterator, and slicing arr[::-1] creates a new list, both violating the in-place constraint.
Mutation vs. Return: The function modifies the list in place and returns None; do not expect a new list to be returned.
2
How do arrays differ from linked lists in storage and performance?
KnownRevise
An array stores elements in contiguous memory, while a linked list stores elements in separate nodes connected by references. Arrays provide faster random access and better cache locality; linked lists provide efficient insertion and deletion when the affected node is already known.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
Core Differences
Both are linear data structures, but their storage models differ:
Array: Elements occupy consecutive memory locations. Each position is calculated directly from the array’s base address and index.
Linked list: Each node contains a value and one or more references to neighboring nodes. Nodes can be distributed throughout memory.
Fixed-size array: Has a predetermined capacity and cannot grow without allocating a new array.
Dynamic array: Grows by reserving additional capacity. Python’s list is a dynamic array.
Linked list: Is inherently dynamic because nodes are allocated separately, although deleting nodes does not automatically reduce the memory retained by the runtime.
Arrays and linked lists are concrete implementations, not abstract data types. A stack or queue, for example, is an abstract data type that can be implemented using either one.
Operation Complexity
The following costs assume an unsorted structure and n elements.
Structure
Access
Search
Insert
Delete
Extra Memory per Element
Order Kept
Fixed-size array
O(1)
O(n)
O(n) if capacity is available; otherwise requires a new array
O(n)
O(1) per slot
Yes
Dynamic array
O(1)
O(n)
O(n) in the middle; O(1) amortized at the end
O(n) in the middle; O(1) amortized at the end
O(1) per slot, plus unused capacity
Yes
Singly linked list
O(n)
O(n)
O(1) after finding the insertion point
O(1) after finding the node
O(1) reference per node, plus node overhead
Yes
Best, average, and worst case: Access, search, and dynamic-array shifting are O(1) or O(n) in all these cases. Linked-list insertion and deletion are O(1) once the node is known.
Finding a position by value in a linked list still takes O(n), so linked-list insertion is not necessarily faster end to end.
Deleting the final element of a dynamic array can require shrinking its backing storage, making that occasional operation O(n) even though deletion is normally O(1) amortized.
Both structures require O(n) total storage, excluding runtime-specific object overhead. A single operation generally uses O(1) auxiliary space.
Neither implementation in this comparison uses recursion, so there is no recursion-stack contribution.
Memory and Performance
Cache locality: Arrays are contiguous, so the CPU can often load nearby elements into the cache efficiently. Linked-list nodes may be scattered across memory, causing more cache misses.
Memory overhead: A linked-list node stores both the value and a reference, so it uses more memory per element than a raw array slot.
Resizing: A dynamic array occasionally copies its elements when it grows, but amortized O(1) append cost makes this acceptable for typical workloads.
Pointer safety: Linked-list mutations require careful handling of next-node references to avoid losing nodes or creating cycles.
Python 3.12 Mapping
Use a list when indexed access, iteration, or appending dominates.
Use collections.deque when efficient insertion and removal at both ends are required. It is not a general-purpose linked list.
Python does not provide a built-in linked list. A custom implementation would need a node class containing a value and a reference to the next node.
dict and set use hash tables rather than arrays or linked lists.
tuple is an immutable sequence, while array.array provides a compact typed array with a fixed element type.
For example, inserting X after B in [A, B, C] shifts C to the right in an array. In a linked list, the node containing B can point directly to a new X node without moving C.
Choosing Between Them
Choose an array for lookup tables, image buffers, numeric collections, and workloads requiring frequent indexed access. Its main trade-off is the O(n) cost of inserting or deleting elements in the middle.
Choose a linked list when frequent insertion or deletion occurs after a node has already been located, such as maintaining an LRU cache with a hash map and linked list. An array-backed deque is often a better practical alternative when only front and rear operations are needed.
Interview Tips
State whether “array” means fixed-size or dynamic before discussing resizing.
Distinguish O(1) linked-list insertion from the O(n) search needed to locate the insertion point.
Mention cache locality and pointer overhead, not only asymptotic complexity.
A common follow-up asks: “What would you choose if the data does not fit in memory?” Consider streaming, chunking, or an external-memory algorithm rather than assuming everything can be loaded at once.
3
How do you detect duplicate values in an array without extra space?
KnownRevise
For bounded integer values, use in-place cyclic placement to detect duplicates in O(n) time and O(1) auxiliary space, while modifying the array. For unrestricted integers, sort the array with an in-place heap sort and compare adjacent elements, taking O(n log n) time and O(1) auxiliary space.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
How It Works
Cyclic placement works when all values fit into a contiguous range containing fewer than n + 1 distinct positions. Typical examples include arrays whose values are 0 through n - 1.
For each position, treat the value as an index:
Calculate the value’s target position.
If another element already occupies that target with the same value, a duplicate exists.
Otherwise, swap the elements and continue until the current element reaches its target position.
Each swap places at least one element correctly, so the algorithm performs at most O(n) swaps.
For example, with [3, 1, 3, 0]:
Step
Operation
Array
1
Move 3 from index 0 to index 3
[0, 1, 3, 3]
2
Index 2 also contains 3, which is already at index 3
Duplicate found
Complexity Comparison
Approach
Best time
Average time
Worst time
Extra space
When to use
Pairwise comparison
O(1) with immediate exit
O(n²)
O(n²)
O(1)
Simple solution for small arrays
Hash set
O(1) with immediate exit
O(n)
O(n²) under persistent collisions
O(n)
Fastest general approach, but violates the space constraint
In-place heap sort
O(n log n)
O(n log n)
O(n log n)
O(1)
Arbitrary comparable values
Cyclic placement
O(n)
O(n)
O(n)
O(1)
Values have a suitable bounded range
Auxiliary space is O(1) for both in-place implementations and does not include the input array. The input is modified.
Code Example
The first function supports arbitrary integers using in-place heap sort. The second provides the faster O(n) solution when the value range is suitable.
def has_duplicate(values: list[int]) -> bool:
"""Return whether values contains a duplicate, sorting values in place."""
size = len(values)
if size < 2:
return False
def sift_down(root: int, heap_size: int) -> None:
while True:
child = 2 * root + 1
if child >= heap_size:
return
if (
child + 1 < heap_size
and values[child + 1] > values[child]
):
child += 1
if values[root] >= values[child]:
return
values[root], values[child] = values[child], values[root]
root = child
# Build a max heap.
for root in range(size // 2 - 1, -1, -1):
sift_down(root, size)
# Move the largest remaining value to its final position.
for end in range(size - 1, 0, -1):
values[0], values[end] = values[end], values[0]
sift_down(0, end)
# Equal values are now adjacent.
for index in range(1, size):
if values[index] == values[index - 1]:
return True
return False
def has_duplicate_bounded(values: list[int]) -> bool:
"""Detect duplicates in place when max(values) - min(values) < len(values)."""
size = len(values)
if size < 2:
return False
minimum = min(values)
maximum = max(values)
if maximum - minimum >= size:
raise ValueError("Values do not fit in the required bounded range")
for index in range(size):
while values[index] - minimum != index:
target = values[index] - minimum
# Another element already has the value that belongs at target.
if values[target] - minimum == target:
return True
values[index], values[target] = values[target], values[index]
return False
print(has_duplicate([3, 1, 3, 0])) # True
print(has_duplicate_bounded([3, 1, 3, 0])) # True
Edge Cases
Empty input or one element: returns False.
Duplicate values: returns True.
Negative values: supported by the heap-sort version and by the bounded version when all values satisfy its range requirement.
Already sorted input: handled normally; the heap-sort version still takes O(n log n).
Values outside the bounded range: the bounded function raises ValueError rather than producing an incorrect result.
Common Pitfalls
Python’s built-in list.sort() is not an O(1)-space solution; Timsort can use O(n) temporary memory.
A set detects duplicates quickly but requires O(n) additional space.
Repeatedly checking value in values is O(n) per membership test, resulting in O(n²) total time.
Both implementations mutate the input, which may be unacceptable if the caller requires it to remain unchanged.
In an interview, clarify the permitted value range and whether modifying the array is allowed; these determine whether O(n) cyclic placement is valid.
4
How do you perform binary search on a sorted array?
KnownRevise
Binary search finds a target in a sorted array by repeatedly searching only half of the remaining range. Each comparison eliminates half the candidates, producing logarithmic running time.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
How It Works
For a textbook binary search on an ascending array:
Compare the target with the middle element.
If they match, return the middle index.
If the target is smaller, continue in the left half.
If the target is larger, continue in the right half.
Continue until the range becomes empty, meaning the target is absent.
The key invariant is that the target, if present, always remains within the current search interval.
For example, search for 7 in [1, 3, 5, 7, 9]:
Step
Search range
Middle value
Action
1
[1, 3, 5, 7, 9]
5
Target is larger
2
[7, 9]
7
Target matches
In Python 3.12, bisect.bisect_left() performs this kind of binary search on a sorted sequence. It returns the insertion position for the target, which is the first occurrence when duplicates exist.
Complexity
Textbook early-exit search
Best time:O(1) if the first examined middle element matches.
Average time:O(log n) because each comparison approximately halves the search range.
Worst time:O(log n) when the target is absent or near the end.
Auxiliary space:O(1); the input array is not counted.
bisect_left()
Best, average and worst time:O(log n) because it computes a lower bound rather than returning immediately upon an equality match.
Auxiliary space:O(1); the input sequence is not counted.
Total space including the input:O(n).
Code Example
from bisect import bisect_left
def find_index(sorted_values: list[int], target: int) -> int:
"""Return the first index of target, or -1 when target is absent."""
index = bisect_left(sorted_values, target)
if index < len(sorted_values) and sorted_values[index] == target:
return index
return -1
print(find_index([1, 3, 5, 7, 9], 7)) # Expected output: 3
For a sorted array containing duplicates, bisect_left() returns the index of the first matching value. Use bisect_right() when you need the insertion position after all equal values.
Edge Cases
Input or condition
Expected behavior
Empty array
Return -1
One element
Return 0 if it matches; otherwise return -1
Duplicate values
Return the first matching index
Negative numbers
Work normally when the array is sorted
Already sorted input
Expected and required condition
Missing target
Return -1
Unsorted input
Results are undefined; sort first if necessary
The implementation does not mutate the input. A Python list is a dynamic array rather than a fixed-size low-level array, but it supports the same indexing and slicing operations needed here.
Common Pitfalls
Assuming the input is sorted: Binary search produces incorrect results on an unsorted array.
Using bisect_left() without checking equality: It returns an insertion position, which may be past the end of the sequence.
Off-by-one errors: Carefully handle empty ranges and middle-element selection.
Confusing duplicate behavior: bisect_left() finds the first equal position; bisect_right() finds the position after the equal range.
Forgetting the sortedness requirement: If sorting is required first, add its O(n log n) time cost when analyzing the complete operation.
Typical follow-up: Interviewers may ask how to find the first or last occurrence, return all matching indices, or count values below a threshold.
5
How do you rotate a 2D matrix 90 degrees clockwise?
KnownRevise
Rotate an N × N matrix 90 degrees clockwise by transposing it across the main diagonal and then reversing the order of elements in each row. This can be done in place, using constant auxiliary space.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
How It Works
For a square matrix:
Transpose the matrix by swapping matrix[i][j] with matrix[j][i] for every position above the diagonal.
Reverse each row using row.reverse().
The resulting matrix represents the original matrix rotated 90 degrees clockwise.
The common assumption is that the matrix is square. Rotating a rectangular M × N matrix produces an N × M matrix, which normally requires a new allocation. For that case, the original element at matrix[i][j] moves to rotated[j][M - 1 - i].
Complexity
For an N × N matrix:
Measure
Complexity
Reason
Best case
O(N²)
Every element participates in the transpose and row reversals
Average case
O(N²)
The algorithm performs the same work regardless of values
Worst case
O(N²)
The transpose and reversals together perform O(N²) element work
Auxiliary space
O(1)
The existing matrix is modified without additional containers
The input matrix is modified in place and is not counted as auxiliary space. For a rectangular matrix, creating the rotated result takes O(MN) time and O(MN) additional space.
Code Example
from typing import TypeVar
T = TypeVar("T")
def rotate_matrix_90_clockwise(matrix: list[list[T]]) -> None:
"""Rotate a square matrix 90 degrees clockwise in place."""
if not matrix:
return
row_count = len(matrix)
column_count = len(matrix[0])
if any(len(current_row) != column_count for current_row in matrix):
raise ValueError("The matrix must be rectangular")
if row_count != column_count:
raise ValueError("The matrix must be square")
# Transpose across the main diagonal.
for row in range(row_count):
for column in range(row + 1, column_count):
matrix[row][column], matrix[column][row] = (
matrix[column][row],
matrix[row][column],
)
# Reverse each row to change the transpose into a clockwise rotation.
for row in matrix:
row.reverse()
example_matrix = [[1, 2], [3, 4]]
rotate_matrix_90_clockwise(example_matrix)
print(example_matrix) # [[3, 1], [4, 2]]
Edge Cases
Empty matrix: [] is left unchanged.
One element: [[5]] remains [[5]].
Duplicates and negative values: Their positions rotate correctly because values are never compared or modified.
Rectangular matrix: The function raises ValueError because an in-place rotation would change the matrix dimensions.
Ragged rows: The function raises ValueError rather than producing an invalid result.
Already rotated matrix: Applying the function four times restores the original matrix.
Common Pitfalls
Reversing the order of rows before transposing also produces a clockwise rotation. To produce a counterclockwise rotation, transpose first and then reverse the order of the rows.
Only swap entries above the main diagonal. Swapping every (i, j) pair swaps each off-diagonal pair twice and undoes the transpose.
Remember that Python’s list.reverse() is O(N) time and uses O(1) auxiliary space.
For a rectangular matrix, explicitly decide whether returning a newly allocated N × M matrix is acceptable.
A frequent interview follow-up asks for a counterclockwise rotation; use the transpose followed by reversing the order of the rows.
6
How does run-length encoding compress a string such as “aabbccc”?
KnownRevise
Run-length encoding (RLE) replaces each consecutive group of identical characters with the character followed by the group’s length. Thus, "aabbccc" becomes "a2b2c3".
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
How It Works
The algorithm scans the string from left to right while tracking:
The current character in the consecutive run.
The run length, or number of times that character has appeared consecutively.
When a different character is encountered, the completed run is encoded as character + count. The final run must also be flushed after the scan finishes.
RLE is most effective for strings with many adjacent repetitions, such as formatted text, pixel runs, or repeated-status records. It can expand data with few repetitions—for example, "abc" becomes "a1b1c1".
Complexity
Let n be the input length and r the number of consecutive runs.
Best-case time:O(n) because the entire input must be inspected.
Average-case time:O(n) because each character is processed once.
Worst-case time:O(n) because each character is processed once.
Space:O(r) output-fragment entries and up to O(n) characters for the constructed result. The recursion stack is O(1) because the implementation is iterative. These figures exclude the original input; including the input and result gives O(n) total space.
Code Example
def run_length_encode(text: str) -> str:
"""Encode consecutive equal characters as the character and run length."""
if not text:
return ""
encoded_parts: list[str] = []
current_character = text[0]
run_length = 1
for index in range(1, len(text)):
character = text[index]
if character == current_character:
run_length += 1
else:
encoded_parts.append(f"{current_character}{run_length}")
current_character = character
run_length = 1
# Flush the final run.
encoded_parts.append(f"{current_character}{run_length}")
return "".join(encoded_parts)
print(run_length_encode("aabbccc")) # a2b2c3
Example Walkthrough
The table shows the state after processing each input character:
Input character
Current run
Encoded output so far
a
a, length 1
""
a
a, length 2
""
b
b, length 1
"a2"
b
b, length 2
"a2"
c
c, length 1
"a2b2"
c
c, length 2
"a2b2"
c
c, length 3
"a2b2"
After the loop, the final run is flushed, producing "a2b2c3".
Common Pitfalls
Off-by-one errors: Initialize the first run explicitly, then flush the last run after the loop.
Consecutive versus global counts: RLE counts adjacent equal characters, not every occurrence of a character across the string.
Empty and single-character inputs: An empty string returns ""; a one-character string such as "a" returns "a1".
Ambiguous output: The compact format "a2b2c3" is not automatically reversible when original characters may be digits or counts may contain multiple digits. A production codec needs separators, escaping, or length-prefixed records.
Character semantics: Python strings contain Unicode code points, so the algorithm counts code points rather than user-perceived characters or grapheme clusters.
Common follow-up: Interviewers may ask how to decode RLE, how to add a separator for unambiguous decoding, or how to choose between RLE and a more advanced compression algorithm.
7
How do array slices work across programming languages?
KnownRevise
An array slice is a contiguous subsequence of an array, commonly expressed as array[start:end]. Languages implement slices either by copying the selected elements into new storage or by creating a lightweight view that references the original data, so identical-looking syntax can have different performance and mutation behavior.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
How It Works
An array is a linear, contiguous, non-primitive data structure. A slice usually selects a start position, an end position, and sometimes a step:
Copying: The selected elements are placed in a new array. Modifying the slice does not normally affect the source.
Viewing: A small descriptor stores a reference, offset, length, and sometimes capacity or stride. Modifying the slice may affect the source.
Strided slicing: Some numerical libraries support nonconsecutive elements through a stride. A one-dimensional view may also expose a stride, although ordinary language slices usually select a contiguous range.
The end index is commonly exclusive. Exact handling of negative indices, out-of-range bounds, and steps varies by language.
Language Behavior
Language or library
Typical result
Storage behavior
Python 3.12 list
New list
Copies element references; source is independent
Python 3.12 array.array
New typed array
Copies packed element values
Python 3.12 memoryview
Buffer view
Shares the source buffer
Go a[start:end]
Slice header
Shares the backing array
Java 21 Arrays.copyOfRange
New array
Copies elements
C++20 std::span
Span view
References an existing array
Rust &values[start..end]
Borrowed slice
References existing elements
NumPy 2.x basic slicing
Array view
Usually shares memory
Python lists and array.array objects use copy semantics, but memoryview provides standard-library access to view semantics.
Complexity and Python Structures
For a slice containing k elements:
Copy-based slice: O(k) best, average, and worst-case time because every selected element is copied. It requires O(k) extra space, excluding the original input.
View-based slice: O(1) best, average, and worst-case creation time because only metadata is created. Reading all k elements takes O(k) time. The extra space is O(1), excluding the original source data.
Resizing a fixed view: Usually unsupported. Changing existing elements through a view can modify the source.
Python structure costs:
Structure
Access
Search
Middle insert/delete
Extra memory per element
Order and slice behavior
list
O(1)
O(n) average and worst
O(n) average and worst
One pointer/reference slot
Preserves order; slices are copies
array.array
O(1)
O(n) average and worst
O(n) average and worst
Packed numeric storage
Preserves order; slices are copies
memoryview
O(1)
O(k) average and worst for a k-element view
Not supported through the view
No duplicated elements
Preserves order; shares source storage
Python list append is O(1) amortized, but list.pop(0) and list.insert(0, value) are O(n) because elements must shift.
Example Walkthrough
Given [10, 20, 30, 40, 50] and the slice [1:4]:
Start at index 1, producing 20.
Continue through index 3, producing 30 and 40.
Exclude index 4.
The result is [20, 30, 40].
For a copy, three values are stored in a new container. For a view, only a reference to that range is created.
Assuming every slice is a view: Check the language’s documentation; Python list slicing creates a copy.
Assuming copying is deep: Python list slices copy references, so nested mutable objects remain shared.
Ignoring aliasing: A view can cause source data to change unexpectedly.
Overlooking Python bounds rules: Negative indices are supported, out-of-range bounds are clamped, and a zero step raises ValueError.
Edge cases: Empty inputs and empty results work normally; one-element, duplicate, and negative values are preserved unchanged. Sortedness, disconnected graphs, and cycles do not affect slicing.
8
What are the time complexities of array insertion and deletion?
KnownRevise
Array insertion and deletion are generally O(n) when existing elements must be shifted. At an end position, a dynamic array can perform these updates in O(1) amortized time, although an individual resize or compaction may take O(n).
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
Core Complexity
Arrays are linear and contiguous structures. A fixed-size array is static, while a dynamic array can grow or shrink. Elements may be primitive or non-primitive, depending on the language and array type.
Insertion in the middle: O(n) because elements after the insertion point must shift right.
Deletion in the middle: O(n) because elements after the deleted position must shift left.
Insertion at the end:
Fixed-size array: O(1) if capacity remains; otherwise insertion is impossible.
Dynamic array: O(1) amortized because occasional capacity growth costs O(n).
Deletion at the end:
Fixed-size array: O(1).
Dynamic array: O(1) for removing the element. If the implementation periodically compacts to reclaim capacity, an individual compaction can cost O(n), giving O(1) amortized time across a sequence of deletions.
Access by index: O(1).
Search by value in an unsorted array: O(n) average and worst case, with O(1) best case if the first element matches.
For an arbitrary insertion or deletion position, the best case is O(1) at the end, the average case is O(n) when the position is selected uniformly, and the worst case is O(n) when most elements must shift.
In Python 3.12, list is the usual dynamic-array implementation. list.append() is O(1) amortized, list.pop() from the end is O(1), while list.insert(index, value) and list.pop(index) cost O(n) when elements must shift.
Operation Comparison
Structure
Access
Search
Insert
Delete
Extra Memory per Element
Order
Fixed-size array
O(1)
O(n) average and worst; O(1) best
O(1) best at the end with capacity; O(n) average and worst when shifting; impossible when full
O(1) best at the end; O(n) average and worst when shifting
O(1)
Positional order is preserved
Dynamic array / Python list
O(1)
O(n) average and worst; O(1) best
O(1) best and amortized at the end; O(n) average and worst at an existing position; O(n) worst for one resize
O(1) best at the end; O(1) amortized for end deletion; O(n) average and worst when shifting or compacting
O(1) amortized
Positional order is preserved
Dynamic arrays commonly use geometric capacity growth, so occasional O(n) resizes distribute their cost across many operations. Fixed and dynamic arrays are commonly used for lookup tables, buffers, and image data.
Space Complexity
Auxiliary space: O(1) for shifting elements in place, excluding the input array.
Resize or compaction: O(n) additional memory may be temporarily required because the old and new allocations can coexist.
Recursion stack: O(1), because these operations are iterative.
Total storage including the input: O(n).
The input itself is not counted when reporting auxiliary space.
Contiguous storage is relatively cache-friendly, but shifting many elements creates substantial memory traffic.
Example Walkthrough
Starting with [10, 20, 30]:
Insert 25 at index 2.
30 shifts one position right.
The array becomes [10, 20, 25, 30].
Delete the element at index 1.
20 and 25 shift left.
The result is [10, 25, 30].
These concrete operations shift only one and two elements, respectively. For a general array of length n, either operation is O(n) in the worst case because it may shift a suffix containing Θ(n) elements.
Common Pitfalls
Treating middle insertion or deletion as O(1) ignores the required shifting.
Confusing amortized O(1) with worst-case O(1): one dynamic-array resize or compaction can still cost O(n).
Using list.pop(0) or list.insert(0, value) repeatedly causes O(n) work per operation; use collections.deque for frequent front operations.
Forgetting that a fixed-size array cannot grow beyond its allocated capacity.
Ignoring the temporary O(n) memory required during a resize.
A common follow-up is: “What structure would you use for frequent insertions and deletions at both ends?” A deque is usually the better choice.
9
How can two sorted arrays be merged into one sorted array?
KnownRevise
The standard technique is the two-pointer merge: keep one pointer in each sorted array, repeatedly append the smaller current value, and copy any remaining values afterward. This produces one sorted result in linear time without modifying either input.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
Common Approaches
Approach
Best / Average / Worst Time
Space
When to Use
Two-pointer merge
O(n + m) / O(n + m) / O(n + m)
O(n + m) for the result; O(1) auxiliary
Best general-purpose approach when both inputs are already sorted
Concatenate and sort
O(n + m) best case; O((n + m) log(n + m)) average and worst
O(n + m), including intermediate and result lists
Simple code is more important than efficiency
Merge into a preallocated buffer
O(n + m) / O(n + m) / O(n + m)
O(n + m) for the output buffer; O(1) auxiliary
Low-level implementations or systems that already manage a destination buffer
In-place merge using repeated single-position rotations
O(n + m) / O(nm) / O(nm)
O(1) auxiliary for the rotation method
When allocating a new result is undesirable; the average case assumes random relative ordering
Here, n and m are the lengths of the two arrays. The input arrays themselves are not counted as additional space.
How the Two-Pointer Merge Works
Start at index 0 in both arrays.
Compare the two current values.
Append the smaller value and advance only that array’s pointer.
Repeat until one array is exhausted.
Append the remaining elements from the other array.
For example:
Step
Left value
Right value
Result
1
1
2
[1]
2
4
2
[1, 2]
3
4
5
[1, 2, 4]
4
7
5
[1, 2, 4, 5]
5
7
8
[1, 2, 4, 5, 7]
6
exhausted
8
[1, 2, 4, 5, 7, 8]
Using <= when values are equal preserves a stable order: equal items from the left array appear before equal items from the right array.
Complexity
Time: O(n + m) in the best, average, and worst cases. Each input element is considered once, and Python list appends are amortized O(1), so the total remains O(n + m).
Output space: O(n + m) because the merged list contains every input element.
Auxiliary space: O(1), excluding the output and input lists. The implementation creates no temporary slices and uses no recursion, so there is no recursion-stack cost.
The inputs are not modified.
Code Example
def merge_sorted_arrays(left: list[int], right: list[int]) -> list[int]:
"""Merge two ascending sorted integer lists without modifying either input."""
merged: list[int] = []
left_index = 0
right_index = 0
while left_index < len(left) and right_index < len(right):
if left[left_index] <= right[right_index]:
merged.append(left[left_index])
left_index += 1
else:
merged.append(right[right_index])
right_index += 1
while left_index < len(left):
merged.append(left[left_index])
left_index += 1
while right_index < len(right):
merged.append(right[right_index])
right_index += 1
return merged
print(merge_sorted_arrays([1, 4, 7], [2, 5, 8])) # [1, 2, 4, 5, 7, 8]
The implementation handles empty inputs, one-element inputs, duplicates, negative numbers, and already sorted inputs. It also avoids mutating the original lists.
Common Pitfalls
Forgetting the remaining tail: After the main loop, one array may still contain values that must be appended.
Incorrect boundary checks: Use left_index < len(left) and right_index < len(right), avoiding off-by-one errors.
Assuming the result contains no duplicates: Merging preserves every element unless duplicate removal is explicitly requested.
Mutating inputs unintentionally: Decide whether the API should preserve both source arrays.
Ignoring worst-case memory needs: The result itself requires O(n + m) space even though the merge uses only O(1) auxiliary space.
Using integer overflow in fixed-width languages: Check whether adding array lengths or calculating buffer sizes can overflow.
Interview follow-up: Interviewers may ask about merging many sorted files, performing the merge in place, preserving stability, or handling data larger than memory, such as in external sorting.
10
Find the kth largest element in an unsorted array
KnownRevise
Quickselect finds the kth largest element in an unsorted array in expected linear time by partitioning elements around a pivot and discarding the portion that cannot contain the required rank. This answer treats k as one-based and counts duplicate values as separate elements.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
Approaches and Complexity
Approach
Best
Average
Worst
Extra space
When to use
Sorting with a general comparison sort
O(n log n)
O(n log n)
O(n log n)
O(1)
Simplicity matters more than performance
Min-heap of size k
O(n log(k + 1))
O(n log(k + 1))
O(n log(k + 1))
O(k)
Guaranteed performance or repeated queries
Quickselect
O(n)
O(n) expected
O(n²)
O(1)
One query with good average performance
Quickselect best case, O(n): favorable pivots isolate the answer quickly; three-way partitioning can also finish in one pass when values are equal.
Quickselect average case, O(n): each partition reduces the candidate set by a constant factor on average.
Space, O(1): the implementation is iterative, so there is no recursion stack. The input list itself occupies O(n) space.
How Quickselect Works
Convert the desired rank to a zero-based ascending index: target = n - k.
Choose a pivot and partition the current range into three groups: values less than, equal to, and greater than the pivot.
If target lies before the equal group, continue in the smaller partition.
If it lies after the equal group, continue in the greater partition.
Otherwise, the pivot is the kth largest value.
Three-way partitioning is useful because duplicate values are grouped together instead of being processed individually.
Example Walkthrough
For [7, 3, 9, 5, 1, 8, 2] and k = 3:
The target is zero-based index 4, corresponding to the third-largest position.
One possible pivot is 5; values smaller than 5 cannot contain the target.
In the remaining candidates {7, 8, 9}, a pivot of 8 eliminates 9.
The remaining value is 7, so the answer is 7.
The pivot sequence can differ, but the final result is always 7.
Code Example
from random import randrange
def kth_largest(values: list[int], k: int) -> int:
"""Return the kth largest value, counting duplicates separately.
The input list is reordered in place.
"""
if not values:
raise ValueError("values must not be empty")
if not 1 <= k <= len(values):
raise ValueError("k must be between 1 and len(values)")
target = len(values) - k
low, high = 0, len(values) - 1
while low <= high:
pivot = values[randrange(low, high + 1)]
less = low
current = low
greater = high
# Partition into values < pivot, == pivot, and > pivot.
while current <= greater:
if values[current] < pivot:
values[less], values[current] = values[current], values[less]
less += 1
current += 1
elif values[current] > pivot:
values[current], values[greater] = values[greater], values[current]
greater -= 1
else:
current += 1
if target < less:
high = less - 1
elif target > greater:
low = greater + 1
else:
return pivot
raise RuntimeError("unreachable")
numbers = [7, 3, 9, 5, 1, 8, 2]
print(kth_largest(numbers, 3)) # 7
The implementation handles empty input and invalid k by raising ValueError. It also works for one element, negative numbers, already sorted input, and duplicates. To preserve the original list, call it with numbers.copy(); doing so changes the auxiliary space to O(n).
Common Pitfalls
Rank confusion:k = 1 means the largest element, while k = len(values) means the smallest.
Duplicate semantics: This version counts duplicates separately. A distinct kth-largest variant requires an additional deduplication strategy.
Unexpected mutation: Quickselect reorders the input list.
Ignoring worst-case behavior: Deterministic pivots can produce O(n²) runtime; randomized pivots make the expected runtime O(n).
Using the wrong rank formula: For ascending order, the target index is n - k, not k - 1.
A common follow-up asks how the solution would change for distinct values, repeated queries, or a stream whose size is unknown.
11
How do singly and doubly linked lists differ?
KnownRevise
A singly linked list stores each node with one pointer to the next node, while a doubly linked list gives every node both a previous-node and next-node pointer. Both implement the linked-list ADT, but singly linked lists use less memory, whereas doubly linked lists support backward traversal and constant-time deletion of a directly referenced node.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
Core Difference
Both are linear, dynamic, non-primitive, linked-memory data structures that preserve insertion order.
Singly linked list node: contains data and one next pointer.
Doubly linked list node: contains data and both prev and next pointers.
The first node’s prev and the last node’s next are normally None.
Python 3.12 has no built-in public linked-list or node type. list is array-backed, while collections.deque is a separate built-in sequence optimized for endpoint operations; neither exposes the conventional node-and-link interface.
Operation Costs
The costs depend on whether the required node or position is already known.
Operation
Singly linked list: best
Singly linked list: average/worst
Doubly linked list: best
Doubly linked list: average/worst
Access by index
O(1) for index 0
O(n)
O(1) for index 0
O(n)
Search by value
O(1) for a first-element match
O(n)
O(1) for a first-element match
O(n)
Insert at head
O(1)
O(1)
O(1)
O(1)
Insert before a known node
O(1) if its predecessor is known
O(n) to find the predecessor
O(1)
O(1)
Find a position, then insert
O(1) if the first candidate matches
O(n)
O(1) if the first candidate matches
O(n)
Delete head
O(1)
O(1)
O(1)
O(1)
Delete a node given its reference
O(1) if it is the head or its predecessor is known
O(n)
O(1)
O(1)
Find a position, then delete
O(1) if the first candidate matches
O(n)
O(1) if the first candidate matches
O(n)
Delete tail
O(n) unless a predecessor reference is maintained
O(n)
O(1) with a maintained tail reference
O(1)
Append
O(1) with a maintained tail reference
O(1)
O(1) with a maintained tail reference
O(1)
Index access and value searches are O(n) because nodes must be visited sequentially. A doubly linked list does not make searching faster; it only allows movement and updates in both directions. Once the relevant reference is available, updating a fixed number of links takes O(1).
Memory and Performance
Characteristic
Singly linked list
Doubly linked list
Total storage, including existing list/input nodes
O(n)
O(n)
Auxiliary space per operation
O(1), excluding input/list storage and including the recursion stack, which is zero for iterative operations
O(1), excluding input/list storage and including the recursion stack, which is zero for iterative operations
Extra memory per element
One pointer
Two pointers
Memory layout
Non-contiguous and potentially scattered
Non-contiguous and potentially scattered
Backward traversal
Not directly supported
Supported through prev
Insertion order preserved
Yes
Yes
Maintenance complexity
Lower
Higher because more links must be updated
Both structures generally have more memory overhead and weaker cache locality than a contiguous Python list, because their nodes are separately allocated and may be scattered in memory.
When to Use
Choose a singly linked list when memory usage matters and traversal is primarily forward. Examples include simple history lists, playlists, and representing a process’s next steps.
Choose a doubly linked list when backward traversal or constant-time deletion of a given node is important. A common example is an LRU cache, where a hash table locates entries and a doubly linked list maintains their recency order.
Choose collections.deque when efficient FIFO queue operations are needed without implementing a node-based list yourself. It is useful for BFS, task scheduling, and buffering.
Choose list when indexing and cache-friendly iteration are more important than inexpensive node insertion or deletion. Common uses include lookup tables, image buffers, and configuration collections.
Common Pitfalls
Forgetting that deleting a node in a singly linked list may require finding its predecessor, changing the operation from O(1) to O(n).
Failing to update both prev and next pointers in a doubly linked list, which can detach nodes or create cycles.
Assuming a linked list provides O(1) indexed access; it still requires O(n) traversal.
Ignoring per-node pointer overhead and poor cache locality.
Using a Python list as a queue with pop(0): removing the first element takes O(n), whereas collections.deque.popleft() takes O(1).
Mutating a caller-owned list when the API should preserve the input; copy it first if its original order or contents must remain unchanged.
Interview follow-up: “How would you implement an LRU cache, and how would you make that implementation thread-safe?”
12
How do you detect a cycle in a singly linked list?
KnownRevise
Floyd’s cycle-detection algorithm detects a cycle by moving one pointer one node at a time and another two nodes at a time. If the pointers meet, the list contains a cycle; if the faster pointer reaches the end, it does not.
Read full answerHide full answer
Applies toPython 3.12
How It Works
Initialize slow and fast at the head.
Advance slow by one node and fast by two nodes.
If slow and fast reference the same node after advancing, a cycle exists.
If fast is None or fast.next is None, the list is acyclic.
Once both pointers enter a cycle, the faster pointer gains one node per iteration relative to the slower pointer, so their separation eventually becomes zero.
Python has no built-in linked-list type, so the example defines a minimal Node class.
Complexity
Let n be the number of distinct nodes reachable from head.
Approach
Best case
Average case
Worst case
Auxiliary space
Floyd’s algorithm
O(1) for an empty list or immediate self-cycle
O(n)
O(n)
O(1)
Hash set of visited nodes
O(1) for an empty list or early cycle
O(n) expected
O(n²) with pathological hash collisions
O(n)
Floyd’s time: O(n), because each pointer traverses only a bounded number of list positions before the pointers meet or the end is reached.
Floyd’s space: O(1) auxiliary space; no recursion stack is used.
Hash-set time: O(n) expected because each node is visited once with expected O(1) set operations. The theoretical worst case is O(n²) if hash collisions make those operations O(n) each.
Hash-set space: O(n) because every visited node requires a set entry.
If the input is counted, the list itself occupies O(n) storage, including one next reference per node. Linked nodes may be scattered in memory rather than contiguous and cache-friendly.
Edge Cases
Empty list: head is None, so the function returns False.
One node without a cycle: The fast pointer cannot advance two steps, so the function returns False.
Negative values: Values do not affect detection; only links and node identity matter.
Duplicate values: The function compares node identity with is, not stored values.
Already sorted values: Value ordering is irrelevant because traversal follows next links.
Self-cycle: The pointers meet after one iteration.
Cycle in the middle or at the tail: The pointers meet after traversing the relevant nodes.
Disconnected components or unreachable cycles: Only nodes reachable from head are examined; a cycle in a disconnected component is outside this function’s scope.
Code Example
from __future__ import annotations
class Node:
def __init__(self, value: int, next_node: Node | None = None) -> None:
self.value = value
self.next = next_node
def has_cycle(head: Node | None) -> bool:
"""Return True if the linked list contains a cycle."""
slow = head
fast = head
while fast is not None and fast.next is not None:
slow = slow.next
fast = fast.next.next
if slow is fast:
return True
return False
# Build: 1 -> 2 -> 3 -> 4 -> 2
node1 = Node(1)
node2 = Node(2)
node3 = Node(3)
node4 = Node(4)
node1.next = node2
node2.next = node3
node3.next = node4
node4.next = node2
print(has_cycle(node1)) # True
Interview Tips
Use is, not ==, to compare nodes because separate nodes can contain equal values.
Advance slow once and fast twice; moving both at the same speed does not reliably detect a cycle.
Prefer Floyd’s algorithm when auxiliary memory is constrained; it uses O(1) extra space and does not modify the list.
A hash set is a reasonable alternative when visited-node tracking is already available, but it requires O(n) additional memory.
Be prepared to explain why the pointers meet and how the algorithm handles empty lists, self-cycles, and longer cycles.
13
Which linked list operations are available, and what are their time complexities?
KnownRevise
A linked list is a linear, dynamic, non-contiguous implementation of the list ADT. Operations are O(1) only when the affected node is already known; searching or reaching a position by index generally requires O(n) traversal.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
Core Operations
The major operations on a singly linked list are:
Traversal: Visit every node from the head to the tail.
Search: Find the first node containing a value.
Access: Retrieve the value at an index.
Insertion: Add a node at the beginning, after a known node, or at the end.
Deletion: Remove the head, a known node’s successor, or the first node containing a value.
Peek: Read the first value without removing it.
Reversal: Reverse the direction of every next reference.
Concatenation: Append all nodes from another list.
Copying: Create a new list containing the same values.
A linked list preserves insertion order and is classified as a linear, dynamic, non-primitive, and linked-memory structure.
Complexity by Operation
For a deterministic linked list, average and worst-case costs are normally identical.
Operation
Average
Worst
Reason
Peek head
O(1)
O(1)
The head reference is immediately available
Access by index k
O(k)
O(n)
Nodes must be followed sequentially
Search by value
O(n)
O(n)
Each node may need inspection
Traversal
O(n)
O(n)
Every node is visited once
Insert at head
O(1)
O(1)
Only the head reference changes
Insert after a known node
O(1)
O(1)
A constant number of links are changed
Append with a maintained tail
O(1)
O(1)
The tail reference is immediately available
Append without a tail reference
O(n)
O(n)
The final node must first be found
Delete head
O(1)
O(1)
The head is advanced to its successor
Delete after a known node
O(1)
O(1)
The surrounding links are updated directly
Delete by value
O(n)
O(n)
The value and its predecessor must be found
Reverse iteratively
O(n)
O(n)
Every node is visited and relinked once
Concatenate with maintained tails
O(1)
O(1)
The second list’s head is linked to the first tail
Concatenate without tail tracking
O(n)
O(n)
The first list’s tail must be located
For a doubly linked list, deleting a node directly is O(1) when that node reference is available because both prev and next can be updated. Searching and accessing by index remain O(n).
Python 3.12 Mapping
Python has no standard general-purpose linked-list class:
list is a dynamic contiguous array, not a linked list; inserting into its middle is O(n).
collections.deque provides efficient operations at both ends and is the normal choice for queues and stacks.
A custom linked list can be implemented with a node class containing a value and a next reference.
The implementation below is iterative, so it uses no recursion stack.
Memory and Real-World Use
Total space: O(n) when the stored nodes are counted.
Auxiliary space per operation: O(1), excluding the stored input and any returned collection.
Per-node overhead: O(1) for the value and one or more reference fields.
Memory locality: Unlike an array, linked nodes may be scattered throughout memory, reducing cache efficiency.
Typical use: An LRU cache combines a hash map with a doubly linked list. The hash map finds a cache entry in O(1) average time, while the linked list moves it to the front in O(1).
Interview Tips
Distinguish knowing a node reference from knowing only an index; locating the node by index is still O(n).
In a singly linked list, deleting a node generally requires access to its predecessor unless the node is the head.
Appending is O(n) in a basic implementation that does not maintain a tail reference.
Handle empty lists, duplicate values, removal of a missing value, and the transition from one element to an empty list.
Python’s list is contiguous, whereas a linked list uses non-contiguous nodes with additional reference overhead.
Common follow-up questions ask about implementing a doubly linked list, using one in an LRU cache, making it thread-safe, or handling concurrent modifications.
14
How do you reverse a linked list in place?
KnownRevise
An in-place reversal rewires the list’s next pointers so it runs in the opposite direction without creating new nodes. For a singly linked list, three pointers—previous, current, and next_node—are sufficient, and the former tail becomes the new head.
Read full answerHide full answer
Applies toPython 3.12
How It Works
A singly linked list is a linear, linked-memory structure. Each node stores a value and a pointer to another node, adding pointer overhead and generally providing worse cache locality than a contiguous list such as list.
Before reversing, the algorithm checks that the list is acyclic. It then processes each node:
Save the current node’s original successor in next_node.
Point the current node backward to previous.
Move previous to the current node.
Continue with next_node.
Return previous, which is the new head.
The existing nodes are reused; only their links change. This pattern is useful when a sequence represented by nodes must be traversed backward without copying its values.
Example Walkthrough
For 1 -> 2 -> 3 -> None:
Step
previous
current
Link after rewiring
Initial
None
1
1 -> 2
Process 1
1
2
1 -> None
Process 2
2
3
2 -> 1
Process 3
3
None
3 -> 2
The returned head is 3, so the result is 3 -> 2 -> 1 -> None.
Complexity
Time:O(n) for best, average, and worst cases. The cycle check and reversal each visit at most every node.
Auxiliary space:O(1), excluding the input. Only a fixed number of pointers are used, and the iterative implementation has an O(0) recursion stack.
Total space including input:O(n), because the original n nodes are retained and reused.
Code Example
from __future__ import annotations
from dataclasses import dataclass
@dataclass(slots=True)
class Node:
value: int
next: Node | None = None
def _contains_cycle(head: Node | None) -> bool:
"""Return whether the singly linked list contains a cycle."""
slow = head
fast = head
while fast is not None and fast.next is not None:
slow = slow.next
fast = fast.next.next
if slow is not None and slow is fast:
return True
return False
def reverse_linked_list(head: Node | None) -> Node | None:
"""Reverse an acyclic singly linked list in place and return its new head."""
if _contains_cycle(head):
raise ValueError("Cannot reverse a linked list containing a cycle")
previous: Node | None = None
current: Node | None = head
while current is not None:
next_node = current.next
current.next = previous
previous = current
current = next_node
return previous
head = Node(1, Node(2, Node(3)))
reversed_head = reverse_linked_list(head)
values: list[int] = []
current = reversed_head
while current is not None:
values.append(current.value)
current = current.next
print(values) # Expected output: [3, 2, 1]
Common Pitfalls
Losing the remainder of the list: Save current.next before replacing it with previous.
Returning the wrong node: The new head is previous, not the original head.
Ignoring cycles: A normal singly linked-list reversal assumes an acyclic structure; this implementation detects and rejects cycles.
Unexpected edge cases: An empty list returns None, while a one-element list returns the same node with next set to None. Duplicate and negative values require no special handling, and the original value order does not affect the algorithm.
Recursion depth: A recursive solution can exceed Python’s default recursion limit for long lists. The iterative implementation avoids that problem and remains O(1) auxiliary space.
15
How to find a linked list’s middle element in one pass
KnownRevise
Use a two-pointer traversal: a slow pointer advances one node at a time while a fast pointer advances two nodes. When the fast pointer reaches the end, the slow pointer points to the middle element.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
How It Works
Initialize slow and fast at the head of the list.
Move slow forward one node and fast forward two nodes per iteration.
Continue while fast and fast.next exist.
Return slow, which is the middle node.
For an even-length list, this algorithm returns the second middle element. For example, in a four-node list, it returns node 3 rather than node 2.
Approach
Time
Extra Space
Trade-off
Count length, then traverse again
O(n)
O(1)
Requires two traversal phases
Slow and fast pointers
O(n)
O(1)
Meets the one-pass requirement
Edge Cases
Empty list: Returns None.
One element: Returns that element’s value.
Duplicate or negative values: Treated normally; values do not affect traversal.
Sorted or unsorted list: Ordering is irrelevant because the algorithm uses links rather than comparisons.
Cycle: Not supported; the pointers could loop indefinitely. A finite, acyclic linked list is assumed.
Code Example
from __future__ import annotations
from dataclasses import dataclass
@dataclass(slots=True)
class Node:
value: int
next: Node | None = None
def find_middle_value(head: Node | None) -> int | None:
"""Return the middle value, choosing the second middle for even lengths."""
slow = head
fast = head
while fast is not None and fast.next is not None:
if slow is None:
return None
slow = slow.next
fast = fast.next.next
return slow.value if slow is not None else None
head = Node(1, Node(2, Node(3, Node(4, Node(5)))))
result = find_middle_value(head)
print(result) # Expected output: 3
Example Walkthrough
For 1 -> 2 -> 3 -> 4 -> 5:
Iteration
slow
fast
Initial
1
1
1
2
3
2
3
5
Stop
3
5
Because fast.next is None, the loop stops and slow identifies the middle value, 3.
Complexity
Best time: O(n), since the slow pointer may still need to traverse half the list.
Average time: O(n).
Worst time: O(n), because the fast pointer traverses approximately half the list.
Auxiliary space: O(1). Only two pointers and a few references are used. The input list is not counted, and there is no recursion stack.
Common Pitfalls
Using fast is not None alone instead of checking both fast and fast.next can cause an AttributeError.
Moving both pointers at the same speed leaves slow at the list’s end.
Clarifying which middle is expected for an even-length list avoids ambiguity.
Remember that Python has no built-in linked-list type; a class such as Node is needed.
An interviewer may follow up by asking how to detect a cycle or return the middle node instead of its value.
16
What are the trade-offs of linked lists compared with arrays?
KnownRevise
A linked list stores elements in non-contiguous nodes connected by references, while an array stores elements directly in contiguous memory. Linked lists excel when elements are frequently inserted or removed at known positions, but arrays are usually faster and more memory-efficient for traversal and indexed access.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
Core Difference
Both are linear data structures, but their memory layouts differ:
Array: Elements occupy adjacent memory locations. A fixed-size array has a predetermined capacity; a dynamic array, such as Python’s list, grows as needed.
Linked list: Each node contains an element and one or more references to other nodes. Nodes are usually allocated separately and may be scattered through memory.
ADT versus implementation: A list is an abstract sequence of ordered elements; an array and a linked list are two implementations of that abstraction.
Python mapping: list is a dynamic-array implementation. Python has no built-in general-purpose linked list; collections.deque provides optimized double-ended queue operations but does not expose individual nodes.
Operation Costs
The comparison below uses a singly linked list and a dynamic array such as Python’s list.
Operation
Linked list: average
Linked list: worst
Dynamic array: average
Dynamic array: worst
Access by index
O(n)
O(n)
O(1)
O(1)
Search for unsorted value
O(n)
O(n)
O(n)
O(n)
Insert at known node/index
O(1)
O(n), including location search
O(n) when shifting elements
O(n)
Insert at beginning
O(1)
O(1)
O(n)
O(n)
Append at end
O(1) with a tail reference
O(n) without one
O(1) amortized
O(n) during resizing
Delete known node
O(1) if its predecessor is known
O(n) if the node must first be found
O(n) when shifting elements
O(n)
Delete from beginning
O(1)
O(1)
O(n)
O(n)
The insertion and deletion advantage applies only after the correct position is known. Finding that position still requires O(n) traversal unless another index or reference identifies it.
Advantages of Linked Lists
Efficient splicing: Inserting or deleting after a known node requires O(1) time and does not require shifting the remaining elements.
No contiguous allocation requirement: Nodes can be allocated independently, avoiding the need for one large contiguous memory block.
Stable node references: A reference to a node remains valid when other elements are inserted or removed, provided the node itself is not deleted.
Flexible growth: Nodes can be added without resizing an underlying array and copying its elements.
Useful structures: Linked lists commonly support LRU caches when combined with a hash table for O(1) lookup and node relocation.
Disadvantages and Trade-offs
No random access: Reaching the ith element requires O(n) traversal, whereas an array provides O(1) indexed access.
Higher memory overhead: Each node stores an element plus one or more references. A linked list therefore uses more total memory than an array of the same length.
Poor cache locality: Nodes are commonly scattered in memory, so traversal performs more pointer chasing and cache misses. Contiguous arrays are generally faster to scan.
Allocation overhead: Creating each node may require a separate allocation and eventual cleanup.
Higher constant factors: Language implementations often perform array operations in optimized native code, so a linked list may not outperform list even for many insertions.
Complex implementation: Maintaining head, tail, size, and predecessor information introduces additional logic and potential bugs.
Space Complexity
Array: O(n) total storage for n elements, including the input itself.
Linked list: O(n) total storage for n elements, including the input itself, but with a larger per-element constant.
Auxiliary space: O(1) for a basic traversal in either structure. No recursion stack is required.
Choosing One
Use a linked list when stable node references and O(1) insertion or deletion at known positions are more important than indexed access and compact storage. Choose an array for lookups, sorting, numerical processing, or memory-efficient sequential traversal.
In Python, list is normally the safer choice because of its cache-friendly storage and fast native implementation. A linked list is more compelling in specialized systems, such as an LRU cache that must relocate entries in O(1) time.
Common Pitfalls
Do not claim that linked-list insertion is always O(1); locating the insertion point can still take O(n).
Do not ignore the extra pointer or reference stored by every node.
Remember that arrays preserve order too; linked lists do not automatically provide sorting or faster searching.
Avoid using a Python list as a queue with pop(0), because removing the first element shifts the remaining elements and costs O(n); use collections.deque.popleft().
A frequent interview follow-up is how the design would change if operations must be thread-safe or the data must exceed available memory.
17
How do you implement a FIFO queue with a linked list?
KnownRevise
A linked-list queue implements the queue abstract data type using FIFO ordering: elements are added at the tail and removed from the head. Maintaining references to both ends allows enqueue and dequeue operations to run in constant time.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
Design
The queue uses a singly linked list with two pointers:
Head: Points to the oldest element and is used by dequeue and peek.
Tail: Points to the newest element and is used by enqueue.
Size: Tracks the number of elements, allowing is_empty and __len__ to run in O(1).
Python has no built-in linked-list structure, so the nodes must be implemented explicitly.
How It Works
To enqueue an element, create a node and append it after the tail.
To dequeue an element, remove the head node and return its value.
If the last node is removed, reset both head and tail to None.
Peeking returns the head’s value without modifying the list.
A small walkthrough:
Operation
Head
Tail
Queue
Initial
None
None
Empty
Enqueue A
A
A
A
Enqueue B
A
B
A, B
Dequeue
B
B
B
Dequeue
None
None
Empty
Complexity and Alternatives
Structure
Access
Search
Insert (enqueue) average/worst
Delete (dequeue) average/worst
Extra memory per element
Order
Linked-list queue
Front/peek O(1); arbitrary access unsupported, or O(n) to reach a node
O(n) by traversal
O(1) / O(1)
O(1) / O(1)
O(1): one node and one next link
FIFO
collections.deque
O(1) at either end; O(n) for arbitrary indexing
O(n) by traversal
O(1) / O(1)
O(1) / O(1)
O(1) asymptotic overhead
FIFO
Enqueue and dequeue are O(1) because each requires only a constant number of pointer updates. Peek is O(1) because it reads the head directly, while finding an arbitrary value requires O(n) traversal.
For n queued elements, total queue storage is O(n): the implementation owns one node and one next reference per element and retains references to the values. The external value objects are not counted separately, and each operation requires O(1) temporary workspace. No recursion is used, so the recursion stack is O(1).
Linked structures avoid shifting elements but add node and pointer overhead and generally have less predictable memory locality. In most Python applications, collections.deque is preferable unless linked nodes are already part of the design.
Code Example
from __future__ import annotations
from dataclasses import dataclass
from typing import Generic, TypeVar
Value = TypeVar("Value")
@dataclass(slots=True)
class _Node(Generic[Value]):
value: Value
next: _Node[Value] | None = None
class LinkedQueue(Generic[Value]):
"""Implement a FIFO queue using a singly linked list."""
def __init__(self) -> None:
self._head: _Node[Value] | None = None
self._tail: _Node[Value] | None = None
self._size = 0
def enqueue(self, value: Value) -> None:
"""Add a value to the back of the queue."""
new_node = _Node(value)
if self._tail is None:
self._head = self._tail = new_node
else:
self._tail.next = new_node
self._tail = new_node
self._size += 1
def dequeue(self) -> Value:
"""Remove and return the value at the front of the queue."""
if self._head is None:
raise IndexError("dequeue from an empty queue")
removed_node = self._head
self._head = removed_node.next
self._size -= 1
if self._head is None:
self._tail = None
return removed_node.value
def peek(self) -> Value:
"""Return the front value without removing it."""
if self._head is None:
raise IndexError("peek from an empty queue")
return self._head.value
def is_empty(self) -> bool:
"""Return whether the queue contains no elements."""
return self._size == 0
def __len__(self) -> int:
return self._size
def run_example() -> None:
"""Demonstrate FIFO order and queue state."""
queue = LinkedQueue[str]()
queue.enqueue("A")
queue.enqueue("B")
first = queue.dequeue()
print(first, queue.peek(), len(queue), queue.is_empty())
run_example() # A B 1 False
Edge Cases and Use Cases
Empty queue: dequeue and peek raise IndexError rather than returning an invalid value.
One element: Removing it resets both head and tail, leaving a consistent empty queue.
Duplicates and negative values: These are stored normally because queue order, rather than value validity, determines behavior.
Already ordered values: Ordering is irrelevant because the queue always preserves insertion order.
Real-world use: A linked-list queue can support FIFO task scheduling, buffering, or request processing when nodes are already used by the surrounding system.
Common Pitfalls
Updating only the head and forgetting to advance the tail corrupts the list.
Failing to clear the tail after removing the final node can leave a dangling reference.
Using list.pop(0) for a queue causes an O(n) shift on every dequeue.
Confusing FIFO queue behavior with stack behavior; a stack removes the newest element first.
Interviewers may ask how the implementation would support priorities, thread safety, iteration, or cleanup of detached nodes.
18
How does a linked list implement a stack?
KnownRevise
A stack (LIFO) can be implemented with a singly linked list by treating the list head as the top of the stack. Push inserts a new node at the head, and pop unlinks the head node, so each pointer update is O(1); a tail pointer is unnecessary.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
How It Works
Abstract data type: a stack defines LIFO operations—push, pop, and peek—without specifying its internal representation.
Classification: the linked-list implementation is linear, dynamically sized, non-primitive, and linked-memory. A Python list implementation is contiguous-memory.
Node structure: each node stores a value and a next reference to the following node.
Stack pointer: the stack maintains a head reference pointing to the top node, or None when empty.
Push: create a node, set its next to the current head, and update head.
Pop: save the current value, advance head to head.next, and return the saved value.
Peek: return head.value without modifying the list.
Python availability: Python 3.12 has no built-in linked-list structure; the class below provides one. A Python list is the usual built-in stack candidate.
Edge cases: one element, duplicate values, and negative values are all valid. Only pop and peek on an empty stack are errors.
Operations and Complexity
Operation or cost
Linked-list stack
Python list stack
Random access
O(1) for the head; O(n) average/worst for other positions
O(1) best/average/worst
Search by value
O(1) best if the top matches; O(n) average/worst
O(1) best if the first item matches; O(n) average/worst
Push/top insert
O(1) best/average/worst*
O(1) amortized average; O(n) worst during resize
Pop/top delete
O(1) best/average/worst*
O(1) amortized average; O(n) worst during shrinking or reallocation
Peek
O(1) in all cases
O(1) in all cases
Empty check
O(1) in all cases
O(1) in all cases
Length
O(1) when a counter is maintained
O(1) with the built-in len
Extra memory per element
One node object and one next reference beyond the value
One contiguous slot plus occasional spare capacity beyond the value
Order
LIFO order preserved
LIFO order preserved
*Under the usual algorithmic model in which allocation and deallocation take O(1) time; the pointer rewiring itself is always O(1).
For n stored values, total space is O(n), counting the stack’s node objects and reference fields. No recursion stack is used. The caller-provided input is not counted separately; counting the input container adds O(n).
Code Example
from __future__ import annotations
from dataclasses import dataclass
from typing import Generic, Optional, TypeVar
T = TypeVar("T")
@dataclass(slots=True)
class Node(Generic[T]):
value: T
next: Optional[Node[T]] = None
class LinkedListStack(Generic[T]):
"""Stack implemented with a singly linked list whose head is the top."""
def __init__(self) -> None:
self._head: Optional[Node[T]] = None
self._size: int = 0
def push(self, value: T) -> None:
"""Insert value at the top of the stack."""
self._head = Node(value, self._head)
self._size += 1
def pop(self) -> T:
"""Remove and return the top value, raising if the stack is empty."""
if self._head is None:
raise IndexError("pop from empty stack")
value = self._head.value
self._head = self._head.next
self._size -= 1
return value
def peek(self) -> T:
"""Return the top value without removing it."""
if self._head is None:
raise IndexError("peek from empty stack")
return self._head.value
def is_empty(self) -> bool:
"""Return whether the stack contains no elements."""
return self._head is None
def __len__(self) -> int:
"""Return the number of stored elements."""
return self._size
def __repr__(self) -> str:
"""Return a debug representation with the top element first."""
values: list[str] = []
current = self._head
while current is not None:
values.append(repr(current.value))
current = current.next
return f"LinkedListStack([{', '.join(values)}])"
if __name__ == "__main__":
stack = LinkedListStack[int]()
stack.push(10)
stack.push(20)
stack.push(30)
print(stack) # LinkedListStack([30, 20, 10])
print(stack.peek()) # 30
print(stack.pop()) # 30
print(len(stack)) # 2
Advantages and Limitations
Advantages over a dynamic array:
Top insertion and removal are O(1) under the standard allocation model, without array resizing.
No spare array capacity is reserved for growth.
A persistent variant can share tail nodes; the mutable class shown does not provide persistence.
Disadvantages:
Each element requires node-object and next-reference overhead. slots=True avoids a per-instance __dict__, but allocator overhead still remains.
Nodes can be scattered in memory, producing poorer cache locality than a contiguous array.
Random access and iteration require pointer chasing.
Real-world uses: linked-list stacks can support undo histories and some interpreter or runtime designs; Python-list stacks are common in depth-first traversal, expression parsing, and task reversal.
Common Pitfalls
Forgetting the empty check: attempting to read head.value when head is None raises AttributeError.
Losing the remaining chain: when removing a node, advance head to the current node’s next before releasing that node; head = head.next is sufficient.
Ignoring manual memory management: in languages without garbage collection, explicitly free the detached node.
Interview follow-up: “How would you make it thread-safe?” Use a lock around mutations or use a carefully designed lock-free algorithm with safe memory reclamation. For O(1) length, maintain a size counter as shown.
19
How do you delete a linked-list node without its head pointer?
KnownRevise
Successor copying removes a node when you have direct access to it: copy the next node’s value into the target, then unlink the next node. This works for any node except the tail of a singly linked list.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
How It Works
Given the target node A and its successor B:
Copy B’s value into A.
Point A.next to B.next.
Discard B.
The linked list now contains the same logical sequence as if A had originally held B’s value. The algorithm requires neither the head pointer nor a predecessor.
This technique deletes the logical value at the target position. It does not physically remove the target object. If the exact node object must be removed rather than its value, a predecessor is required.
Code Example
from __future__ import annotations
from dataclasses import dataclass
@dataclass
class Node:
value: int
next: Node | None = None
def delete_node_without_head(node: Node | None) -> None:
"""Delete the logical value at node by copying from its successor."""
if node is None:
raise ValueError("Cannot delete a null node")
successor = node.next
if successor is None:
raise ValueError("The tail node cannot be deleted without a predecessor")
node.value = successor.value
node.next = successor.next
head = Node(1)
target = Node(2)
tail = Node(3)
head.next = target
target.next = tail
delete_node_without_head(target)
values: list[int] = []
current: Node | None = head
while current is not None:
values.append(current.value)
current = current.next
print(values) # Expected output: [1, 3]
Complexity and Alternatives
Approach
Best time
Average time
Worst time
Auxiliary space
Successor copying
O(1)
O(1)
O(1)
O(1)
Search for predecessor, then unlink
O(n)
O(n)
O(n)
O(1)
The constant-time approach performs a fixed number of assignments and pointer changes. Auxiliary space is O(1), excluding the input list; there is no recursion stack.
Constraints and Edge Cases
Null node: The code raises ValueError because there is nothing to delete.
One-element list: Its only node is the tail and cannot be deleted with this technique.
Tail node: Deletion requires access to its predecessor or the head pointer.
Duplicate or negative values: The algorithm is independent of value type; duplicates and negatives are handled identically.
Head node: Copying its successor preserves the logical list, although the original head object remains in place.
Exact object removal: If removing the target object itself is mandatory, use predecessor search and perform a direct unlink.
Common Pitfalls
Assuming the tail has a successor; always check node.next.
Confusing logical deletion with removal of the exact node object.
Forgetting that the operation mutates the target node and linked list.
Using recursion unnecessarily; no recursion or visited set is needed.
Python integers do not overflow, though fixed-width integers in other languages may require careful sizing.
A common interview follow-up asks how you would delete the tail or an exact node when only the target reference is available; the answer is to obtain its predecessor or the head pointer.
20
What is a sentinel node in a linked list?
KnownRevise
A sentinel node is a reserved dummy node used to simplify pointer operations in a linked list. It does not store meaningful user data and eliminates special cases for the first or last element.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
How It Works
Without sentinels, linked-list code must check whether a pointer is None:
Initializing the first node requires a special case.
Inserting before the head requires updating the head.
Removing the last node may require updating the tail.
Traversing the list requires checking the current node before accessing its data or next pointer.
With a head sentinel, the sentinel always remains the first node, and head.next points to the first meaningful node. With a tail sentinel, tail.prev points to the last meaningful node in a doubly linked list.
For example, an empty doubly linked list with head and tail sentinels has this structure:
HEAD <-> TAIL
After inserting one element:
HEAD <-> 10 <-> TAIL
The sentinels remain in place regardless of whether the list is empty or contains many elements.
Common Variants
Head sentinel: Simplifies insertion and deletion at the beginning.
Tail sentinel: Simplifies insertion and deletion at the end, especially in a doubly linked list.
Circular linked list: The last meaningful node points back to the head sentinel. This can support round-robin iteration.
Dummy payload: Some implementations give the sentinel a placeholder value such as 0 or None; the important property is that callers treat it as reserved rather than user data.
Python’s built-in list is a dynamic array, not a linked list. Python does not provide a standard linked-list implementation, so applications typically implement one using classes or store links in parallel collections.
Complexity
Using a sentinel does not improve the asymptotic complexity of linked-list operations. It mainly removes conditional logic and reduces implementation errors.
Operation
Sentinel linked list
Linked list without sentinel
Access by index
O(n) average and worst
O(n) average and worst
Search by value
O(n) average and worst
O(n) average and worst
Insert or delete at a known node
O(1) average and worst
O(1) average and worst
Insert or delete at a known position
O(n) average and worst
O(n) average and worst
Extra memory
O(1) per sentinel; O(n) total
O(n) total
Order preserved
Yes
Yes
Best case: Accessing an already available first node or inserting into an empty list can take O(1).
Average case: Finding an arbitrary node requires O(n) traversal.
Worst case: The target may be the final node, so traversal still costs O(n).
Space: The list uses O(n) total space, including O(1) space for each meaningful node and its pointers. Whether the input itself is counted does not change the additional sentinel overhead.
Recursion stack: A recursive traversal can use O(n) stack space; an iterative traversal avoids that stack cost.
Common Pitfalls
Treating the sentinel as real data: Traversal loops must stop at the designated sentinel rather than accidentally processing it.
Breaking the invariant: Insertions and deletions must preserve links such as head.next and tail.prev.
Assuming sentinels improve Big-O performance: They improve code simplicity, not traversal or search complexity.
Forgetting edge cases: An empty list should connect its sentinels correctly, while a one-element list should have both sentinels on either side of that element. Duplicates and negative values do not affect sentinel behavior.
Choosing the wrong node overhead: Each meaningful linked-list node already stores pointer overhead; adding one or two sentinels increases total memory only by O(1).
Interview Tips
Explain that a sentinel is a dummy boundary node, not the first or last element containing user data. A common follow-up asks whether sentinels are still necessary in a circular implementation: they often are, because they provide a stable starting point and simplify boundary conditions.
21
Which operations does a stack data structure support?
KnownRevise
A stack is a linear, non-primitive, last-in-first-out (LIFO) abstract data type. It primarily supports pushing an element onto the top, popping the top element, and inspecting the top without removing it.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
Core Operations
Operation
Description
List-backed implementation
Linked-list-backed implementation
push(value)
Adds an element to the top
O(1) amortized; O(n) worst case for an individual resize
O(1) worst case
pop()
Removes and returns the top element
O(1) amortized; O(n) worst case for an individual resize
O(1) worst case
peek() / top()
Returns the top element without removing it
O(1)
O(1)
is_empty()
Checks whether the stack has no elements
O(1)
O(1)
size()
Returns the number of stored elements
O(1)
O(1) when a count is maintained
Search by value
Searches all elements; not a core stack operation
O(n) average and worst case
O(n) average and worst case
Random access
Accesses an element by position; not a core stack operation
O(1)
O(n)
Operation names vary by implementation, but the defining behavior is LIFO. A fixed-capacity stack can overflow when full, while a dynamically sized stack grows as needed.
How It Works
Elements are ordered by insertion time, so the most recently inserted element is removed first.
A stack is an abstract data type: it defines the supported operations rather than one mandatory memory layout.
Python’s list is a dynamic, contiguous sequence commonly used as a stack. Its append() and end-position pop() are O(1) amortized.
A linked-list-backed stack uses linked nodes and provides O(1) worst-case insertion and deletion at a known top node, but adds pointer overhead and generally has poorer cache locality.
A common real-world use is an undo history, where the latest action must be reversed first.
For (n) elements, stack storage requires O(n) space. Under the usual auxiliary-space convention, the input values are not counted, the O(n) storage occupied by the stack is reported separately, and additional working space is O(1). No recursion stack is involved.
Python 3.12 Usage
In Python 3.12, list.append(value) performs a push, list.pop() performs a pop, list[-1] inspects the top, len(stack) returns its size, and not stack checks whether it is empty. Calling pop() or indexing stack[-1] on an empty stack raises IndexError.
A one-element stack, duplicate values, and negative values are all valid when the element type permits them. Values are stored by reference in a Python list, so the list itself stores references rather than copying the referenced objects.
Common Pitfalls
Calling pop() or inspecting the top of an empty stack requires explicit handling.
Do not use list.pop(0) to simulate a queue; removing the first element requires O(n) time because the remaining elements shift.
Do not confuse a stack’s LIFO behavior with a queue’s FIFO behavior or use a list as a queue without accounting for that cost.
Do not choose a structure by habit; select it based on whether the newest or oldest element must be processed first.
Consider worst-case costs and memory overhead: list resizing can occasionally cost O(n), while linked implementations add node and pointer overhead per element.
A common interview follow-up asks how the stack would behave under concurrent access, where locking or another synchronization mechanism may be required.
22
How do you evaluate an arithmetic expression using stacks?
KnownRevise
An arithmetic expression can be evaluated with two stacks: one stores operands and the other stores operators. Operators are applied according to precedence and associativity, producing the expression’s final numeric value.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
How Two-Stack Evaluation Works
A stack is a linear, last-in-first-out abstract data type. In Python, a list can implement it using append() and pop() from the end.
The algorithm supports:
Binary operators: +, -, *, and /
Parentheses
Integer and decimal operands
Unary + and unary -
The evaluation follows these rules:
Push numbers onto the value stack.
Push operators onto the operator stack.
Before pushing an operator, apply stacked operators with higher precedence, or with equal precedence if the incoming operator is left-associative.
On ), apply operators until the matching ( is reached, then discard it.
At the end, apply all remaining operators.
The value stack should contain exactly one result.
Precedence and associativity:
Operators
Precedence
Associativity
+, -
1
Left
*, /
2
Left
Unary +, unary -
3
Right
Example Walkthrough
For 3 + 4 * 2:
Step
Token
Value stack
Operator stack
1
3
[3]
[]
2
+
[3]
[+]
3
4
[3, 4]
[+]
4
*
[3, 4]
[+, *]
5
2
[3, 4, 2]
[+, *]
6
End
[3, 8]
[+]
7
Apply +
[11]
[]
The multiplication is applied before addition because * has higher precedence.
Complexity
For an expression of length n, assuming each numeric operation takes constant time:
Best case:O(n) because the expression must be scanned.
Average case:O(n) because every token is processed and each operator is pushed and popped at most once.
Worst case:O(n) under the same assumptions.
Auxiliary space:O(n) for the value stack, operator stack, and token list. The input string itself is not counted, and no recursion stack is used.
Python list append() and pop() are O(1) amortized, so the complete evaluation remains O(n) even when individual resizing costs are included.
Code Example
import re
def evaluate_arithmetic(expression: str) -> float:
"""Evaluate an infix arithmetic expression using two stacks."""
token_pattern = re.compile(
r"[0-9]+(?:\.[0-9]*)?|\.[0-9]+|[()+\-*/]"
)
tokens = token_pattern.findall(expression)
# Reject characters that are not supported by the expression grammar.
if "".join(tokens) != re.sub(r"\s+", "", expression):
raise ValueError("Expression contains an invalid character.")
if not tokens:
raise ValueError("Expression must not be empty.")
values: list[float] = []
operators: list[str] = []
precedence = {
"+": 1,
"-": 1,
"*": 2,
"/": 2,
"u+": 3,
"u-": 3,
}
associativity = {
"+": "left",
"-": "left",
"*": "left",
"/": "left",
"u+": "right",
"u-": "right",
}
expect_operand = True
def pop_value() -> float:
if not values:
raise ValueError("Expression is missing an operand.")
return values.pop()
def apply_operator() -> None:
if not operators:
raise ValueError("Expression is missing an operator.")
operator = operators.pop()
if operator in ("u+", "u-"):
operand = pop_value()
values.append(-operand if operator == "u-" else operand)
return
right = pop_value()
left = pop_value()
if operator == "+":
result = left + right
elif operator == "-":
result = left - right
elif operator == "*":
result = left * right
elif operator == "/":
result = left / right
else:
raise ValueError(f"Unsupported operator: {operator}")
values.append(result)
for token in tokens:
if token[0].isdigit() or token[0] == ".":
if not expect_operand:
raise ValueError("Two operands appear without an operator.")
values.append(float(token))
expect_operand = False
elif token == "(":
if not expect_operand:
raise ValueError("An operand cannot precede '('.")
operators.append(token)
expect_operand = True
elif token == ")":
while operators and operators[-1] != "(":
apply_operator()
if not operators:
raise ValueError("Unmatched closing parenthesis.")
operators.pop()
expect_operand = False
else:
is_unary = expect_operand and token in ("+", "-")
if expect_operand and not is_unary:
raise ValueError("Expected an operand.")
operator = f"u{token}" if is_unary else token
while (
operators
and operators[-1] != "("
and (
precedence[operators[-1]] > precedence[operator]
or (
precedence[operators[-1]] == precedence[operator]
and associativity[operator] == "left"
)
)
):
apply_operator()
operators.append(operator)
expect_operand = True
if "(" in operators:
raise ValueError("Unmatched opening parenthesis.")
if expect_operand:
raise ValueError("Expression must end with an operand.")
while operators:
apply_operator()
if len(values) != 1:
raise ValueError("Expression is malformed.")
return values[0]
print(evaluate_arithmetic("3 + 4 * 2")) # 11.0
print(evaluate_arithmetic("--5")) # 5.0
print(evaluate_arithmetic("-(3 + 4)")) # -7.0
Common Pitfalls
Incorrect precedence/associativity: Apply higher-precedence operators first. For left-associative operators use >= (or > plus equality check); for right-associative operators (unary +/-) only pop when the stacked operator has strictly higher precedence.
Unmatched parentheses: Check for both missing opening and missing closing parentheses.
Unary versus binary minus: Determine whether - expects an operand (unary) or follows another operand (binary).
Invalid expressions: Handle empty input, a single number, repeated operators, adjacent operands, and division by zero.
Queue misuse: A stack is implemented with end-of-list operations; do not use pop(0), which is O(n).
Numeric behavior: This implementation returns float to support decimals. An integer-only version must define integer-division and overflow behavior separately.
A common interview follow-up asks how to extend the algorithm to postfix notation, exponentiation, or function calls.
23
How do you implement a queue using two stacks?
KnownRevise
Queue FIFO behavior can be implemented using two stacks: one accepts new elements, while the other reverses them so that the oldest element is removed first.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
How It Works
A queue is a linear abstract data type supporting insertion at the rear and removal from the front. This implementation is dynamic and uses two Python lists as stacks:
incoming_stack: New elements are pushed here with append().
outgoing_stack: Elements are transferred here when the queue is dequeued or peeked.
Transfer: Moving everything from incoming_stack to outgoing_stack reverses its order.
Removal: Popping from outgoing_stack therefore returns the oldest element.
Each queued element moves from the incoming stack to the outgoing stack at most once. One operation can therefore be expensive, but the total transfer cost is bounded across a sequence of operations.
Complexity
Let n be the number of elements currently queued.
Operation
Best
Average
Amortized
Worst for one operation
Reason
Enqueue
O(1)
O(1)
O(1)
O(n)
list.append() is amortized O(1), but an occasional resize copies the list.
Dequeue
O(1)
O(1)*
O(1)
O(n)
A transfer may reverse all n incoming elements; *the average is O(1) in a workload where each enqueued element is dequeued once.
Peek at front
O(1)
O(1)*
O(1)
O(n)
It may trigger the same transfer as dequeue.
Search
O(1)
O(n)
O(n)
O(n)
Scanning can find an immediately available match, but otherwise examines O(n) elements.
Length
O(1)
O(1)
O(1)
O(1)
Both stack lengths are directly available.
Space: O(n), counting the queued element references and stack/list capacity.
Recursion stack: O(1), because the implementation is iterative.
If the element objects are treated as pre-existing input, the auxiliary storage for references and capacity is still O(n).
Walkthrough
The stacks below show elements from bottom to top:
Action
incoming_stack
outgoing_stack
Result
Enqueue 1
[1]
[]
—
Enqueue 2
[1, 2]
[]
—
Enqueue 3
[1, 2, 3]
[]
—
Dequeue
[]
[3, 2, 1]
Returns 1
Dequeue
[]
[3]
Returns 2
Enqueue 4
[4]
[3]
—
Dequeue
[4]
[]
Returns 3
Dequeue
[]
[4]
Returns 4
Comparison With collections.deque
Structure
Front access
Search
Rear insert
Front delete
Extra memory per element
Order
Two-stack queue
O(1) amortized; O(n) worst
O(n) average/worst; O(1) best
O(1) average/amortized; O(n) worst
O(1) average/amortized; O(n) worst
O(1) references plus list capacity
FIFO
collections.deque
O(1) average/worst
O(n) average/worst; O(1) best
O(1) average/worst
O(1) average/worst
O(1) references plus block and pointer overhead
FIFO
collections.deque is usually preferable in production because it implements queue operations directly. The two-stack version is useful for demonstrating amortized analysis and the stack-based inversion technique.
Code Example
from typing import Generic, TypeVar
T = TypeVar("T")
class TwoStackQueue(Generic[T]):
"""Implement a FIFO queue using two list-backed stacks."""
def __init__(self) -> None:
self._incoming_stack: list[T] = []
self._outgoing_stack: list[T] = []
def enqueue(self, value: T) -> None:
"""Add an element to the rear of the queue."""
self._incoming_stack.append(value)
def dequeue(self) -> T:
"""Remove and return the oldest element."""
if not self._outgoing_stack:
while self._incoming_stack:
self._outgoing_stack.append(self._incoming_stack.pop())
if not self._outgoing_stack:
raise IndexError("dequeue from an empty queue")
return self._outgoing_stack.pop()
def peek(self) -> T:
"""Return the oldest element without removing it."""
if not self._outgoing_stack:
while self._incoming_stack:
self._outgoing_stack.append(self._incoming_stack.pop())
if not self._outgoing_stack:
raise IndexError("peek from an empty queue")
return self._outgoing_stack[-1]
def is_empty(self) -> bool:
"""Return whether the queue contains no elements."""
return not self._incoming_stack and not self._outgoing_stack
def __len__(self) -> int:
"""Return the number of queued elements."""
return len(self._incoming_stack) + len(self._outgoing_stack)
queue = TwoStackQueue[int]()
queue.enqueue(10)
queue.enqueue(20)
queue.enqueue(30)
print(queue.dequeue()) # Expected output: 10
print(queue.peek()) # Expected output: 20
print(len(queue)) # Expected output: 2
Interview Tips
State explicitly that dequeue and peek are O(1) amortized and O(n) for an individual worst-case operation.
Use list.pop() only from the end; list.pop(0) would be O(n).
Empty queues must raise a clear exception rather than returning an invalid value.
Duplicates, negative numbers, and single-element queues work without special handling.
A common follow-up asks how the implementation would be made thread-safe; protect all public operations with the same lock.
24
How do you implement a stack using two FIFO queues?
KnownRevise
A stack is a LIFO abstraction, so pop() must return the most recently pushed element. With two strict FIFO queues, one queue can store the current elements from top to bottom while the other serves as a temporary queue; this construction makes push() O(n), while pop() and peek() are O(1).
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
Design
Maintain two queues:
Front queue: stores the stack elements from top to bottom, with the next element to pop at its front.
Buffer queue: temporarily receives existing elements during a push.
To push a value:
Append the new value to the buffer queue.
Move every element from the front queue to the rear of the buffer queue.
Swap the roles of the two queues.
Because the buffer starts with the new value, the resulting front queue contains the new value followed by the previous stack elements in top-to-bottom order.
A FIFO-to-FIFO transfer preserves relative order; it does not reverse it.
Example Walkthrough
The following sequences are shown from front to rear:
Operation
Front queue
Buffer queue
Result
push(1)
[1]
[]
Stack contains 1
push(2)
[2, 1]
[]
Stack contains 2, 1
push(3)
[3, 2, 1]
[]
Stack contains 3, 2, 1
pop()
[2, 1]
[]
Returns 3
The next two calls to pop() return 2 and 1.
Complexity
Let n be the number of elements currently stored.
Operation
Best
Average
Worst
push
O(1) when empty
O(n)
O(n)
pop
O(1)
O(1)
O(1)
peek
O(1)
O(1)
O(1)
Push: O(n) because the existing elements are transferred between the queues.
Pop and peek: O(1) because they access only the front of the front queue.
Space: O(n), counting the stored stack elements as input/state and including O(n) queue-reference overhead. Additional working state beyond the two queues is O(1), and there is no recursion stack.
Code Example
from collections import deque
from typing import Generic, TypeVar
T = TypeVar("T")
class StackUsingQueues(Generic[T]):
"""Implement a LIFO stack using two strict FIFO queues."""
def __init__(self) -> None:
self._front_queue: deque[T] = deque()
self._buffer_queue: deque[T] = deque()
def push(self, value: T) -> None:
# The new value must become the front of the next stack order.
self._buffer_queue.append(value)
# Preserve the existing top-to-bottom order while moving it.
while self._front_queue:
self._buffer_queue.append(self._front_queue.popleft())
self._front_queue, self._buffer_queue = (
self._buffer_queue,
self._front_queue,
)
def pop(self) -> T:
if not self._front_queue:
raise IndexError("pop from empty stack")
return self._front_queue.popleft()
def peek(self) -> T:
if not self._front_queue:
raise IndexError("peek from empty stack")
return self._front_queue[0]
stack = StackUsingQueues[int]()
stack.push(1)
stack.push(2)
stack.push(3)
print(stack.pop()) # 3
print(stack.pop()) # 2
print(stack.pop()) # 1
Common Pitfalls
FIFO ordering: Moving elements from the front of one FIFO queue to the rear of another preserves order rather than reversing it.
Empty stack: pop() and peek() must raise IndexError.
One element: A stack containing one value returns that value from both pop() and peek().
Duplicates and negative values: These are ordinary elements and need no special handling.
Irrelevant cases: Sorted input, disconnected graphs, and cycles do not apply to a stack implementation.
Interview follow-up: Interviewers may ask about thread safety, reducing the O(n) push cost, or using Python’s list as a direct stack.
25
How do stack overflow and stack underflow differ?
KnownRevise
A stack overflow occurs when available stack space is exhausted, while stack underflow occurs when the top of an empty stack is accessed or removed. They represent opposite boundary failures: overflow means there is no room for another element, whereas underflow means there is no element available to return.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
Key Distinction
A stack is a last in, first out (LIFO) data structure that supports pushing, popping, and usually peeking at its top element.
Failure
Stack state
Typical trigger
Meaning
Data-structure overflow
Full
push
A fixed-capacity stack cannot store another element.
Runtime stack overflow
Available call-stack space is exhausted
Excessive recursion or deeply nested calls
The runtime cannot create another stack frame.
Stack underflow
Empty
pop or peek
The operation attempts to access or remove a top element that does not exist.
The last valid element pushed is the first one removed because stacks follow the LIFO principle.
Data Structure Versus Runtime Stack
These terms describe related but distinct situations:
Data-structure overflow or underflow: A custom stack rejects a push when its capacity is exhausted or rejects a pop or peek when it is empty.
Runtime call-stack overflow: A program consumes the stack used for active function calls, local variables, parameters, and return information. Excessive recursion without a reachable base case is a common cause.
Python behavior: Python normally reports excessive recursion as RecursionError when its recursion limit is reached. This limit is a language-level safeguard and does not necessarily mean the native machine stack has been physically exhausted.
Integer overflow: This is separate from stack overflow. Integer overflow concerns a numeric value exceeding the range supported by its type.
A dynamic stack does not have a fixed logical capacity. For example, Python’s list grows as needed, although an allocation can still fail if the process runs out of memory.
Complexity
For a fixed-capacity, array-backed stack:
Push, pop, and peek: O(1) in the best, average, and worst cases because the top position is accessed directly.
The stored values are part of the stack’s data, and no recursion stack is used.
For a dynamic Python list-backed stack:
Operation
Best
Amortized average
Worst
Reason
Push
O(1)
O(1)
O(n)
An occasional resize copies all elements.
Pop
O(1)
O(1)
O(n)
Shrinking may require copying elements.
Peek
O(1)
O(1)
O(1)
The last element is accessed directly.
The dynamic stack uses O(n) total space for n stored elements, including any excess list capacity, and O(1) auxiliary space per operation. These operations do not use a recursion stack.
Common Pitfalls
Confusing stack overflow with integer overflow: one concerns stack storage, while the other concerns numeric range.
Checking only during pop: peek on an empty stack also needs an empty-state check.
Assuming every stack has a fixed capacity: Python’s list grows dynamically, unlike a fixed-capacity array implementation.
Forgetting that recursive calls consume runtime stack space until the calls return.
A common interview follow-up is: “How would you make a shared stack thread-safe?” Protect each operation, especially compound operations, with appropriate locking.
26
How does a priority queue work and how can you implement it?
KnownRevise
A priority queue is an abstract data type (ADT) whose next element is selected by priority rather than insertion order. A min-priority queue returns the smallest priority first, while a max-priority queue returns the largest priority first.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
Core Operations
A priority queue commonly supports:
Insert: Add an element with a given priority.
Extract-min: Remove and return the highest-priority element.
Peek: Inspect the highest-priority element without removing it.
Size or empty check: Determine how many elements remain.
The ADT specifies operations rather than a memory layout. Its common binary-heap implementation is nonlinear because it is tree-shaped, although sorted-array and tree-based implementations also exist. A real-world use is a task scheduler that repeatedly runs the task with the earliest deadline.
Implementation Choices
Implementation
Peek min
Access
Search
Insert
Delete min
Extra space per element
Order
When to use
Unsorted list and linear scan
O(n)
O(1)
O(n) average/worst
O(1) amortized
O(n)
O(1) slot
None
Tiny queues or simple prototypes
Binary heap
O(1)
O(1)
O(n) average/worst
O(1) expected for random priorities; O(log n) worst
O(log n) average/worst
O(1) slot
Partial priority order
General-purpose priority queues
Sorted array
O(1)
O(1)
O(log n) average/worst
O(n) average/worst
O(n) average/worst
O(1) slot
Fully sorted
Read-mostly workloads
Balanced ordered tree
O(log n)
O(log n)
O(log n) average/worst
O(log n) average/worst
O(log n) average/worst
O(1) words per node
Fully sorted
Ordered traversal or range queries
Use a binary heap when fast insertion and extraction matter most. It avoids keeping every element sorted, although it does not support efficient arbitrary deletion.
Binary Heap Mechanics
In an array-backed binary heap:
The parent of index i is (i - 1) // 2.
Its children are at 2 * i + 1 and 2 * i + 2.
Insertion appends the element and sifts it upward while it has higher priority than its parent.
Extraction replaces the root with the final element and sifts that element downward.
The heap property is partial: each parent has at least as high a priority as its children, but the collection is not globally sorted.
Building a heap by sifting down every internal node takes O(n), rather than O(n log n).
Python provides heapq, which stores a binary min-heap in a list, and queue.PriorityQueue, a thread-safe priority queue. For stable ordering of equal priorities, use (priority, sequence_number, value) tuples.
A small walkthrough using the code below:
Heapifying [5, 1, 3] produces [1, 3, 5].
Pushing 2 produces [1, 3, 5, 2]; sifting up swaps 2 with 3, giving [1, 2, 5, 3].
Popping returns 1; replacing the root and sifting down produces [2, 3, 5].
Complexity
Operation
Best
Average
Worst
Peek
O(1)
O(1)
O(1)
Insert
O(1)
O(1) expected for random priorities
O(log n)
Extract-min
O(1)
O(log n)
O(log n)
Search
O(1)
O(n)
O(n)
Bulk heap construction
O(n)
O(n)
O(n)
Total space including the stored elements is O(n). Auxiliary space per operation is O(1), and the caller’s input is not counted as auxiliary space. The implementation is iterative, so it uses no recursion stack. Python list append is O(1) amortized, but heap insertion can still take O(log n) in the worst case.
Code Example
from collections.abc import Iterable
from typing import TypeVar
T = TypeVar("T")
class MinPriorityQueue:
"""Implement a minimum-priority queue using an array-backed binary heap."""
def __init__(self, items: Iterable[T] | None = None) -> None:
self._heap: list[T] = list(items) if items is not None else []
# Build the min-heap in O(n).
for index in range(len(self._heap) // 2 - 1, -1, -1):
self._sift_down(index)
def __len__(self) -> int:
return len(self._heap)
def peek(self) -> T:
if not self._heap:
raise IndexError("peek from empty priority queue")
return self._heap[0]
def push(self, value: T) -> None:
self._heap.append(value)
self._sift_up(len(self._heap) - 1)
def pop(self) -> T:
if not self._heap:
raise IndexError("pop from empty priority queue")
minimum = self._heap[0]
final_element = self._heap.pop()
if self._heap:
self._heap[0] = final_element
self._sift_down(0)
return minimum
def _sift_up(self, index: int) -> None:
while index > 0:
parent = (index - 1) // 2
if not self._heap[index] < self._heap[parent]:
break
self._heap[index], self._heap[parent] = (
self._heap[parent],
self._heap[index],
)
index = parent
def _sift_down(self, index: int) -> None:
while True:
left = 2 * index + 1
right = left + 1
smallest = index
if left < len(self._heap) and self._heap[left] < self._heap[smallest]:
smallest = left
if right < len(self._heap) and self._heap[right] < self._heap[smallest]:
smallest = right
if smallest == index:
break
self._heap[index], self._heap[smallest] = (
self._heap[smallest],
self._heap[index],
)
index = smallest
def demo() -> None:
"""Show extraction in increasing priority order."""
queue = MinPriorityQueue([5, 1, 3])
queue.push(2)
ordered: list[int] = []
while queue:
ordered.append(queue.pop())
print(ordered) # [1, 2, 3, 5]
if __name__ == "__main__":
demo()
Stored values must support comparison with <. This implementation treats each value as its own priority; for a separate payload and stable ties, store (priority, sequence_number, payload) tuples.
Interview Tips
Check heap indexes carefully to avoid off-by-one errors. Empty heaps raise IndexError; one-element heaps, duplicates, negative priorities, and already sorted input work correctly.
Python integers do not overflow, but priority calculations can overflow in fixed-width languages.
The constructor copies its iterable, so it does not mutate the caller’s input.
A heap is neither a FIFO queue nor globally sorted; use collections.deque for FIFO behavior.
Remember that tree implementations add node and pointer overhead, while heaps use contiguous list storage.
Visited sets and cycle handling are not applicable to a priority queue itself, but matter when it drives graph traversal.
Common follow-ups ask about thread safety, removing arbitrary elements, ordered traversal, and behavior when the data does not fit in memory.
27
What is the difference between LIFO and FIFO?
KnownRevise
A LIFO data structure removes the most recently added element first, while a FIFO data structure removes the oldest element first. LIFO models a stack, and FIFO models a queue.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
Core Distinction
LIFO — Last In, First Out
The newest element is processed first.
Typical operations are push, pop, and peek.
Real-world examples include undo history, browser back navigation, function-call stacks, and expression evaluation.
In Python, a list is commonly used as a stack with append() and pop().
FIFO — First In, First Out
The oldest element is processed first.
Typical operations are enqueue, dequeue, and peek.
Real-world examples include task scheduling, printer queues, buffering, and breadth-first search.
In Python, collections.deque is the standard queue because deque.popleft() is efficient.
Here is a step-by-step example using A, B, and C:
Step
Action
LIFO stack, top on right
FIFO queue, front on left
1
Insert A
A
A
2
Insert B
A B
A B
3
Insert C
A B C
A B C
Removing all three elements produces C, B, A from the LIFO stack and A, B, C from the FIFO queue.
Abstract Types and Implementations
LIFO and FIFO describe ordering rules, or abstract data types, rather than one specific memory layout.
A stack implements LIFO and can use a dynamic array, linked list, or other backing storage.
A queue implements FIFO and can use a linked list, circular buffer, or deque.
Stacks and queues are linear, non-primitive data types: they process a sequence of elements, but are interfaces rather than primitive values.
They are not inherently static or dynamic, nor inherently contiguous or linked. Fixed-capacity buffers are static implementations, while lists, deques, and linked lists can grow dynamically; arrays and deques use contiguous blocks, whereas linked lists use linked nodes.
Python has no built-in linked-list implementation, so linked structures require a custom node class.
The ordering rule does not automatically determine every complexity. For example, a linked-list queue with head and tail references can enqueue and dequeue in worst-case O(1), while a resizing array-based queue may occasionally copy its elements.
Operation Costs
The following compares common Python 3.12 implementations. “Access” means viewing the next element to remove.
Implementation
Access average / worst
Search average / worst
Insert average / worst
Delete average / worst
Extra memory per element
Order
list as LIFO stack
O(1) / O(1)
O(n) / O(n)
O(1) amortized / O(n) during resize
O(1) amortized / O(n) during resize
O(1)
LIFO
collections.deque as FIFO queue
O(1) / O(1)
O(n) / O(n)
O(1) / O(1)
O(1) / O(1)
O(1) plus block overhead
FIFO
Best case: Stack insertion and deletion can take O(1) without triggering reallocation.
Average case: Stack end operations are O(1) amortized over a sequence of operations.
Worst case: A dynamic-array resize can cost O(n) because elements may need to be copied.
Space complexity: Both structures use O(n) total storage for n elements. Ordinary end operations use O(1) auxiliary space when the stored collection is excluded, but a list resize may require O(n) additional backing storage depending on the memory allocator. Counting the input collection gives O(n). Neither implementation uses a recursion stack.
Linked-list stacks and queues also use O(n) total space but add a pointer to every node. Arrays and deques are generally more cache-friendly because their elements occupy contiguous blocks.
Choosing Between Them
Choose LIFO when recent work should be handled before older work, such as reversing items, supporting undo, or evaluating nested expressions.
Choose FIFO when fairness or arrival order matters, such as processing requests in submission order or performing breadth-first search.
A priority queue is a different abstraction: it removes the highest-priority item, not necessarily the oldest or newest one.
Common Pitfalls
Do not use list.pop(0) to implement FIFO dequeue; removing index 0 costs O(n) because remaining elements must shift. Use collections.deque.popleft().
Do not assume every LIFO or FIFO structure has identical operation costs; the backing implementation matters.
Choose the structure based on the required operation rather than habit, and consider worst cases such as hash collisions or reallocations.
Remember that linked structures add pointer overhead and may have poorer cache locality.
In interviews, clarify whether the answer should cover only ordering or also the expected implementation, thread safety, and behavior when the data exceeds memory.
28
When is a circular queue useful in a real system?
KnownRevise
A circular queue is useful when producers and consumers exchange data through a bounded FIFO buffer, especially when memory usage must remain predictable or only recent data matters. Common examples include telemetry buffering, audio-frame pipelines, network packet handling, and embedded sensor systems.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
When It Fits
A circular queue, also called a ring buffer, stores elements in an array and wraps its indices back to the beginning when they reach the end. This avoids shifting elements when removing an item from the front.
It is particularly useful when:
Memory must be bounded: A fixed capacity prevents an unchecked producer from exhausting memory.
Recent data matters: With an overwrite-oldest policy, a full buffer discards its oldest item when a new item arrives.
Producer-consumer processing exists: Producers add work while consumers remove it in FIFO order.
Low-latency operations are needed: Array-backed enqueue and dequeue can both run in O(1) time.
For example, a telemetry system might keep the latest 100 sensor readings. When reading 101 arrives, reading 1 can be overwritten, leaving readings 2 through 101 available.
Some circular queues instead reject an enqueue when full. That is appropriate when every item must be processed and no data may be discarded.
Conceptually, a circular queue is a linear, non-primitive abstract data type. An array-backed implementation can have fixed, or static, capacity, although the circular-indexing technique can also be used with resizable storage.
How It Works
A typical implementation keeps:
A fixed-capacity backing array.
A head index identifying the oldest item.
A rear index or item count identifying where to write next.
A policy for handling a full buffer.
When an index reaches the capacity, it wraps around using modulo arithmetic. For capacity 3, the logical progression is:
Operation
Stored logical queue
Enqueue 1
[1]
Enqueue 2
[1, 2]
Enqueue 3
[1, 2, 3]
Dequeue
[2, 3]
Enqueue 4
[2, 3, 4]
Enqueue 5, overwriting oldest
[3, 4, 5]
Python’s collections.deque supports a maxlen option that discards the oldest item when the deque is full, providing the same bounded overwrite-oldest behavior without exposing ring-buffer indices.
Operation Costs
Let n be the number of items currently stored and c the fixed capacity.
Memory per element: One backing-array slot or object reference, plus any value-specific storage.
Order: FIFO, with the oldest retained item discarded first under the overwrite policy.
Python mapping: Use collections.deque for ordinary FIFO queues and collections.deque(maxlen=c) for bounded overwrite-oldest behavior.
Common Pitfalls
Choosing by habit: Select a structure based on the required operations—bounded FIFO access, ordering, update frequency, and memory limits.
Choosing the wrong full-buffer policy: Overwrite only when losing old data is acceptable; otherwise reject or block the producer.
Using a list as a queue: list.pop(0) is O(n) because remaining elements must shift; use collections.deque and popleft().
Ignoring concurrency requirements: Multiple producers or consumers may require synchronization or a carefully designed lock-free algorithm.
Overlooking edge cases: Define behavior for an empty queue, one item, duplicate and negative values, and zero capacity.
Interview follow-up: Expect questions about full-buffer handling, thread safety, dynamic capacity, and the memory overhead of each implementation.
29
How can a stack return its minimum element in O(1) time?
KnownRevise
A min stack augments each stack entry with the smallest value present at or below that entry. The minimum is then available from the top entry without scanning the stack.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
Core Design
Store tuples of (value, minimum_so_far) in a Python list used as a stack:
push(value): compute min(value, previous_minimum), then append (value, updated_minimum).
pop(): remove the top tuple and return its value.
peek(): return the top tuple’s value.
get_min(): return the top tuple’s stored minimum.
When the top entry is removed, the previous entry automatically exposes the minimum that was current before that push.
Operation Costs
Operation
Best
Average per operation
Worst single operation
Reason
push
O(1)
O(1) amortized
O(n)
Appending may occasionally reallocate the backing array
pop
O(1)
O(1) amortized
O(n)
Removing may occasionally shrink or reallocate the backing array
peek
O(1)
O(1)
O(1)
Reads the final list element directly
get_min
O(1)
O(1)
O(1)
Reads the minimum stored in the final entry
Total space: O(n), counting the stored input values and their metadata.
Extra space relative to a bare value stack: O(n), because every entry carries a minimum snapshot.
Recursion stack: O(1); the implementation is iterative.
In CPython 3.12, lists use contiguous storage for object references, while each tuple adds allocation and reference overhead.
Walkthrough
Operation
Stack entries (value, minimum)
Minimum
push(5)
[(5, 5)]
5
push(3)
[(5, 5), (3, 3)]
3
push(3)
[(5, 5), (3, 3), (3, 3)]
3
pop()
[(5, 5), (3, 3)]
3
push(7)
[(5, 5), (3, 3), (7, 3)]
3
Code Example
class MinStack:
"""A LIFO stack that supports O(1) minimum retrieval."""
def __init__(self) -> None:
self._entries: list[tuple[int, int]] = []
def push(self, value: int) -> None:
"""Add a value to the stack."""
current_minimum = self.get_min() if self._entries else value
self._entries.append((value, min(value, current_minimum)))
def pop(self) -> int:
"""Remove and return the most recently pushed value."""
if not self._entries:
raise IndexError("pop from empty MinStack")
value, _ = self._entries.pop()
return value
def peek(self) -> int:
"""Return the most recently pushed value."""
if not self._entries:
raise IndexError("peek from empty MinStack")
value, _ = self._entries[-1]
return value
def get_min(self) -> int:
"""Return the smallest value currently in the stack."""
if not self._entries:
raise IndexError("get_min from empty MinStack")
_, current_minimum = self._entries[-1]
return current_minimum
def is_empty(self) -> bool:
"""Return whether the stack contains no values."""
return not self._entries
stack = MinStack()
stack.push(5)
stack.push(3)
stack.push(3)
print(stack.get_min()) # 3
print(stack.pop()) # 3
print(stack.get_min()) # 3
Edge Cases
Empty stack: pop, peek, and get_min raise IndexError rather than returning an invalid value.
One element: value and minimum are the same, and popping removes the stack.
Duplicates: Each occurrence stores its own minimum snapshot, so duplicate minimum values work correctly.
Negative numbers: No special handling is needed because comparisons use the normal ordering.
Sorted or unsorted input: Input order does not affect correctness; the structure maintains the minimum of the values currently pushed.
Common Pitfalls
Calling get_min() on an empty stack without checking is_empty().
Storing only the current minimum: removing that value would make the previous minimum unavailable.
Using list.pop(0) instead of list.pop(), which would make removal O(n).
Assuming the stored minimum must be updated eagerly on every push; storing a snapshot with each entry avoids that work.
For a thread-safe production stack, protect both mutation and reads with a lock or use a concurrent stack implementation.
30
What problems can arise when implementing a stack with a dynamic array?
KnownRevise
A dynamic array is usually efficient for a stack, but resizing, memory management, and incorrect queue-style operations can introduce latency spikes, wasted capacity, and higher memory usage. Python’s list provides efficient append() and pop() operations for this purpose.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
Main Problems
Occasional expensive resizing: Appending may allocate a larger contiguous block and copy all existing elements. Individual append() calls are therefore O(1) amortized but O(n) worst case. A sequence of m pushes takes O(m) amortized time.
Memory overhead: A dynamic array reserves capacity in advance, so it may use more memory than the number of stored elements. The unused capacity is implementation-dependent.
Contiguous allocation: All elements must fit in one contiguous region. A very large stack may fail to allocate, and resizing may temporarily require additional memory while the old block is still occupied.
Fragmentation and locality trade-offs: Contiguous storage is usually cache-friendly, but repeated growth can create allocation and copying overhead.
Unnecessary shrinking cost: Automatically reducing capacity can trigger another allocation and copy. Whether and when a dynamic array shrinks is implementation-specific.
Wrong-end deletion: Using pop(0) removes the first element and requires an O(n) shift, turning stack operations into queue-like operations. A stack should use pop() without an index.
Concurrency issues: Individual operations may be safe, but compound operations such as checking if stack: and then calling pop() can race in another thread. Use synchronization when multiple threads share a stack.
Operation Costs
Operation
Dynamic array stack
Linked-list stack
Access by index
O(1) best, average, worst
O(n) best, average, worst
Search
O(n) best, average, worst
O(n) best, average, worst
Insert/push
O(1) amortized average; O(n) worst
O(1) average and worst when the head is known
Delete/pop
O(1) best, average, worst
O(1) when the node is known; O(n) when searching for it
Extra memory per element
O(1) amortized capacity overhead
O(1) pointer and node overhead
Memory layout
Contiguous
Scattered linked nodes
Order
LIFO; insertion order retained until popped
LIFO; insertion order retained until popped
For n elements, a dynamic-array stack uses O(n) auxiliary space, excluding the input itself. A linked-list stack also uses O(n) space but usually consumes more memory because every node stores a pointer. Neither implementation requires recursion, so there is no recursion-stack overhead.
Example Walkthrough
Using a Python list as a stack:
Step
Operation
Stack after operation
1
append(10)
[10]
2
append(20)
[10, 20]
3
append(30)
[10, 20, 30]
4
pop() returns 30
[10, 20]
5
pop() returns 20
[10]
The last element is removed first, so the stack behaves according to the LIFO rule.
Code Example
def use_list_as_stack(values: list[int]) -> tuple[int | None, list[int]]:
"""Push values onto a list-based stack and pop one value."""
stack: list[int] = []
for value in values:
stack.append(value)
popped = stack.pop() if stack else None
return popped, stack
# Example call:
# popped, remaining = use_list_as_stack([10, 20, 30])
# print(popped, remaining)
# Output: 30 [10, 20]
append() is O(1) amortized, and pop() from the end is O(1). In contrast, pop(0) is O(n) because Python must shift every remaining element.
Common Pitfalls
Using pop(0): This is appropriate for a queue implemented with a list, not for a stack.
Ignoring worst-case resize latency: Amortized O(1) does not mean every individual operation is constant-time.
Forgetting capacity overhead: A stack with few logical elements may still reserve substantial contiguous memory.
Assuming atomic compound operations: Check-then-pop sequences need locking in concurrent code.
Choosing by habit: A linked-list stack can avoid contiguous-allocation limits, while a dynamic-array stack is usually simpler and more cache-friendly for typical workloads.
Interview follow-up: Interviewers may ask how you would implement an O(1) maximum-size stack, make a stack thread-safe, or handle a stack larger than available contiguous memory.
31
What is a binary tree and what are its key properties?
KnownRevise
A binary tree is a hierarchical tree in which each node has at most two children, conventionally called its left and right child. Its shape and height determine how efficiently nodes can be searched, inserted, deleted, and traversed.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
Core Properties
Root: The top node has no parent.
Children: Every other node has exactly one parent and zero, one, or two children.
Leaves: Nodes with no children are called leaves.
Depth: A node’s depth is the number of edges from the root to it; the root has depth 0.
Height: A tree’s height is the number of edges on its longest root-to-leaf path. Some contexts count nodes instead, so the convention should be stated.
Left and right children: A binary tree may distinguish the two child positions even when the application does not otherwise use that distinction.
Tree size: A binary tree containing n nodes has n - 1 edges.
Shape: A binary tree is not necessarily complete or balanced. It may be extremely unbalanced, forming a linked-list-like chain.
For n nodes, the minimum possible height is ⌊log₂ n⌋, while the maximum is n - 1. Consequently, balanced trees provide logarithmic lookup paths, whereas degenerate trees can require linear traversal.
Common Binary Tree Variants
Full binary tree: Every node has either zero or two children.
Perfect binary tree: Every internal node has two children, and all leaves are at the same depth.
Complete binary tree: Every level except possibly the last is completely filled, and the last level is filled from left to right.
Balanced binary tree: At every node, the heights of the left and right subtrees differ by at most one. This keeps the height O(log n).
Degenerate binary tree: Almost every node has only one child, producing height O(n).
These categories are independent. For example, a complete binary tree is not necessarily a binary search tree or a heap. A binary search tree requires ordering between left and right subtrees, while a heap requires parent-child ordering and is commonly complete.
Representation and Use Cases
A binary tree can be represented using linked nodes containing a value and references to left and right children. This representation is flexible for arbitrary shapes but adds one pointer or reference per child and may scatter nodes across memory.
A complete binary tree can instead be stored in an array. With zero-based indexing, the left child of index i is at 2i + 1, and the right child is at 2i + 2. This representation is compact and cache-friendly.
Common uses include:
Binary search trees: Maintaining sorted keys and supporting ordered searches.
Heaps: Implementing priority queues, including scheduling and top-k selection.
Expression trees: Representing mathematical or programming-language expressions for parsing and evaluation.
Python has no general-purpose built-in binary tree. heapq implements a specialized min-heap, while ordinary list and deque structures can support breadth-first traversal with additional logic.
Complexity and Traversal
The costs depend heavily on whether the tree is balanced.
Operation
Balanced binary search tree
Arbitrary binary tree
Reason
Read a known node
O(1)
O(1)
The node reference is already available
Search by key
O(log n)
O(n) worst case
Balance limits the search path; a chain may contain every node
Insert
O(log n)
O(n) worst case
Finding the insertion position follows a root-to-leaf path
Delete
O(log n)
O(n) worst case
Finding and restructuring the node can be linear
Full traversal
O(n)
O(n)
Every node is visited once
A traversal visits all n nodes in O(n) time. Preorder, inorder, and postorder traversal use O(h) recursion-stack space, where h is the tree height. Breadth-first traversal uses O(w) queue space, where w is the maximum width of a level. Total storage is O(n) if the tree itself is included, or O(h)/O(w) auxiliary space if only traversal working memory is counted.
Common Pitfalls
Confusing a binary tree with a binary search tree: Binary structure alone does not impose sorted ordering.
Mixing up full, perfect, and complete: They describe different shape constraints.
Ignoring the height convention: Height commonly counts edges, but some definitions count nodes.
Assuming logarithmic operations always apply: Unbalanced trees can degrade to O(n).
Recursing too deeply: A degenerate tree can exceed Python’s default recursion limit; an explicit stack is safer.
Forgetting storage overhead: Linked representations require child references in addition to node values.
Interview Tips
Explain whether a tree is balanced before claiming O(log n) operations. Interviewers commonly ask about the difference between a binary tree and a binary search tree, how inorder traversal produces sorted output for a binary search tree, or how a complete binary tree can be stored in an array.
32
How to implement recursive in-order tree traversal
KnownRevise
In-order traversal is a recursive depth-first search strategy that visits the left subtree, then the root node, and finally the right subtree. It is the standard method for retrieving values from a binary search tree in sorted order.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
How It Works
The algorithm follows a strict Left -> Root -> Right pattern for every node in the tree. It is a form of depth-first search that explores as far down a branch as possible before backtracking.
Recursively traverse the left subtree.
Process the root node (e.g., print or store its value).
Recursively traverse the right subtree.
Consider a small tree with root 2, left child 1, and right child 3. The traversal order is 1, 2, 3. This property makes in-order traversal essential for validating binary search trees, where every node must be greater than its left descendants and less than its right descendants.
Time Complexity:O(n) in all cases. The algorithm visits every node exactly once, where n is the total number of nodes.
Space Complexity:O(h) auxiliary space, where h is the height of the tree. This accounts for the recursion stack frames. The input tree structure is not counted.
Balanced Tree: O(log n) stack depth.
Skewed Tree: O(n) stack depth, effectively a linked list.
The space required for the output list of values is O(n) but is typically excluded from auxiliary space calculations.
Code Example
This implementation uses a nested helper function to avoid passing the result list through every recursive call, which is more efficient than list concatenation.
from typing import List, Optional
class TreeNode:
def __init__(self, val: int = 0, left: Optional['TreeNode'] = None, right: Optional['TreeNode'] = None):
self.val = val
self.left = left
self.right = right
def inorder_traversal(root: Optional[TreeNode]) -> List[int]:
"""Return the values of a binary tree in in-order sequence."""
result: List[int] = []
def traverse(node: Optional[TreeNode]) -> None:
if node is None:
return
traverse(node.left)
result.append(node.val)
traverse(node.right)
traverse(root)
return result
# Example: Skewed tree with root 1, right child 2, right child 3
# 1
# \
# 2
# \
# 3
print(inorder_traversal(TreeNode(1, None, TreeNode(2, None, TreeNode(3)))))
# Output: [1, 2, 3]
Common Pitfalls
Recursion Depth: Python’s default recursion limit is approximately 1000 frames. A skewed tree with more than 1000 nodes will raise a RecursionError. In production or large inputs, an iterative approach with an explicit stack is safer.
Order Confusion: It is easy to mix up in-order (Left-Root-Right) with pre-order (Root-Left-Right) or post-order (Left-Right-Root). Memorize the root position.
Edge Cases: The base case node is None handles empty trees and single nodes gracefully. The logic naturally handles duplicates and negative numbers.
Invalid Inputs: Cycles or disconnected graphs are invalid inputs for this structure and will cause infinite recursion or incomplete traversal.
Follow-up Question: Interviewers often ask for an iterative implementation using an explicit stack to avoid recursion limits, or how to perform Morris traversal for O(1) space.
33
How do you implement a non-recursive post-order tree traversal?
KnownRevise
A non-recursive post-order traversal visits a binary tree in the order left subtree → right subtree → root. An explicit stack stores nodes and a visited flag so each node is processed after both of its children.
Read full answerHide full answer
Applies toPython 3.12
How It Works
Push the root onto a stack with visited=False.
Pop a node from the stack.
If its visited flag is True, append its value to the result.
Otherwise, push the node back with visited=True, then push its right child and left child.
Because the stack is LIFO, the left child is processed before the right child, producing post-order.
Example Walkthrough
For this tree:
1
/ \
2 3
The traversal is:
Visit node 2.
Visit node 3.
Visit root 1.
The result is [2, 3, 1].
Complexity
Let n be the number of nodes and h the tree height measured in edges.
Time:O(n) in the best, average and worst cases. Each node is pushed and processed a constant number of times.
Auxiliary space:O(h) for the explicit stack, since a binary tree can have only one pending sibling per level. The worst case is O(n) for a completely skewed tree.
Output space:O(n) for the returned list.
The input tree is not counted as auxiliary space.
Code Example
from dataclasses import dataclass
@dataclass
class TreeNode:
value: int
left: "TreeNode | None" = None
right: "TreeNode | None" = None
def postorder_traversal(root: TreeNode | None) -> list[int]:
"""Return node values in left-right-root order without recursion."""
result: list[int] = []
stack: list[tuple[TreeNode, bool]] = []
if root is not None:
stack.append((root, False))
while stack:
node, visited = stack.pop()
if visited:
result.append(node.value)
continue
# Process this node after its children.
stack.append((node, True))
# Push right first so the left subtree is visited first.
if node.right is not None:
stack.append((node.right, False))
if node.left is not None:
stack.append((node.left, False))
return result
# Example:
root = TreeNode(
1,
TreeNode(2),
TreeNode(3),
)
print(postorder_traversal(root)) # Expected output: [2, 3, 1]
Common Pitfalls
Wrong push order: Push the right child before the left child when using a LIFO stack. Reversing this produces a different traversal order.
Missing the visited marker: Without marking a node after its children, the result will not be post-order.
Off-by-one or ordering errors: Carefully distinguish the first visit to a node from the later visit that appends its value.
Integer overflow: Python integers have arbitrary precision. When porting the algorithm to a fixed-width language, ensure node values can fit in its integer type.
Mutating input: The function does not modify the tree. Preserve this property if extending the implementation.
Empty tree: The function correctly returns [] when root is None.
One-node tree: The function returns the node’s value.
Duplicate or negative values: These are handled normally because traversal order depends on tree structure, not value magnitude.
Cycle assumption: The algorithm assumes the input is a valid tree. A cyclic structure would cause repeated processing because there is no cycle-detection set.
Disconnected graphs: This is not applicable to a single tree-traversal call. To process multiple disconnected components, call the function once for each root.
Already sorted input: Sorting is not relevant to a tree traversal; the structure determines the traversal order.
Typical follow-up: Interviewers may ask how to adapt the algorithm to an n-ary tree or how to implement preorder and inorder traversals with the same stack-based technique.
34
Why prefer balanced binary trees over unbalanced ones?
KnownRevise
A balanced binary search tree guarantees logarithmic height, keeping search, insertion, and deletion efficient even for worst-case input orders. An unbalanced binary search tree can degenerate into a linked list, causing some operations to take linear time.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
Key Difference
Both structures store keys in sorted order, but their height differs:
Balanced tree: Height is always O(log n).
Unbalanced tree: Height may be O(n) when keys are inserted in sorted or nearly sorted order.
Each tree operation follows one path from the root to a target node. Therefore, limiting the height directly limits the number of comparisons.
Operation Costs
Operation or property
Balanced binary search tree
Unbalanced binary search tree
Search by key
O(log n) average and worst case
O(log n) average for random insertion order; O(n) worst case
Insert by key
O(log n) average and worst case, including rotations
O(log n) average for random insertion order; O(n) worst case
Delete by key
O(log n) average and worst case
O(log n) average for random insertion order; O(n) worst case
Access by position
Not directly supported; traversal is O(n)
Not directly supported; traversal is O(n)
Sorted order
Preserved
Preserved
Extra memory per element
O(1) for links plus constant balance metadata
O(1) for links
Total storage
O(n)
O(n)
The average-case O(log n) result for an unbalanced tree assumes reasonably random insertion order; it is not a guarantee. Balanced trees provide the worst-case bound by enforcing a height restriction after updates.
Space Complexity
The tree stores O(n) nodes, so its total space complexity is O(n). Under the usual auxiliary-space convention, the input values themselves are not counted because the nodes represent the stored data.
Recursive operations also use O(h) stack space, where h is the tree height:
Balanced tree: O(log n) recursion stack in the worst case.
Unbalanced tree: O(n) recursion stack in the worst case.
An iterative implementation can avoid recursion-stack usage, although it may require parent pointers or other bookkeeping for updates.
Small Example
Consider inserting the keys 1 through 7.
An unbalanced tree can become:
1
└─ 2
└─ 3
└─ 4
└─ 5
└─ 6
└─ 7
Its height is 6 edges, so searching for 7 may inspect all seven nodes.
A balanced arrangement could be:
4
/ \
2 6
/ \ / \
1 3 5 7
Searching for 7 now visits 4, 6, and 7, taking only three node visits. The balancing rule keeps this improvement possible regardless of insertion order.
Efficient ordered updates: Insertions and deletions preserve sorted order without rebuilding the entire structure.
Range queries: In-order traversal and successor or predecessor lookups are efficient.
Memory cost: Balanced trees need additional metadata, such as node heights or colors.
Implementation cost: Rotations or other rebalancing logic make implementation more involved than for a plain binary search tree.
A balanced tree is appropriate when ordered data, range queries, and frequent updates matter. A hash table such as Python’s dict is a better alternative when only exact-key lookup is needed and ordering or range queries are unnecessary.
Common Pitfalls
Do not assume every binary tree is a binary search tree; ordering depends on how keys are placed.
“Balanced” does not mean the tree is a heap; balancing and heap-ordering are different concepts.
Do not claim O(log n) for an unbalanced tree without specifying favorable input order.
Avoid using a plain list as a queue when collections.deque is available, because removing from the front of a list is O(n).
Consider whether ordered traversal is required before choosing a tree over a hash table.
In an interview, be ready to explain how AVL and red-black trees restore balance and how their guarantees differ.
35
How does a binary search tree differ from other trees?
KnownRevise
A binary search tree (BST) is a dynamic, non-linear binary tree whose nodes follow an ordering invariant: keys in a node’s left subtree are smaller, while keys in its right subtree are larger, subject to an explicit duplicate-key policy. This ordering supports efficient key lookup and range queries when the tree remains reasonably balanced.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
Core Properties
Ordering invariant: Every key in the left subtree precedes the node’s key, and every key in the right subtree follows it.
Traversal order: An in-order traversal produces keys in ascending order when duplicates are rejected, or nondecreasing order when a duplicate policy preserves their relative ordering.
Classification: A BST is non-linear, dynamic, and non-primitive. It is a composite structure containing keys and references to nodes.
Memory layout: Nodes are normally allocated separately and connected through references rather than stored in one contiguous block.
Operation Costs
Search: Compare the target with the current node and continue into only one subtree.
Insertion: Follow the ordering path until an appropriate empty child position is found.
Deletion: For a node with two children, copy its in-order successor or predecessor and then remove that displaced node. For a node with zero or one child, replace the node with its child or remove it directly.
For a tree with height (h), where height counts edges:
Operation
Best
Average
Worst
Reason
Search
O(1)
O(log n)
O(n)
Work follows one root-to-leaf path
Insert
O(1)
O(log n)
O(n)
A skewed tree may require visiting every node
Delete by key
O(1)
O(log n)
O(n)
Lookup dominates, followed by local restructuring
Traversal
O(n)
O(n)
O(n)
Every node is visited
The average BST bounds assume random insertion order or a tree whose height is O(log n). Sorted insertion can produce a tree whose height is n.
Comparison With Other Structures
For tree rows, access means obtaining an existing node through a known reference, while search, insertion, and deletion are key-based.
Structure
Access
Search: average / worst
Insert: average / worst
Delete: average / worst
Extra memory per element
Order kept
General binary tree
O(1) with node reference
O(n) / O(n)
O(n) / O(n)
O(n) / O(n)
O(1) child-reference overhead
None
Ordinary BST
O(1) with node reference
O(log n) / O(n)
O(log n) / O(n)
O(log n) / O(n)
O(1) child-reference overhead
Sorted
Balanced BST, such as AVL
O(1) with node reference
O(log n) / O(log n)
O(log n) / O(log n)
O(log n) / O(log n)
O(1) references plus balance metadata
Sorted
Binary heap
O(1) peek
O(n) / O(n)
O(log n) / O(log n)
O(log n) at root; O(n) arbitrary
O(1) array-slot overhead
Heap order only
B-tree
O(1) with node reference
O(logₘ n) / O(logₘ n)
O(logₘ n) / O(logₘ n)
O(logₘ n) / O(logₘ n)
O(1) key/pointer metadata per key
Sorted
Here, (m) is the maximum number of children per B-tree node.
How BSTs Differ From Other Trees
General binary tree: Has at most two children per node but imposes no key-ordering invariant. Expression trees are a common application.
Balanced BST: Is a BST that also maintains O(log n) height through balancing mechanisms. Balanced BSTs implement ordered maps and sets.
Binary heap: Is a complete tree with parent-child heap ordering, ideal for constant-time root access and efficient priority scheduling. Python’s heapq provides an array-backed binary min-heap.
B-tree: Is a multiway ordered tree optimized for storage systems and database indexes; B+ trees are a common database variant.
Ternary tree: Allows up to three children per node, unlike a binary tree’s maximum of two.
For example, inserting 2, 5, 3, 7, 1 produces a BST whose in-order traversal is 1, 2, 3, 5, 7.
Space and Practical Use
BSTs are useful for in-memory ordered indexes, range queries, and implementing ordered maps. A BST with n nodes requires O(n) total node storage, typically with O(1) reference overhead per node.
Auxiliary space is O(1) for iterative operations and O(h) for recursive operations because the recursion stack can contain h frames. These auxiliary bounds exclude the input collection and existing BST nodes. If newly created node storage is counted as additional space, total extra space is O(n + h), which is O(n).
Python 3.12 has no built-in balanced BST. bisect searches an already sorted list in O(log n), but inserting into that list remains O(n).
Common Pitfalls
Choosing by habit: Select a BST for ordered lookup or range queries; use a hash table when average O(1) key lookup without ordering is sufficient.
Unbalanced insertion order: Sorted input can degrade key-based operations to O(n).
Inconsistent duplicate handling: Choose and document whether duplicates are rejected, counted, or placed consistently.
Recursive depth: A skewed tree may exceed Python’s default recursion limit; an explicit stack is safer.
Memory overhead: Each node stores values, references, and possibly balancing metadata.
Mutating during traversal: Structural changes can invalidate the traversal position; use a strategy designed for the mutation.
36
How do you validate a binary tree is a BST
KnownRevise
A binary tree is a valid BST when every node’s value is strictly greater than all values in its left subtree and strictly less than all values in its right subtree. The correct check tracks a valid value range for each node rather than comparing only to immediate children, since a node’s descendants must all respect the ancestor’s bounds.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
How It Works
The core idea is to pass down a minimum and maximum bound as you traverse:
Start at the root with the range (-inf, +inf).
For each node, verify its value falls strictly inside the current bounds.
When visiting the left child, tighten the upper bound to the node’s value.
When visiting the right child, tighten the lower bound to the node’s value.
An empty subtree is valid by definition.
A common mistake is checking only node.left.val < node.val < node.right.val. This fails for trees where a descendant violates the bound, such as a node whose right child’s left subtree contains a smaller value.
Time and Space Complexity
Time: O(n) — every node is visited once.
Space: O(h) — the recursion stack only; the input tree itself is not counted. Worst case O(n) for a skewed tree, O(log n) for a balanced tree.
Code Example
from typing import Optional
class TreeNode:
def __init__(self, val: int = 0, left: Optional['TreeNode'] = None, right: Optional['TreeNode'] = None):
self.val = val
self.left = left
self.right = right
def is_valid_bst(root: Optional[TreeNode]) -> bool:
"""Return True if the binary tree rooted at `root` is a valid BST."""
def validate(node: Optional[TreeNode], min_val: float, max_val: float) -> bool:
if not node:
return True
if not (min_val < node.val < max_val):
return False
return validate(node.left, min_val, node.val) and validate(node.right, node.val, max_val)
return validate(root, float('-inf'), float('inf'))
# Example: a valid BST
# 5
# / \
# 3 7
# / \
# 1 4
# print(is_valid_bst(TreeNode(5, TreeNode(3, TreeNode(1), TreeNode(4)), TreeNode(7))))
# True
Common Pitfalls
Comparing only immediate children — the recursive range check is required, not just left < node < right.
Integer overflow in other languages — use Long.MIN_VALUE/Long.MAX_VALUE in Java instead of float('inf') to avoid edge-case failures.
Duplicates are not allowed — a BST requires strictly less/greater, never <= or >=.
Recursion depth — for very deep or skewed trees, an iterative solution with an explicit stack avoids hitting Python’s recursion limit.
Follow-up — interviewers often ask you to implement the iterative version, or to handle a tree with duplicate values.
37
How do you find the lowest common ancestor (LCA) in a BST?
KnownRevise
The lowest common ancestor (LCA) of two nodes in a binary search tree (BST) is the deepest node that has both nodes as descendants. It can be found by following a single path from the root, using the BST ordering to decide whether to move left or right.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
Approach
At each node, compare the values of the two target nodes with the current node:
If both targets are smaller, the LCA must be in the left subtree.
If both are larger, the LCA must be in the right subtree.
Otherwise, the targets are on opposite sides of the current node, or one equals it, so the current node is the LCA.
This works because every value in a node’s left subtree is smaller and every value in its right subtree is larger.
The implementation assumes a strict BST with unique keys and that both target nodes belong to the tree.
Example Walkthrough
Consider this tree:
8
/ \
3 10
/ \ \
1 6 14
For nodes 6 and 14:
At 8, 6 < 8 while 14 > 8.
The targets lie in different subtrees.
Therefore, 8 is their lowest common ancestor.
For nodes 1 and 6:
At 8, both values are smaller, so move left to 3.
At 3, 1 < 3 while 6 > 3.
The targets now lie in different subtrees, so the LCA is 3.
Correctness
When both targets are smaller than the current node, both belong to its left subtree. Any common ancestor outside that subtree would be higher than the current node, so the LCA must be found by continuing left. The symmetric argument applies when both targets are larger.
When one target is on each side, the current node is a common ancestor. Since no deeper node can be an ancestor of both, it is the lowest one. If one target equals the current node, that node is also the LCA.
Complexity
Let h be the tree height measured in edges and n the number of nodes.
Case
Time
Reason
Best
O(1)
The root is already the LCA.
Average/typical
O(log n)
The tree is balanced, so its height is logarithmic.
Worst
O(n)
The tree is completely skewed, and the search follows one long path.
Auxiliary space:O(1) because the iterative implementation uses only one current-node reference and no recursion stack.
Input storage: The tree itself uses O(n) space, but it is not counted as auxiliary space.
A recursive implementation would require O(h) auxiliary space for the recursion stack and could exceed Python’s default recursion limit on a deeply skewed tree.
Code Example
from __future__ import annotations
from dataclasses import dataclass
@dataclass(slots=True)
class TreeNode:
value: int
left: TreeNode | None = None
right: TreeNode | None = None
def lowest_common_ancestor(
root: TreeNode | None,
node_a: TreeNode | None,
node_b: TreeNode | None,
) -> TreeNode | None:
"""Return the lowest common ancestor of two nodes in a strict BST."""
if root is None or node_a is None or node_b is None:
return None
current = root
while current is not None:
if node_a.value < current.value and node_b.value < current.value:
current = current.left
elif node_a.value > current.value and node_b.value > current.value:
current = current.right
else:
return current
return None
root = TreeNode(
8,
left=TreeNode(
3,
left=TreeNode(1),
right=TreeNode(6),
),
right=TreeNode(
10,
right=TreeNode(14),
),
)
node_6 = root.left.right
node_14 = root.right.right
ancestor = lowest_common_ancestor(root, node_6, node_14)
print(ancestor.value if ancestor is not None else None) # 8
Common Pitfalls
Duplicate keys: The code assumes a strict BST. If duplicates are allowed, the application must specify whether equal-valued nodes belong on the left, right, or both sides.
Incorrect targets: The function assumes both nodes are in the tree; it does not validate that they are present.
Wrong ancestor definition: The LCA is the deepest common ancestor, not merely a node whose value lies between the two target values.
Recursion limits: The iterative version avoids stack overflow on a skewed tree.
Malformed trees: Cycles and disconnected nodes are not valid BST conditions and are not detected by this algorithm.
Common interview follow-ups include finding the LCA in a general binary tree, returning the path between two nodes, handling duplicate values, and determining the LCA when one target is absent.
38
When should DFS or BFS be used in trees?
KnownRevise
Depth-first search (DFS) follows one branch as deeply as possible before backtracking, while breadth-first search (BFS) explores nodes level by level. DFS suits path exploration and subtree processing; BFS suits level-order traversal and shortest paths measured by edge count.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
DFS: Explore One Branch at a Time
DFS uses a stack, either explicitly or through recursive function calls. It is useful when:
Finding any path between two nodes.
Processing an entire subtree before moving to a sibling.
Generating paths, combinations, or permutations with backtracking.
Computing postorder or other depth-dependent results.
Detecting cycles or finding connected components when applied to a general graph.
In a tree, DFS does not guarantee the shortest path in a general graph because it may explore a longer branch first. A tree itself has exactly one path between any two connected nodes, so cycle detection is unnecessary.
BFS: Explore by Levels
BFS uses a queue and processes all nodes at one depth before nodes at the next depth. It is useful when:
Producing level-order traversal.
Finding the shortest path by number of edges in an unweighted tree or graph.
Finding nodes closest to a starting node.
Processing tasks level by level, such as expanding a search by distance.
Performing breadth-first operations such as minimum-depth or minimum-change analyses.
For example, BFS finds the path with the fewest edges from a node in an unweighted graph. DFS is preferable when the goal is to exhaust a branch or evaluate one subtree at a time.
Complexity and Selection
A tree is a non-linear data structure. In the following table, a complete traversal visits all V nodes:
Input space: The tables show auxiliary space, excluding the input tree. Including the input, total space is O(V).
Recursive DFS: Its recursion stack is O(H), where H is the tree height measured in edges.
Iterative DFS: Pending sibling nodes can make its stack O(V) in the worst case.
Early termination: If a traversal stops when it finds a target, its time is O(K), where K is the number of visited nodes; the best case is O(1) when the target is the root.
Memory behavior: DFS is often memory-efficient for narrow trees, while BFS can consume substantial memory when a level is very wide.
Example Walkthrough
For this tree:
1
├── 2
│ └── 4
└── 3
└── 5
DFS visits 1, then follows the first branch through 2 and 4, backtracks, and visits 3 and 5: [1, 2, 4, 3, 5].
BFS visits 1, then [2, 3], then [4, 5]: [[1], [2, 3], [4, 5]].
Code Example
The following implementations avoid recursion-depth limits and preserve each node’s child order.
from collections import deque
from dataclasses import dataclass, field
@dataclass(slots=True)
class TreeNode:
value: int
children: list["TreeNode"] = field(default_factory=list)
def dfs_preorder(root: TreeNode | None) -> list[int]:
"""Return DFS preorder values using an explicit stack."""
if root is None:
return []
result: list[int] = []
stack: list[TreeNode] = [root]
while stack:
node = stack.pop()
result.append(node.value)
stack.extend(reversed(node.children))
return result
def bfs_level_order(root: TreeNode | None) -> list[list[int]]:
"""Return values grouped by tree level using a queue."""
if root is None:
return []
result: list[list[int]] = []
queue: deque[TreeNode] = deque([root])
while queue:
level_size = len(queue)
level: list[int] = []
for _ in range(level_size):
node = queue.popleft()
level.append(node.value)
queue.extend(node.children)
result.append(level)
return result
tree = TreeNode(
1,
[
TreeNode(2, [TreeNode(4)]),
TreeNode(3, [TreeNode(5)]),
],
)
print(dfs_preorder(tree), bfs_level_order(tree))
# [1, 2, 4, 3, 5] [[1], [2, 3], [4, 5]]
Common Pitfalls
Using a list as a queue: list.pop(0) is O(n); use collections.deque.popleft() for O(1) removal.
Choosing DFS for shortest paths: Use BFS when every edge has equal cost and you need the minimum number of edges; use Dijkstra’s algorithm when weights differ but are non-negative.
Forgetting visited state: A tree is connected and acyclic, so these implementations need no visited set. General graphs require one to handle cycles; disconnected graphs also require an outer loop over all roots.
Edge cases: Empty input returns an empty result, a single node returns that node, and duplicate or negative values do not affect traversal order.
Order assumptions: Traversal order follows the tree’s structure, not whether its values are sorted.
39
How to convert a binary tree into a doubly linked list
KnownRevise
Converting a binary tree into a doubly linked list is done by in-order traversal, which visits nodes in ascending order for a binary search tree and reuses each node’s left and right pointers to link the previous and next nodes in the list.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
How It Works
In-order traversal: visit left subtree, current node, right subtree to get a deterministic sequence.
Last node reference: maintain a last_node variable for the most recently visited node.
Pointer repurposing: set the current node’s left pointer to last_node and last_node‘s right pointer to the current node.
Head tracking: the first node visited becomes the head of the doubly linked list.
Applicability: works for any binary tree; for a binary search tree, the list is sorted.
Step-by-Step Example
Consider the tree:
2
/ \
1 3
Visit left subtree: node 1. No left child. last_node is None, so head becomes node 1. last_node = node 1.
Return to node 2. last_node is node 1. Set 1.right = 2 and 2.left = 1. last_node = node 2.
Visit right subtree: node 3. No left child. last_node is node 2. Set 2.right = 3 and 3.left = 2. last_node = node 3.
The list is 1 <-> 2 <-> 3. Return head = node 1.
Complexity
Time: O(n) where n is the number of nodes, because each node is visited exactly once.
Space: O(h) for the recursion stack, where h is the tree height.
Balanced tree: O(log n).
Skewed tree: O(n).
The conversion is in-place, so no additional data structures are used beyond the recursion stack.
Code Example
from typing import Optional
class TreeNode:
def __init__(self, val: int = 0, left: 'TreeNode' = None, right: 'TreeNode' = None):
self.val = val
self.left = left
self.right = right
def convert_to_doubly_linked_list(root: Optional[TreeNode]) -> Optional[TreeNode]:
"""Convert a binary tree into a doubly linked list in-place.
The left pointer of each node becomes the previous node,
and the right pointer becomes the next node in the list.
Returns the head of the doubly linked list.
"""
if root is None:
return None
# Track the last node visited during in-order traversal.
last_node = None
head = None
def in_order(node: Optional[TreeNode]) -> None:
nonlocal last_node, head
if node is None:
return
# Visit left subtree.
in_order(node.left)
# Process current node.
if last_node is None:
# First node becomes the head of the list.
head = node
else:
last_node.right = node
node.left = last_node
last_node = node
# Visit right subtree.
in_order(node.right)
in_order(root)
return head
# Example: tree with root 2, left child 1, right child 3
# root = TreeNode(2, TreeNode(1), TreeNode(3))
# head = convert_to_doubly_linked_list(root)
# Expected output: head.val == 1, head.right.val == 2, head.right.left.val == 1, head.right.right.val == 3
Common Pitfalls
Empty tree: always check for None root before traversal.
Head tracking: the first node visited must be stored as the head; otherwise, you cannot access the list start.
Recursion depth: for very deep or skewed trees, recursion may exceed Python’s limit; consider an iterative approach with an explicit stack.
Mutating input: this conversion modifies the tree structure in-place, so the original tree is lost.
Follow-up: interviewers often ask how to handle a non-recursive solution or how to convert the list back to a tree.
40
How to serialize and deserialize a binary tree?
KnownRevise
Serialization is the process of converting a binary tree into a sequence of characters or a list so it can be stored or transmitted, while deserialization reconstructs the original tree structure from that sequence. The core requirement is to capture not only the node values but also the null pointers that define the tree’s shape, ensuring the round-trip conversion is lossless.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
How It Works
To serialize a tree, you must traverse it in a deterministic order. The two standard approaches are:
Level-order traversal (BFS): Uses a queue to process nodes level by level. This is the most common interview expectation (e.g., LeetCode 297) because it maps naturally to a list representation.
Pre-order traversal (DFS): Recursively visits the root, then left, then right. It is compact but requires careful handling of recursion depth.
Both methods must explicitly mark empty children (e.g., with None or #) so the deserialization algorithm knows where branches end. Without these markers, a sequence of values like 1, 2, 3 is ambiguous.
Code Example
The following implementation uses collections.deque for O(1) popleft operations, avoiding the O(n) cost of list.pop(0).
from collections import deque
from typing import Optional
class TreeNode:
def __init__(self, val: int = 0, left: 'TreeNode' = None, right: 'TreeNode' = None):
self.val = val
self.left = left
self.right = right
class TreeCodec:
def serialize(self, root: Optional[TreeNode]) -> str:
"""Converts a binary tree to a comma-separated string using BFS."""
if not root:
return ""
queue = deque([root])
result = []
while queue:
node = queue.popleft()
if node:
result.append(str(node.val))
queue.append(node.left)
queue.append(node.right)
else:
result.append("None")
return ",".join(result)
def deserialize(self, data: str) -> Optional[TreeNode]:
"""Reconstructs the binary tree from its serialized string."""
if not data:
return None
values = data.split(",")
root = TreeNode(int(values[0]))
queue = deque([root])
i = 1
while queue and i < len(values):
node = queue.popleft()
if values[i] != "None":
node.left = TreeNode(int(values[i]))
queue.append(node.left)
i += 1
if i < len(values) and values[i] != "None":
node.right = TreeNode(int(values[i]))
queue.append(node.right)
i += 1
return root
# Example: Tree 1 -> (2, 3)
# root = TreeCodec().deserialize("1,2,3,None,None,None,None")
Complexity
Time Complexity: O(N) for both serialization and deserialization, where N is the number of nodes. Every node and every null child is visited exactly once.
Space Complexity: O(N). The queue holds up to N/2 nodes at the widest level, and the output string or list of values also scales linearly with the number of nodes.
Common Pitfalls
Using list.pop(0): This shifts all remaining elements, making it O(n) per operation and turning an O(n) algorithm into O(n²). Always use collections.deque for queues.
Forgetting null markers: If you only store values, the deserializer cannot distinguish between a tree with a left child and one without.
Recursion depth: A recursive DFS approach can exceed Python’s default recursion limit (approx. 1000) for deep or skewed trees. The iterative BFS approach above avoids this risk.
Ambiguous delimiters: Ensure the separator (e.g., ,) cannot appear in the node values. Using integers avoids this, but strings require escaping.
Trailing nulls: Serializers often append trailing None entries for children of the last level. Ensure the deserializer consumes them consistently.
Follow-up question: Interviewers often ask how to handle trees with duplicate values, how to serialize a binary tree with parent pointers, or what to do if the tree is too large to fit in memory (streaming serialization).
41
How does a hash table work internally?
KnownRevise
A hash table is a dynamic, non-primitive, non-linear implementation of the map or dictionary abstract data type. It hashes keys into bucket indices and provides expected constant-time lookup, insertion, and deletion when hashing quality and load factor are controlled.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
Core Mechanics
A lookup proceeds in three steps:
Compute the key’s hash value.
Convert that value into a bucket index, commonly with modulo.
Compare the key with entries in that bucket until the desired entry is found or the bucket is exhausted.
The map defines the supported operations, while a hash table defines one implementation. The hash function should distribute the complete key set uniformly; similar keys do not need to map to nearby buckets. Hash tables are commonly used for caches, database indexes, and frequency counting.
The bucket array is contiguous and generally cache-friendly. Separate chaining stores additional entries separately, adding pointer or reference overhead and making memory access less contiguous.
Collisions, Load Factor, and Resizing
A collision occurs when different keys select the same bucket.
Separate chaining: Each bucket contains a collection of entries, so colliding entries are stored together.
Open addressing: All entries occupy slots in an array, and collisions are resolved with a probing sequence such as linear probing.
Load factor:size / bucket_count. A threshold near 0.75 is common, although implementations vary.
Resizing: A larger bucket array is allocated and all entries are rehashed. Doubling the table gives amortized constant-time insertion because the total rehash cost is spread across insertions.
Open addressing often uses tombstones for deletion. Without periodic cleanup or another bounded-probe strategy, stale tombstones can make later searches and deletions increasingly expensive.
Operation Costs
These bounds assume constant-time key hashing, a good hash function, a bounded load factor, and bounded probe or chain lengths.
Implementation
Access by Key
Search
Insert
Delete
Extra Space per Entry
Order
Separate chaining
Best O(1), average O(1), worst O(n)
Best O(1), average O(1), worst O(n)
Best O(1), average O(1) amortized, worst O(n)
Best O(1), average O(1), worst O(n)
O(1) plus bucket overhead
No global order
Open addressing
Best O(1), average O(1), worst O(n)
Best O(1), average O(1), worst O(n)
Best O(1), average O(1) amortized, worst O(n)
Best O(1), average O(1), worst O(n)
O(1) slot, excluding cleanup overhead
No global order
Average operations are constant because a good hash function usually produces short chains or few probes. Worst-case operations are linear when many keys collide.
For n entries and a bounded load factor, total table space is O(n), including buckets and entry metadata but excluding the key and value objects themselves. An individual iterative operation uses O(1) auxiliary space, including a constant-size recursion stack. Hashing a string of length L additionally costs O(L) time and O(1) auxiliary space.
Python 3.12 Mapping
dict implements a hash-table-backed mapping and preserves insertion order.
set provides expected O(1) membership testing using hashing.
collections.deque supports efficient queue operations but not keyed lookup.
Production Python code should normally use dict and set because the built-ins are optimized and hardened.
Python randomizes str and bytes hashes by default, making hash-flooding denial-of-service attacks harder.
Code Example
The following educational implementation uses separate chaining, predictable hashing, duplicate-key replacement, deletion, and automatic resizing.
class HashTable:
"""A small string-to-integer hash table using separate chaining."""
def __init__(self, initial_capacity: int = 8) -> None:
if initial_capacity < 1:
raise ValueError("initial_capacity must be positive")
self._buckets: list[list[tuple[str, int]]] = [
[] for _ in range(initial_capacity)
]
self._size = 0
self._threshold = initial_capacity * 3 // 4
@staticmethod
def _stable_hash(text: str) -> int:
value = 0
for character in text:
value = (value * 31 + ord(character)) & 0xFFFFFFFF
return value
def _bucket_index(self, key: str) -> int:
return self._stable_hash(key) % len(self._buckets)
def _resize(self) -> None:
old_buckets = self._buckets
self._buckets = [[] for _ in range(len(old_buckets) * 2)]
self._threshold = len(self._buckets) * 3 // 4
for bucket in old_buckets:
for key, value in bucket:
self._buckets[self._bucket_index(key)].append((key, value))
def __getitem__(self, key: str) -> int:
bucket = self._buckets[self._bucket_index(key)]
for stored_key, value in bucket:
if stored_key == key:
return value
raise KeyError(key)
def __setitem__(self, key: str, value: int) -> None:
bucket = self._buckets[self._bucket_index(key)]
for position, (stored_key, _) in enumerate(bucket):
if stored_key == key:
bucket[position] = (key, value)
return
if self._size + 1 > self._threshold:
self._resize()
bucket = self._buckets[self._bucket_index(key)]
bucket.append((key, value))
self._size += 1
def __delitem__(self, key: str) -> None:
bucket = self._buckets[self._bucket_index(key)]
for position, (stored_key, _) in enumerate(bucket):
if stored_key == key:
del bucket[position]
self._size -= 1
return
raise KeyError(key)
def __contains__(self, key: str) -> bool:
bucket = self._buckets[self._bucket_index(key)]
return any(stored_key == key for stored_key, _ in bucket)
def __len__(self) -> int:
return self._size
def main() -> None:
"""Demonstrate replacement, membership, and deletion."""
table = HashTable()
table["a"] = 1
table["b"] = 2
table["a"] = 3
print(table["a"])
print("b" in table)
del table["a"]
print("a" in table)
if __name__ == "__main__":
main()
Expected output:
3
True
False
With eight buckets, "a" hashes to bucket 1 and "b" to bucket 2. Reinserting "a" replaces its value in bucket 1; deleting it removes only that entry. Empty-table lookups and deletions raise KeyError; one-element tables work normally; duplicate keys replace values; and negative integer values are supported. Input order is irrelevant, while graph connectivity and cycles do not apply to a hash table.
Common Pitfalls
Confusing average O(1) with guaranteed O(1) performance.
Using predictable or weak hashes in security-sensitive systems.
Forgetting that hash tables provide keyed lookup, not positional or range queries.
Ignoring entry, bucket, and tombstone overhead.
Making operations thread-safe without a defined locking or concurrent-data-structure strategy.
42
How do hash tables resolve collisions?
KnownRevise
Collision resolution is the process used when two different keys produce the same hash-table index. The two main approaches are separate chaining, which stores colliding entries together, and open addressing, which searches for another available slot using a probing sequence.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
Core Idea
A hash table computes an index from a key using a hash function:
index = hash(key) % number_of_slots
A collision occurs when different keys produce the same index—for example, "cat" and "act" might have identical deterministic hash values.
Hash tables are generally classified as:
Non-linear: keys are mapped to locations rather than accessed by position.
Dynamic: implementations can grow as more entries are inserted.
Non-primitive: they combine arrays, linked nodes, hashing, and other lower-level structures.
Partly contiguous: buckets or slots are contiguous, while chained nodes may be scattered in memory.
Conceptually, a hash table is an abstract data type supporting lookup, insertion, and deletion. Its implementation determines how collisions are resolved.
A simple chaining example proceeds as follows:
"cat" hashes to bucket 1 and is inserted there.
"act" also hashes to bucket 1.
Both entries are stored in that bucket, so retrieving "act" follows the chain to the matching node.
Resolution Strategies
Separate chaining:
Each bucket contains a linked list, dynamic array, or another collection.
Insertion places a new entry in the corresponding bucket.
Lookup and deletion traverse that bucket.
It tolerates a higher load factor, but each entry requires additional node or pointer overhead.
Nodes are not necessarily contiguous in memory, which can reduce cache efficiency.
Open addressing:
All entries remain in one array.
If a slot is occupied, the algorithm probes another slot.
Common probe sequences include linear probing, quadratic probing, and double hashing.
Linear probing is simple but can create clusters of occupied slots.
Deletion requires care: simply clearing a slot can break later lookup chains, so implementations use tombstones or backward-shift deletion.
It usually has lower per-entry overhead but requires a controlled load factor.
In Python, dict and set provide production-quality hash-table implementations. Their internal collision-handling details are implementation-specific and should not be relied upon by application code.
Complexity
The following costs assume a good hash function, a controlled load factor, and n stored entries.
Operation
Separate Chaining
Open Addressing
Access by numeric index
Not supported
Not supported
Search/access by key
O(1) best and average; O(n) worst
O(1) best and average; O(n) worst
Insert
O(1) best and average; O(n) worst
O(1) best and average; O(n) worst
Delete
O(1) best and average; O(n) worst
O(1) best and average; O(n) worst
Extra memory per element
O(1) for a node and pointer, plus bucket array
O(1) slot, including unused-slot overhead
Key order preserved
No global ordering
No global ordering
Both approaches require O(n) total space when the number of buckets or slots is proportional to the number of entries. The input is not copied, and neither implementation uses a recursion stack. A normal operation needs O(1) auxiliary space; resizing a dynamic table can temporarily require O(n) extra space.
Worst-case operations become O(n) when every key maps to the same bucket or probe sequence encounters a large cluster.
Common Pitfalls
Assuming collisions are impossible because hashing is used.
Ignoring the O(n) worst case caused by poor distribution or adversarial keys.
Allowing the load factor to grow too large, which harms lookup and insertion performance.
Removing an open-addressed slot without preserving the validity of later probe chains.
Using mutable keys; a key’s hash or equality must not change while it is stored.
Confusing dict insertion order with sorted key order.
Overlooking pointer and allocation overhead in separately chained implementations.
Interviewers commonly follow up with questions about load factors, resizing, worst-case behavior, deletion in open addressing, or how to make a hash table thread-safe.
43
What makes a hash function effective for hashing?
KnownRevise
A hash function converts an input key into a fixed-size value called a hash code, which is typically used to locate data in a hash table. A good hash function is deterministic, fast, and distributes similar or different keys evenly enough to minimize collisions.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
Purpose and Operation
A hash table uses the hash value as an index or part of an index into an underlying array. When an element is inserted, its key is hashed, and the resulting value determines where the element is stored.
Lookup, insertion, and deletion repeat the same process:
Compute the hash of the key.
Convert it into a valid array index.
Compare the stored key with the requested key.
If they differ, follow the table’s collision-resolution strategy.
Equal keys must always produce equal hash values during the lifetime of the hash table. Different keys may also produce the same hash; this is called a hash collision and is unavoidable when the set of possible inputs is larger than the range of hash values.
Properties of a Good Hash Function
Deterministic: The same key produces the same result every time within a given execution or data-structure lifecycle.
Fast: Hashing should require little work relative to the cost of accessing the hash table.
Well-distributed: Keys should be spread across the available buckets, reducing clustering and long collision chains.
Consistent: Equal keys must have equal hashes.
Total over the domain: Every key supported by the application should receive a valid hash or a defined fallback value.
Low overhead: The hash value usually has a fixed size, making the additional memory per stored item constant.
Hard to exploit when security matters: Ordinary hashing assumes the input is trusted; adversarial and cryptographic use require stronger properties.
The exact properties depend on the context. A general-purpose hash-table hash prioritizes speed and distribution, while a cryptographic hash function prioritizes collision resistance and resistance to preimage attacks.
Example Walkthrough
Suppose a hash table has eight buckets and uses:
bucket_index = hash(key) mod 8
Key
Simplified hash value
Bucket index
"apple"
19
3
"banana"
25
1
"cherry"
6
6
The keys are placed in buckets 3, 1, and 6. If two keys produced the same index, the hash table would need collision handling, such as chaining with linked lists or open addressing with probing.
Complexity
For a key of length k, computing a hash generally takes O(k) time because the function examines the input. For fixed-size machine-word keys, this is treated as O(1).
Operation
Average time
Worst-case time
Reason
Compute a hash
O(k)
O(k)
The key must be processed
Hash-table lookup
O(1)
O(n)
Average lookup finds an available bucket; worst case examines a collision chain
Hash-table insertion
O(1)
O(n)
Resizing and collision handling can increase the cost
Hash-table deletion
O(1)
O(n)
The key may belong to a long collision chain
Here, n is the number of stored keys. A hash table uses O(n) additional space for its buckets and entries, excluding the input keys and values. A hash function itself uses O(1) auxiliary space when it returns one fixed-size value, excluding its input.
Choosing Between Ordinary and Cryptographic Hashing
Use case
Recommended approach
Important property
Dictionary or set implementation
Built-in runtime hash
Fast and well-distributed
File checksum or fingerprint
Cryptographic hash such as SHA-256
Collision and preimage resistance
Password storage
Password hashing such as Argon2 or bcrypt
Slow, salted, and deliberately expensive
Message authentication
HMAC or another authenticated construction
Detects tampering with a secret key
Partitioning data into buckets
General-purpose hash
Uniform distribution and predictable performance
Python’s built-in hash() is suitable for normal in-memory dictionary and set usage. However, its numeric values for objects such as strings are not guaranteed to remain stable across separate Python processes, so they should not be used as persistent identifiers.
Common Pitfalls and Interview Tips
Assuming a good hash function eliminates collisions; it only reduces their frequency.
Using a slow or poorly distributed hash because it appears cryptographically strong.
Relying on Python’s hash() values as permanent database keys or file identifiers.
Assuming average O(1) hash-table performance always applies; adversarial inputs or poor distribution can degrade it to O(n).
Comparing hash values without also comparing the original keys, because unequal keys may legitimately share a hash.
Confusing a checksum with a secure password-storage mechanism.
Ignoring the cost of hashing very large keys, such as reading an entire file into memory before hashing it.
Interview Tips: Explain that a hash function should be deterministic, fast, and distribute keys uniformly. Mention that collisions are unavoidable and must be handled by the hash table, not assumed away by the hash function. Common follow-up questions ask about collision resolution, worst-case performance, rehashing, thread safety, and how cryptographic hashing differs from hash-table hashing.
44
How would you design an LRU cache data structure?
KnownRevise
An LRU cache uses a hash table for constant-time lookup and a doubly linked list to track usage order. Every access moves an entry to the front, while inserting beyond capacity evicts the least recently used entry from the back.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
Design
The LRU behavior is an abstract data type; its standard implementation combines two structures:
Hash table: Maps each key to its cache node, allowing fast access to the corresponding linked-list node.
Doubly linked list: Stores entries from most recently used to least recently used.
Get operation: Look up the entry and move it to the front.
Put operation: Insert or update the entry, move it to the front, and evict the back if capacity is exceeded.
Why a doubly linked list: A node can be removed in O(1) time when its pointer is known; singly linked lists would require finding the predecessor.
Real-world use: LRU caches are used in web browsers to cache recently accessed pages, in database buffer pools to keep hot pages in memory, and in CDNs to serve frequently requested content.
For capacity 2, the operations are:
Put A and B: order is B, A.
Get A: order becomes A, B.
Put C: B is evicted, leaving A, C.
Get B: returns a cache miss.
Complexity
Implementation
Access
Search
Insert or update
Delete or eviction
Extra space per entry
Order kept
Hash table + doubly linked list
O(1) average, O(n) worst
O(1) average, O(n) worst
O(1) average, O(n) worst
O(1) average, O(n) worst
O(1)
LRU order
collections.OrderedDict
O(1) average, O(n) worst
O(1) average, O(n) worst
O(1) average, O(n) worst
O(1) average, O(n) worst
O(1)
LRU order when updated explicitly
The worst-case O(n) cost comes from hash collisions. Linked-list operations remain O(1).
For a cache containing c entries:
Time: O(1) average per get and put, including eviction.
Space: O(c) for the hash table and linked-list nodes.
Recursion stack: O(1) because the implementation is iterative.
Input accounting: The cached entries are the data structure itself, so there is no separate input copy.
Python Alternatives
collections.OrderedDict implements the same general design. move_to_end(key) marks an entry as recently used, while popitem(last=False) removes the least recently used entry.
functools.lru_cache is also useful for memoizing function calls, but it is a decorator rather than a general-purpose cache object.
Code Example
from __future__ import annotations
from typing import Generic, TypeVar
K = TypeVar("K")
V = TypeVar("V")
class _Node(Generic[K, V]):
__slots__ = ("key", "value", "prev", "next")
def __init__(self, key: K, value: V) -> None:
self.key = key
self.value = value
self.prev: _Node[K, V] | None = None
self.next: _Node[K, V] | None = None
class LRUCache(Generic[K, V]):
def __init__(self, capacity: int) -> None:
"""Create an empty LRU cache with a fixed positive capacity."""
if capacity <= 0:
raise ValueError("capacity must be greater than zero")
self._capacity = capacity
self._entries: dict[K, _Node[K, V]] = {}
self._head: _Node[K, V] | None = None # Most recently used
self._tail: _Node[K, V] | None = None # Least recently used
def get(self, key: K) -> V | None:
"""Return a value and mark its key as recently used."""
try:
node = self._entries[key]
except KeyError:
return None
self._remove(node)
self._add_to_front(node)
return node.value
def put(self, key: K, value: V) -> None:
"""Insert or update a value, evicting the LRU entry if necessary."""
if key in self._entries:
node = self._entries[key]
node.value = value
self._remove(node)
self._add_to_front(node)
return
node = _Node(key, value)
self._entries[key] = node
self._add_to_front(node)
if len(self._entries) > self._capacity:
lru_node = self._tail
if lru_node is not None:
del self._entries[lru_node.key]
self._remove(lru_node)
def _add_to_front(self, node: _Node[K, V]) -> None:
"""Insert a node at the most-recently-used end."""
node.prev = None
node.next = self._head
if self._head is None:
self._tail = node
else:
self._head.prev = node
self._head = node
def _remove(self, node: _Node[K, V]) -> None:
"""Detach a known node from the linked list."""
if node.prev is None:
self._head = node.next
else:
node.prev.next = node.next
if node.next is None:
self._tail = node.prev
else:
node.next.prev = node.prev
node.prev = None
node.next = None
def example() -> None:
cache: LRUCache[str, int] = LRUCache(capacity=2)
cache.put("a", 1)
cache.put("b", 2)
print(cache.get("a")) # 1; refreshes "a"
cache.put("c", 3) # evicts "b"
print(cache.get("b")) # None
print(cache.get("c")) # 3
if __name__ == "__main__":
example()
# Expected output:
# 1
# None
# 3
Common Pitfalls
Forgetting to refresh recency: Updating an existing key must move it to the front.
Using a Python list as a queue: pop(0) is O(n); use collections.deque or a linked list.
Leaving stale entries: Remove an evicted node from both the hash table and linked list.
Ignoring None values: This API returns None for both a miss and a stored None; use a sentinel or membership method when that distinction matters.
Assuming thread safety: Concurrent access requires locking or a thread-safe cache implementation.
Common follow-up: Discuss what changes when the cache must be thread-safe, distributed, or larger than available memory.
45
What are the trade-offs between an array and a hash table?
KnownRevise
The choice between an array and a hash table depends on whether you need ordered, index-based access or fast key-value lookups. Arrays store elements in contiguous memory for predictable O(1) access, while hash tables use a hash function to achieve average O(1) operations but lack inherent ordering.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
Operation Costs
The primary difference lies in how operations scale with the number of elements, n. Arrays provide direct access via index, but inserting or deleting in the middle requires shifting elements. Hash tables avoid shifting but can degrade to O(n) if many keys collide in the same bucket.
Operation
Array
Hash Table
Access
O(1)
O(1) avg / O(n) worst
Search
O(n)
O(1) avg / O(n) worst
Insert
O(1) amortized append / O(n) middle
O(1) avg
Delete
O(n)
O(1) avg
Space
O(n)
O(n) + overhead
Extra memory per element
None (contiguous)
Pointer/bucket overhead
Order preserved
Yes (insertion order)
Not inherently (Python 3.7+ dict guarantees insertion order as a language feature)
Memory and Cache Behavior
Arrays are contiguous and cache-friendly, using minimal overhead per element. This makes them efficient for iterating over large datasets. Hash tables scatter memory and require extra space for buckets and pointers, often holding more than one item per bucket due to the load factor. Space complexity for arrays is O(n) for n elements, while hash tables are O(n) plus the overhead of the hash table implementation.
Python Implementation Mapping
In Python 3.12, list is a dynamic array, and dict is a hash table that preserves insertion order. Arrays are static or dynamic sequences, while hash tables are implementations of the Map abstract data type.
When to Choose Which
Array: Use when you need fast random access by index or to store fixed-size, ordered data like lookup tables or images. The trade-off is that insertion and deletion in the middle are slow.
Hash Table: Use when you need fast membership testing or key-value mapping, such as caches, indexes, or counting frequencies. The trade-off is that you lose ordering (except Python’s ordered dict) and pay extra memory overhead.
Common Pitfalls
Choosing by habit: Select a structure based on the operations you need, not familiarity.
Ignoring worst cases: Hash collisions can degrade performance to O(n); unbalanced BSTs (if used) also degrade.
Using a list as a queue: list.pop(0) is O(n); use collections.deque for O(1) pops.
Forgetting memory overhead: Hash tables use significantly more memory per element than arrays.
Follow-up questions: Interviewers often ask “how would you make it thread-safe?” or “what if the data doesn’t fit in memory?”.
46
What is a hash set used for in common applications?
KnownRevise
A hash set is a collection that stores unique elements and provides fast membership checks, typically in average (O(1)) time. It is commonly used for removing duplicates, tracking previously encountered items, and testing whether a value belongs to a group.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
How It Works
A hash set uses a hash function to map each element to a location in an internal table:
Compute the element’s hash value.
Use that value to locate the appropriate bucket or slot.
Compare the stored element with the requested element when a hash collision occurs.
The set contains at most one copy of each element, so duplicate insertions do not create additional entries. Hash sets are dynamic containers: they can grow as elements are added.
Hash sets are generally classified as dynamic, non-linear structures. They store references to non-primitive objects and use an internal table rather than a contiguous linear sequence. Memory is not stored in one predictable contiguous block, so cache locality is usually worse than with arrays.
Common Uses
Duplicate removal: Store only unique values, such as distinct customer IDs.
Seen tracking: Record items already processed during a traversal or algorithm.
Membership testing: Check whether a user, permission, word, or coordinate already exists.
Set operations: Find intersections, unions, and differences between groups of values.
Tagging and indexing: Track labels, categories, or identifiers associated with an entity.
Frequency-related tasks: Identify repeated values with a set, or use a frequency map such as collections.Counter when counts are required.
A hash set is a good choice when the main operation is checking whether a value exists, rather than retrieving elements by position or maintaining a meaningful order.
Complexity and Python Mapping
In Python 3.12, set and frozenset implement set behavior using hash tables. set is mutable; frozenset is immutable and can be used as a dictionary key or placed in another set.
Operation
Hash set
Python list
Access/search
No positional access; key lookup is (O(1)) average, (O(n)) worst case
Index access is (O(1)); value search is (O(n))
Insert
(O(1)) average, (O(n)) worst case
Middle insertion is (O(n)); append is (O(1)) amortized
Delete
(O(1)) average, (O(n)) worst case by key
Middle deletion is (O(n))
Extra memory per element
(O(1)) average implementation overhead, including hash-table slack
(O(1)) pointer per element, plus list slack
Order
No meaningful or stable order guarantee
Preserves insertion order
The worst-case costs arise when many elements collide in the hash table. In practice, good hash distributions usually keep operations close to average-case performance.
For (n) elements:
Time: Building or processing a set is (O(n)) average and (O(n^2)) worst case under pathological collision behavior.
Space: (O(n)) additional space for the hash table. The input collection is not counted as part of the set’s own storage.
Recursion stack: No recursion stack is used by the set operations themselves.
Code Example
This function finds values that occur more than once by tracking values already seen:
def find_duplicates(values: list[int]) -> list[int]:
"""Return the values that occur more than once, in sorted order."""
seen: set[int] = set()
duplicates: set[int] = set()
for value in values:
if value in seen:
duplicates.add(value)
else:
seen.add(value)
return sorted(duplicates)
# Example:
# find_duplicates([1, -1, 2, 1, -1, 3]) # [-1, 1]
The function handles an empty list, a single element, duplicates, and negative numbers. It uses additional (O(n)) space and runs in (O(n)) average time. If the original order is important, use dict.fromkeys(values) instead of relying on set iteration order.
Common Pitfalls
Choosing a list by habit: Use a set when membership testing dominates; use a list when order or positional access matters.
Ignoring worst cases: Hash collisions can degrade operations from average (O(1)) to (O(n)).
Expecting sorted output: A set does not maintain sorted order. Sort the result explicitly when needed.
Using a set for counts: A set identifies uniqueness, while a frequency map is appropriate for exact counts.
Forgetting memory overhead: Hash sets store references plus hash-table metadata, so they can use more memory than compact arrays.
Mutating while iterating: Avoid changing a set’s size during iteration; create a copy or collect changes first.
Interview Tips
Explain that a hash set is an implementation of the set abstraction backed by hashing. A common follow-up is to compare it with a balanced binary search tree: a hash set favors average (O(1)) lookup without ordering, while a tree provides ordered traversal and worst-case (O(log n)) operations.
47
How do you count word frequencies in a long text?
KnownRevise
Hash-table word counting processes the text once and stores each word’s frequency in a dictionary. In Python, dict is a hash-table implementation, providing constant-time average updates even for large texts.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
How It Works
Scan the text with a tokenizer that identifies words while ignoring punctuation.
Normalize each word, usually with casefold() for case-insensitive counting.
Update that word’s count in a dict.
Return the dictionary of unique words and their frequencies.
This approach is useful for search indexing, document analysis, spam detection, and identifying the most common terms in a document.
Unlike a sorted list, a dictionary preserves first-occurrence order rather than lexical order. If alphabetical output is required, sort the keys afterward, adding O(U log U) time for U unique words.
Operation Costs
Let n be the number of characters, W the number of words, and U the number of unique words.
Structure
Access
Search
Insert
Delete
Extra Memory per Element
Order
dict hash table
Avg O(1), worst O(U)
Avg O(1), worst O(U)
Avg O(1), worst O(U)
Avg O(1), worst O(U)
One key/count entry plus hash-table overhead
Insertion order
Sorted list of pairs
O(1)
O(log U)
O(U) due to shifting
O(U) due to shifting
One pair plus list pointer
Lexical order
A dictionary is preferable when counting is the priority. A sorted list may be better when ordered iteration is required throughout processing.
Complexity
Best-case time:O(n) because every character must be scanned.
Average-case time:O(n) because tokenization is linear and dictionary updates take O(1) on average.
Worst-case time:O(n + WU) under the standard hash-table worst-case assumption that severe hash collisions make each update scan all U entries.
Space:O(U + D), where D is the total size of the distinct normalized words. This excludes the original input but includes the returned dictionary. There is no recursion stack.
Example Walkthrough
For "Hello hello world!":
Token
Normalized Token
Frequency
Hello
hello
1
hello
hello
2
world
world
1
The result is {"hello": 2, "world": 1}.
Code Example
import re
WORD_PATTERN = re.compile(r"[^\W_]+(?:['’][^\W_]+)*")
def count_word_frequencies(text: str) -> dict[str, int]:
"""Count case-insensitive frequencies of Unicode word tokens."""
frequencies: dict[str, int] = {}
for match in WORD_PATTERN.finditer(text):
word = match.group(0).casefold()
frequencies[word] = frequencies.get(word, 0) + 1
return frequencies
result = count_word_frequencies("Hello hello world!")
print(result)
# {'hello': 2, 'world': 1}
finditer() processes matches incrementally instead of first creating a list containing every token. The regular expression recognizes Unicode letters and digits, permits internal apostrophes, and excludes underscores and standalone punctuation.
Common Pitfalls
Unclear tokenization: Decide whether punctuation, contractions, hyphens, numbers, and underscores count as words. For example, "state-of-the-art" produces four tokens, while "don't" remains one token.
Case handling:casefold() is more suitable than lower() for international text, although tokenization policy still affects results.
Empty input: The function correctly returns {}. A one-word input and repeated words are also handled naturally.
Signs in numbers: The shown tokenizer treats "-42" as "42". Preserve signs with a different pattern if they are semantically important.
Repeated linear scans: Calling list.count() for every word can take O(WU) time; a dictionary reduces average counting work to O(n).
Memory limits: For text too large to fit comfortably in memory, process it in chunks while carrying the frequency dictionary between chunks.
Common follow-up questions: Interviewers may ask about producing the top k words, sorting output, processing a stream, reducing memory, or making concurrent updates thread-safe.
48
How does a high load factor affect hash table performance?
KnownRevise
A high load factor in a hash table means many entries occupy the available slots, increasing collisions and making operations slower. The load factor is usually defined as α = n / m, where n is the number of stored entries and m is the number of slots or buckets.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
Load Factor Meaning
A load factor of 0.5 means the table is half full, while 0.9 means it is 90% full. The exact internal representation varies by implementation, but the concern remains the same: as occupancy increases, finding an empty or matching slot becomes harder.
Hash table: A non-linear structure implemented with hashing, typically using buckets, linked entries, or an open-addressed array.
Typical target: Many implementations keep the load factor below a threshold such as 0.75, though the precise value depends on the collision strategy.
Python dict and set: Python manages their internal occupancy and resizing automatically; users cannot directly set their load factor.
Performance Implications
Operation
Best case
Average case with bounded load factor
Worst case
Search
O(1)
O(1) expected
O(n)
Insert
O(1)
O(1) expected
O(n)
Delete
O(1)
O(1) expected
O(n)
These are expected complexities when hashing is well distributed and the load factor remains controlled. A high load factor does not necessarily change the formal average-case Big-O by itself, but it increases the expected number of probes or comparisons per operation.
The effect depends on the collision-resolution strategy:
Open addressing: Colliding entries are placed in other slots. With linear probing, a high load factor causes clustering, where consecutive occupied slots increase probe lengths.
Separate chaining: Each bucket holds a collection of colliding entries. Higher occupancy increases average bucket length and therefore lookup work.
Poor hash functions: A high load factor amplifies the damage caused by uneven key distribution because several keys continue competing for the same locations.
Memory and Resizing Trade-Off
A higher load factor can use memory more efficiently because fewer buckets are needed. However, the performance gain must be weighed against slower searches and insertions.
For a table with n entries and m buckets:
Total space: O(n + m).
Space excluding the stored input and entries: O(m) for the bucket or slot array.
When m is proportional to n, total space is O(n).
There is no recursion stack.
Many implementations resize or rehash the table when a threshold is exceeded. Resizing typically costs O(n) time because the entries must be rehashed or redistributed, but spreading that cost across growth operations makes insertion amortized O(1).
Practical Consequences
Latency: Average search and insertion times increase because more collisions must be resolved.
Cache behavior: Open-addressed tables become less cache-friendly as clusters grow.
Worst-case risk: Poor hashing combined with high occupancy can make common operations approach O(n).
Throughput: Systems with many writes may spend more time probing or resizing.
Memory efficiency: A high load factor reduces per-entry slot overhead but may increase the cost of every operation.
For example, a database index or in-memory cache generally prefers a moderate load factor because predictable lookup latency matters more than minimizing a small amount of unused memory.
Common Pitfalls
Treating expected O(1) as guaranteed: hash tables have an O(n) worst case.
Assuming all hash tables use the same collision strategy or resizing policy.
Ignoring hash quality; a low load factor cannot fully compensate for a severely skewed hash function.
Comparing capacity without accounting for per-entry overhead, such as chaining pointers or unused open-addressed slots.
Using list.pop(0) or list.insert(0, value) as queue operations: each is O(n) because elements must be shifted. Use collections.deque for O(1) queue operations.
Interview Tips
Explain that a high load factor usually trades memory efficiency for slower operations, mainly because collisions and probe sequences increase. A strong follow-up answer discusses resizing thresholds, amortization, clustering in open addressing, and the difference between expected and worst-case behavior.
49
How does hash table rehashing work and when is it triggered?
KnownRevise
Rehashing rebuilds a hash table after changing its bucket count or hash-index calculation so that existing entries are redistributed among the new buckets. It is normally triggered when the table’s load factor exceeds a configured threshold.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
How Rehashing Works
A hash table maps keys to buckets using something like hash(key) % bucket_count. When the bucket count changes, every existing key must be assigned to a potentially different bucket.
The general process is:
Allocate a new bucket array.
Recalculate each entry’s bucket index using the new capacity.
A high load factor means many entries occupy the same buckets, increasing search time and collision overhead.
When Rehashing Is Triggered
Growth: Before an insertion would make the load factor exceed the maximum allowed value.
Hash-function change: When the hashing policy changes, all entries must be indexed again.
Optional shrinking: Some implementations rebuild a sparsely populated table after many deletions. Python dict, however, does not automatically shrink merely because entries are deleted.
Periodic cleanup: Rebuilding can remove accumulated tombstone markers in implementations that use open addressing.
The exact threshold and growth factor are implementation details. Python’s dict manages its own sizing, so programs should not depend on its precise internal capacity or threshold.
Complexity
Let n be the number of entries, m the number of buckets, and L the maximum load factor.
Operation
Best
Average
Worst
Reason
Rehashing
O(n + m)
O(n + m)
O(n + m)
Initializes buckets and redistributes entries
Insert
O(1)
O(1) amortized
O(n)
Resizing may be required; collisions may form one chain
Search
O(1)
O(1)
O(n)
Collisions may place all entries in one bucket
Delete
O(1)
O(1)
O(n)
The target may be behind many colliding entries
With geometric growth and a constant maximum load factor, m = O(n), so a rehash costs O(n). Its cost is amortized across insertions, although one individual insertion can take O(n).
Total table space is O(n + m). During rehash, O(n + m) additional space may be required because old and new buckets coexist temporarily. The recursion stack is O(1) because the implementation is iterative. The stored input is included in the total table-space calculation.
Worked Example
With four buckets and a maximum load factor of 0.75:
Inserting three entries produces a load factor of 3 / 4 = 0.75.
Inserting a fourth entry would produce 4 / 4 = 1.0.
The table first doubles to eight buckets.
The three existing entries are redistributed.
The fourth entry is inserted, producing a load factor of 4 / 8 = 0.5.
Exact bucket positions depend on the hash values and possible collisions.
Code Example
The following educational implementation uses separate chaining and implements resizing directly rather than using Python’s built-in dict.
from collections.abc import Hashable
from typing import Generic, TypeVar
K = TypeVar("K", bound=Hashable)
V = TypeVar("V")
class HashTable(Generic[K, V]):
"""Hash table with separate chaining and automatic resizing."""
def __init__(self, initial_capacity: int = 8,
max_load_factor: float = 0.75) -> None:
if initial_capacity < 1:
raise ValueError("initial_capacity must be positive")
if not 0 < max_load_factor <= 1:
raise ValueError("max_load_factor must be in (0, 1]")
# Round capacity up to a power of two for simple doubling.
self._capacity = 1 << (initial_capacity - 1).bit_length()
self._max_load_factor = max_load_factor
self._buckets: list[list[tuple[K, V]]] = [
[] for _ in range(self._capacity)
]
self._size = 0
@property
def capacity(self) -> int:
return self._capacity
def _index(self, key: K) -> int:
# Python's modulo also handles negative hash values.
return hash(key) % self._capacity
def _rehash(self, new_capacity: int) -> None:
old_buckets = self._buckets
self._capacity = new_capacity
self._buckets = [[] for _ in range(new_capacity)]
for bucket in old_buckets:
for entry in bucket:
key, _ = entry
new_index = hash(key) % self._capacity
self._buckets[new_index].append(entry)
def put(self, key: K, value: V) -> None:
index = self._index(key)
# Update an existing key without increasing the entry count.
for position, (stored_key, _) in enumerate(self._buckets[index]):
if stored_key == key:
self._buckets[index][position] = (key, value)
return
projected_load = (self._size + 1) / self._capacity
if projected_load > self._max_load_factor:
self._rehash(self._capacity * 2)
index = self._index(key)
self._buckets[index].append((key, value))
self._size += 1
def get(self, key: K, default: V | None = None) -> V | None:
for stored_key, value in self._buckets[self._index(key)]:
if stored_key == key:
return value
return default
def __len__(self) -> int:
return self._size
table = HashTable[str, int](initial_capacity=4)
table.put("alpha", 1)
table.put("beta", 2)
table.put("gamma", 3)
table.put("delta", 4)
print(table.get("alpha"), table.get("delta"), len(table), table.capacity)
# Expected output: 1 4 4 8
Common Pitfalls
Ignoring collisions: Average O(1) operations become O(n) when many keys share a bucket.
Assuming an order: A hash table does not inherently preserve key order; Python dict separately guarantees insertion order.
Rehashing unnecessarily: Duplicate-key updates should not increase the size or trigger resizing.
Ignoring temporary memory: Rebuilding temporarily holds both old and new bucket arrays.
Assuming sorted input matters: Hash-table performance depends on hashing and collisions, not whether keys arrive in sorted order.
Overlooking edge cases: Empty tables, one element, duplicate keys, and negative keys are all valid inputs; graphs and cycles do not apply to this structure.
Interview follow-up: Interviewers commonly ask how you would make resizing thread-safe, handle collisions, or reduce the O(n) cost of one large rehash.
50
Hash tables vs balanced trees performance comparison
KnownRevise
The main performance difference is that hash tables offer O(1) average-case operations but degrade to O(n) worst-case, while balanced trees guarantee O(log n) for all operations.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
Operation Costs
Aspect
Hash Table
Balanced Tree
Access (avg/worst)
O(1) / O(n)
O(log n) / O(log n)
Search (avg/worst)
O(1) / O(n)
O(log n) / O(log n)
Insert (avg/worst)
O(1) / O(n)
O(log n) / O(log n)
Delete (avg/worst)
O(1) / O(n)
O(log n) / O(log n)
Extra memory per element
High (load factor, buckets)
Moderate (pointers per node)
Order kept?
No (except insertion order in Python 3.7+)
Yes (in-order traversal)
Hash table worst cases occur with many collisions; balanced trees stay height-balanced, so their worst case is O(log n).
Memory and Cache Behaviour
Hash tables use contiguous arrays for buckets, making them cache-friendly. Extra memory per element is high due to load factor (typically 0.7-0.8) and bucket overhead.
Balanced trees use nodes with pointers, scattering memory. Extra memory per element is moderate (two pointers per node for BST, more for AVL or red-black).
Arrays are contiguous and cache-friendly; linked structures add a pointer per node and scatter memory.
Order and Use Cases
Hash tables do not keep order (except Python 3.7+ dict keeps insertion order). Use for caches, indexes, counting.
Balanced trees keep sorted order via in-order traversal. Use for sorted data, database indexes (B+ trees).
Choosing Between Them
Choose hash table when fast lookups are needed and order doesn’t matter.
Choose balanced tree when you need ordered data, range queries, or guaranteed worst-case performance.
Common Pitfalls
Choosing a structure by habit instead of by the operations needed.
Follow-up: “How would you make it thread-safe?” or “What if the data doesn’t fit in memory?”
51
How are graphs defined and represented in computer memory?
KnownRevise
A graph is a non-linear data structure that models relationships between objects using vertices and edges. In memory, it is usually represented as an adjacency list or an adjacency matrix, depending on graph density and the operations required.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
Core Definition
A graph is commonly written as G = (V, E):
Vertices V: Represent entities such as cities, users, files, or tasks.
Edges E: Represent connections between vertices.
Undirected graph: An edge connects two vertices symmetrically, such as a friendship.
Directed graph: An edge has a direction, such as a one-way dependency.
Weighted graph: Each edge carries a cost, distance, capacity, or another value.
A graph is an abstract model supporting operations such as adding vertices, adding edges, finding neighbors, and traversing paths. Its implementation determines how those relationships are stored.
Memory Representations
Adjacency list: Stores the neighbors of each vertex, commonly as dict[int, list[int]]. A weighted edge can be stored as (neighbor, weight). This representation is space-efficient for sparse graphs and supports neighbor traversal efficiently. Python has no built-in graph type, but dictionaries and lists are its usual building blocks.
Adjacency matrix: Uses a two-dimensional array indexed by vertex positions. matrix[u][v] stores edge presence or a weight. It supports constant-time edge lookup but requires quadratic space. Packed numeric rows are contiguous and cache-friendly, while Python list-of-lists storage has object and reference overhead.
Example walkthrough:
Start with vertices A, B, and C.
For undirected edges A-B and B-C, store B in A‘s list and A in B’s list.
Store C in B‘s list and B in C’s list.
In a three-by-three matrix, mark (A, B), (B, A), (B, C), and (C, B) as edges.
Complexity Comparison
The costs below use V for vertices and E for edges. Access means finding a vertex’s record, while edge search means checking whether u and v are connected.
Representation
Access
Search edge
Insert edge
Delete edge
Memory and extra storage per item
Order
Adjacency list with lists
O(1) average, O(V) worst from dictionary collisions
O(deg(u)) average and worst by scanning
O(1) amortized average; O(V) worst from a dictionary collision, while neighbor-list resizing costs O(deg(u))
O(deg(u)) average and worst by finding and removing the neighbor
O(V + E) total; O(1) metadata per vertex and one neighbor entry per directed edge
Neighbor insertion order
Adjacency list with sets
O(1) average, O(V) worst from dictionary collisions
O(1) average, O(deg(u)) worst from set collisions
O(1) amortized average; O(V + deg(u)) worst from dictionary and set collisions or resizing
O(1) average; O(V + deg(u)) worst
O(V + E) total; one set entry per directed edge
No defined neighbor order
Adjacency matrix
O(1) with direct vertex indices
O(1) best, average, and worst
O(1) in an allocated matrix; packed resizing may cost O(V²)
O(1) by clearing a cell
O(V²) total; one cell per vertex pair, including absent edges
Vertex-index traversal order
An undirected adjacency list commonly stores each edge twice, once from each endpoint.
Adjacency-list traversal: O(V + E) time because it visits each vertex and stored edge.
Adjacency-matrix traversal: O(V²) time because each vertex may require scanning a complete row.
The representation costs above count the graph input. A standard traversal additionally needs O(V) space for visited markers and its queue or stack; recursive DFS also has an O(V) call stack. Excluding the input, traversal auxiliary space is therefore O(V). Including it, totals are O(V + E) for an adjacency list and O(V²) for a matrix. Copying the graph into another representation adds O(V + E) or O(V²) space, respectively, excluding the original.
Choosing a Representation
Choose an adjacency list for sparse graphs, pathfinding, dependency graphs, social networks, and general traversal. Its storage grows with the actual connections.
Choose an adjacency matrix for dense graphs, frequent edge-existence checks, or algorithms based on matrix operations. Constant-time lookup comes at the cost of quadratic space.
Adjacency lists are usually easier to update because adding or removing a vertex does not require expanding a matrix.
Real-world examples include road maps, social networks, dependency graphs, and state-transition systems.
Edge Cases
An empty graph has no vertices or edges.
A graph with one vertex may have no edges or a self-loop.
A simple graph does not allow parallel edges; a multigraph may represent distinct relationships with parallel edges.
Negative edge weights are valid for some algorithms but make Dijkstra’s shortest-path algorithm invalid.
Disconnected graphs require traversal to start from multiple vertices to visit every component.
Cycles require a visited set or equivalent marking to prevent repeated traversal.
Common Pitfalls
Choose a representation based on the required operations, not habit.
Do not ignore the adjacency matrix’s O(V²) space requirement for large sparse graphs.
Do not use a Python list with pop(0) as a queue; use collections.deque.popleft() for O(1) removal.
Do not forget the visited set when cycles are possible.
Preserve vertex and edge weights when they affect the algorithm.
Interviewers commonly ask about duplicate edges, self-loops, disconnected components, or graphs too large to fit in memory.
52
How do you implement DFS and BFS on a graph?
KnownRevise
Depth-first search (DFS) explores one path as deeply as possible before backtracking, while breadth-first search (BFS) explores vertices level by level. Both can traverse a graph represented as an adjacency list using a visited set to prevent repeated processing.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
Core Ideas
DFS: Use a stack to store pending vertices. Remove the most recently added vertex and explore its neighbors.
BFS: Use a queue to store pending vertices. Remove the oldest vertex and explore its neighbors.
Visited set: Mark a vertex when it is added to the stack or queue so cycles, duplicate edges, and repeated references do not cause infinite traversal.
Iterative DFS avoids Python’s recursion-depth limit, which is approximately 1,000 nested calls by default.
In Python 3.12, collections.deque provides efficient popleft() for BFS. A normal list is suitable as a stack because append() and pop() operate at its end.
Complexity
Let V be the number of reachable vertices and E the number of reachable edges.
Shortest paths in unweighted graphs, level-order processing
Time reason: Each reachable vertex is processed once, and each adjacency-list edge is examined once.
Space reason: The visited set, output, and DFS stack or BFS queue each use at most O(V) space.
The visited set has O(1) average-time operations. Under pathological hash collisions, its strict worst case can degrade to O(V) per operation; an indexed visited array provides deterministic O(V + E) traversal when vertices have contiguous integer identifiers.
Auxiliary space excludes the input graph and includes the stack or queue. Including an adjacency-list graph, total space is O(V + E).
Code Example
from collections import deque
from collections.abc import Hashable, Mapping, Sequence
Graph = Mapping[Hashable, Sequence[Hashable]]
def dfs(graph: Graph, start: Hashable) -> list[Hashable]:
"""Return DFS visit order for the component reachable from start."""
if start not in graph:
return []
visited: set[Hashable] = {start}
stack: list[Hashable] = [start]
order: list[Hashable] = []
while stack:
vertex = stack.pop()
order.append(vertex)
# Reverse pushing so the first listed neighbor is explored first.
for neighbor in reversed(graph[vertex]):
if neighbor not in visited:
visited.add(neighbor)
stack.append(neighbor)
return order
def bfs(graph: Graph, start: Hashable) -> list[Hashable]:
"""Return BFS visit order for the component reachable from start."""
if start not in graph:
return []
visited: set[Hashable] = {start}
queue: deque[Hashable] = deque([start])
order: list[Hashable] = []
while queue:
vertex = queue.popleft()
order.append(vertex)
for neighbor in graph[vertex]:
if neighbor not in visited:
visited.add(neighbor)
queue.append(neighbor)
return order
if __name__ == "__main__":
graph = {
"A": ["B", "C"],
"B": ["D"],
"C": ["D", "E"],
"D": ["E"],
"E": [],
}
print(dfs(graph, "A")) # ['A', 'B', 'D', 'E', 'C']
print(bfs(graph, "A")) # ['A', 'B', 'C', 'D', 'E']
Example Walkthrough
For the graph above:
DFS begins at A. It follows the first listed neighbor B, continues through D to E, and then backtracks to visit C.
BFS begins at A and visits all distance-one vertices, B and C, before continuing to distance-two vertices.
Step
DFS path
BFS path
1
A
A
2
B
B
3
D
C
4
E
D
5
C
E
Edge Cases
Empty graph or missing start: Both functions return [].
One vertex: The result contains only that vertex.
Duplicate vertices or edges: The visited set ensures each vertex appears once.
Cycles and self-loops: Safe because visited vertices are skipped.
Disconnected graph: Each call traverses only the component containing start; repeat the call for other components.
Negative or non-integer labels: Labels are treated as hashable identifiers, not compared numerically.
Mark vertices visited when adding them to the stack or queue, not only when removing them; otherwise duplicates can be queued.
Do not use list.pop(0) as a queue operation because it costs O(n) and shifts every remaining element.
Remember that BFS finds shortest paths only for unweighted graphs or graphs with equal-weight edges. For arbitrary non-negative weights, use Dijkstra’s algorithm.
A common follow-up asks how to traverse every component of a disconnected graph or how to return both the path and its distance.
53
What is the graph shortest path problem and how is it solved?
KnownRevise
The shortest path problem finds a path between graph vertices whose total edge weight is minimal. It is commonly solved with BFS for unweighted graphs, Dijkstra’s algorithm for nonnegative weighted graphs, and Bellman–Ford when negative weights may exist.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
Core Concepts
Vertices represent locations or objects.
Edges represent connections and may have weights such as distance, cost, or travel time.
Path weight is the sum of the weights of the edges in the path.
A graph is a non-linear data structure. In Python, it is commonly represented with a dict mapping each vertex to adjacent vertices and edge weights.
Python has no built-in graph type. An adjacency list uses O(V + E) space, where V is the number of vertices and E is the number of edges.
The appropriate algorithm depends on the graph:
Algorithm
Appropriate graph
Time
Auxiliary space
BFS
Unweighted or equal-weight edges
O(V + E)
O(V)
Dijkstra
Nonnegative weighted edges
O((V + E) log V)
O(V + E) with a lazy heap
Bellman–Ford
Some negative edges, but no reachable negative cycle
O(VE)
O(V)
Topological dynamic programming
Directed acyclic graph
O(V + E)
O(V)
Floyd–Warshall
All-pairs paths in a small weighted graph
O(V³)
O(V²)
Auxiliary space excludes the stored graph. There is no recursion stack in these versions.
How Dijkstra’s Algorithm Works
Dijkstra’s algorithm uses a min-heap to process the vertex with the smallest currently known distance.
Set the source distance to 0 and all other distances to infinity.
Insert the source into the min-heap.
Remove the vertex with the smallest tentative distance.
Relax its outgoing edges, updating a neighbor if a shorter route is found.
Insert improved neighbors into the heap and repeat.
The greedy choice is safe because every remaining edge has nonnegative weight: once a vertex is removed with its smallest known distance, no later route can improve it.
Consider these edges:
S → A: 1
S → B: 4
A → B: 2
B → C: 1
The distance states are:
Start: S = 0.
Process S: A = 1, B = 4.
Process A: improve B to 3.
Process B: discover C with distance 4.
The shortest distance from S to C is 4, using S → A → B → C.
Code Example
from heapq import heappop, heappush
def shortest_paths_dijkstra(
source: str,
edges: list[tuple[str, str, int]],
) -> dict[str, int]:
"""Return minimum distances from source to all reachable vertices."""
if any(weight < 0 for _, _, weight in edges):
raise ValueError("Dijkstra's algorithm requires nonnegative edge weights")
adjacency: dict[str, list[tuple[str, int]]] = {}
for start, end, weight in edges:
adjacency.setdefault(start, []).append((end, weight))
adjacency.setdefault(end, [])
distances: dict[str, int] = {source: 0}
min_heap: list[tuple[int, str]] = [(0, source)]
while min_heap:
current_distance, vertex = heappop(min_heap)
# Ignore an outdated heap entry.
if current_distance != distances.get(vertex):
continue
for neighbor, weight in adjacency.get(vertex, []):
new_distance = current_distance + weight
if new_distance < distances.get(neighbor, float("inf")):
distances[neighbor] = new_distance
heappush(min_heap, (new_distance, neighbor))
return distances
example_edges = [
("S", "A", 1),
("S", "B", 4),
("A", "B", 2),
("B", "C", 1),
]
print(shortest_paths_dijkstra("S", example_edges))
# Expected output: {'S': 0, 'A': 1, 'B': 3, 'C': 4}
Complexity and Edge Cases
Time: O((V + E) log V) for a simple graph under the standard Dijkstra bound. Best, average, and worst-case complexities do not materially differ because the heap can still contain outdated entries.
Space: O(V + E) overall, including the adjacency list. The distance dictionary uses O(V), while the lazy heap can contain O(E) entries in the worst case. The input edge list is not modified.
Empty input: The function returns {source: 0}.
One vertex: The source receives distance 0.
Disconnected graph: Only vertices reachable from the source are returned.
Cycles: Nonnegative cycles are handled; revisiting a vertex cannot produce a smaller distance.
Duplicate edges: Supported; the algorithm keeps the effective shortest route.
Zero-weight edges: Supported.
Negative weights: Rejected by this implementation; use Bellman–Ford instead.
Common Pitfalls and Interview Tips
Do not use Dijkstra when negative edges exist; a negative cycle makes shortest distances undefined.
Do not confuse O(V + E) adjacency-list space with the auxiliary heap and distance storage.
Remember that unreachable vertices are absent from the returned dictionary, conceptually having infinite distance.
Avoid using a sorted list as a priority queue; insertion could become O(V).
Edge ordering does not affect correctness, and input edges should not be mutated unexpectedly.
In languages with fixed-width integers, verify that path weights cannot overflow.
A common follow-up is: “How would the algorithm change if only one destination were required?” A valid answer is A* with an admissible heuristic, provided nonnegative edge weights are guaranteed.
54
How does Dijkstra's algorithm work for shortest paths?
KnownRevise
Dijkstra’s algorithm is a greedy shortest-path algorithm for weighted graphs with non-negative edge weights. It repeatedly finalizes the reachable vertex with the smallest tentative distance from the source, ultimately determining the shortest distance to every reachable vertex.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
How It Works
Dijkstra maintains tentative distances from the source and a min-priority queue of candidate vertices. Its greedy choice is to process the vertex with the smallest known distance first.
This choice is safe because, with non-negative edge weights, passing through an unprocessed vertex cannot produce a distance smaller than the smallest tentative distance already selected.
Set the source distance to 0 and all other distances to infinity.
Insert the source into a min-heap.
Remove the vertex with the smallest tentative distance. Skip it if it has already been finalized.
Finalize that vertex and examine its outgoing edges. If going through it produces a shorter distance to a neighbor, update the neighbor and insert the new candidate into the heap.
Repeat until the heap is empty.
Walkthrough: Suppose A connects to B with weight 1 and C with weight 4, while B connects to C with weight 2.
Pop A with distance 0; update B to 1 and C to 4.
Pop B with distance 1; update C to 1 + 2 = 3.
Pop C with distance 3; it has no outgoing edges to relax.
The stale entry (4, C) is eventually popped and skipped because C was already finalized.
Time: Each vertex is finalized at most once, each edge is examined O(1) times, and at most O(E) heap entries are inserted. Best, average, and worst cases are identical for a fixed graph and deterministic implementation.
Space: The heapq version uses O(V + E) auxiliary space because stale queue entries can accumulate. The distances and finalized set each use O(V). The input graph is not counted.
Code Example
import heapq
def dijkstra(
graph: dict[str, list[tuple[str, int]]], start: str
) -> dict[str, float]:
"""Return shortest distances; unreachable vertices receive infinite distance."""
vertices: dict[str, None] = dict.fromkeys(graph)
for neighbors in graph.values():
for neighbor, _ in neighbors:
vertices[neighbor] = None
if not vertices:
return {}
if start not in vertices:
raise ValueError("Start vertex is not in the graph")
for neighbors in graph.values():
if any(weight < 0 for _, weight in neighbors):
raise ValueError("Dijkstra requires non-negative edge weights")
distances: dict[str, float] = {
vertex: float("inf") for vertex in vertices
}
distances[start] = 0.0
finalized: set[str] = set()
priority_queue: list[tuple[float, str]] = [(0.0, start)]
while priority_queue:
current_distance, vertex = heapq.heappop(priority_queue)
if vertex in finalized:
continue
finalized.add(vertex)
for neighbor, weight in graph.get(vertex, []):
if neighbor in finalized:
continue
new_distance = current_distance + weight
if new_distance < distances[neighbor]:
distances[neighbor] = new_distance
heapq.heappush(priority_queue, (new_distance, neighbor))
return distances
graph = {"A": [("B", 1), ("C", 4)], "B": [("C", 2)], "C": []}
result = dijkstra(graph, "A")
print(result) # {'A': 0.0, 'B': 1.0, 'C': 3.0}
Common Pitfalls
Negative weights: Dijkstra is invalid for graphs containing negative edge weights. Use Bellman–Ford when such weights may occur.
Stale heap entries: Do not process every popped entry; skip entries for vertices already finalized.
Disconnected graphs: Unreachable vertices retain an infinite distance, which must be handled explicitly by the caller.
Edge cases: An empty graph returns {}; a one-vertex graph returns distance 0 for that vertex; duplicate edges are examined individually; and non-negative cycles terminate because each vertex is finalized once.
Input ordering: Vertex and edge ordering does not affect correctness, so an already sorted adjacency list behaves the same.
Dense graphs: An O(V²) array implementation can outperform a heap when E is close to V².
Interview follow-up: Interviewers commonly ask how to reconstruct the path itself, which requires storing predecessor information as distances are updated.
55
What is a directed acyclic graph (DAG), and where is it used?
KnownRevise
A directed acyclic graph (DAG) is a directed graph that contains no directed cycle. It models one-way dependencies or relationships in which the dependencies cannot loop back on themselves.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
Definition and Properties
A DAG consists of:
Vertices (V): Represent entities such as tasks, packages, or processes.
Directed edges (E): Represent one-way relationships or dependencies.
No directed cycle: A cycle would be a sequence of directed edges that returns to its starting vertex.
For example, the relationships A → B → C and A → D → C form a DAG. No directed path returns to its starting vertex. In contrast, A → B → C → A contains a cycle and is not a DAG.
A DAG is a non-linear graph, not necessarily a tree. Vertices may have multiple incoming or outgoing edges, and the graph may be disconnected. Every finite DAG has a topological ordering, in which every vertex appears after the vertices that must precede it.
Representation in Python 3.12
Python has no built-in graph or DAG type. A common representation is a dict[str, list[str]] mapping each vertex to a list of its outgoing neighbors. For example, the entry "app": ["api", "database"] represents the edges app → api and app → database.
This adjacency-list representation uses O(V + E) space for the graph. An iterative DFS or BFS can then detect cycles or produce a topological ordering without risking Python’s default recursion-depth limit.
Example Uses
A practical DAG is a build-system dependency graph:
Each vertex represents a build target, such as a library, executable, or test suite.
An edge A → B means that A must be processed before B.
A topological ordering determines a valid build sequence.
A cycle such as A → B → A indicates circular dependencies and prevents a valid topological ordering.
Other common uses include:
Package and task dependency management
CI/CD workflow ordering
Spreadsheet formula dependencies
Database query execution plans
Data pipeline lineage and transformation planning
Build systems such as Make, Bazel, and Gradle
Operations and Complexity
Common operations include:
Topological sorting: Produces an ordering in which every edge points from an earlier vertex to a later one.
Cycle detection: Determines whether a directed cycle exists.
Reachability analysis: Finds the vertices reachable from a given vertex.
Dependency propagation: Determines which tasks are affected when one task changes.
For an adjacency-list representation, a complete topological sort takes O(V + E) time in the best, average, and worst cases because each vertex and edge is processed at most once. Its auxiliary space is O(V), including indegree information, the queue, and the resulting order; this excludes the input graph. Counting the graph itself, total space is O(V + E).
An iterative DFS cycle check uses O(V + E) average and worst-case time and O(V) auxiliary space, including the explicit stack and visited states. Its best case can be O(1) if an immediately examined edge reveals a cycle. Reachability and dependency-propagation traversals require O(V + E) worst-case time and O(V) auxiliary space, excluding the input graph.
Common Pitfalls
Confusing directed and undirected cycles: A DAG forbids cycles that follow edge directions, not merely cycles in the underlying undirected graph.
Assuming every DAG is a tree: DAGs can have multiple parents, multiple children, and disconnected components.
Ignoring edge direction: The meaning of A → B must remain consistent throughout cycle detection and topological sorting.
Overlooking memory overhead: Adjacency lists require space for both vertices and edges, in addition to temporary visited states and traversal structures.
Ignoring edge cases: Empty graphs and single-vertex graphs are acyclic; disconnected components must all be included when validating the entire graph.
Common follow-up: Interviewers often ask how to detect a cycle or produce a topological ordering, and what error should be reported when circular dependencies are found.
56
How to detect a cycle in a directed graph?
KnownRevise
Cycle detection in a directed graph is done by tracking nodes that are currently on the active DFS path. If the traversal revisits a node that is still marked as visiting (i.e., a back-edge), a cycle exists. The standard approach runs in O(V + E) time using O(V) extra space.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
How It Works
The most common interview solution is a depth-first search with a three-state coloring scheme:
0 = unvisited: the node has never been explored.
1 = visiting: the node is on the current recursion stack (the active path from the DFS root).
2 = visited: the node and all its descendants have been fully explored.
As you traverse from a node to its neighbors, check each neighbor’s state. If a neighbor is in state 1, you have found a back-edge to an ancestor on the current path, which proves a cycle exists. If a neighbor is 0, recurse into it. Once all neighbors of a node are processed, mark it 2.
Because a graph may be disconnected, you must start a DFS from every unvisited node, not just the first one.
Complexity
Time: O(V + E) — every node and every edge is visited a constant number of times.
Space: O(V) — for the state array and the recursion stack, which can hold at most all nodes on one path.
Visit 1 from 3. Node 1 is in state 1 (visiting), so a back-edge is found.
A cycle exists: 1 -> 3 -> 1.
Code Example
from typing import Any
def has_cycle(graph: dict[Any, list[Any]]) -> bool:
"""Detect whether a directed graph contains a cycle.
Uses DFS with a three-state coloring scheme:
0 = unvisited, 1 = visiting (on the current path), 2 = visited.
A back-edge to a node in state 1 means a cycle exists.
Args:
graph: adjacency list mapping each node to its outgoing neighbors.
Returns:
True if the graph contains a cycle, False otherwise.
"""
# State 0 = unvisited, 1 = visiting, 2 = visited
state = {node: 0 for node in graph}
def dfs(node: Any) -> bool:
state[node] = 1 # Mark as visiting, i.e., on the current recursion stack
for neighbor in graph[node]:
if state[neighbor] == 1:
return True # Found a back-edge, so a cycle exists
if state[neighbor] == 0 and dfs(neighbor):
return True
state[node] = 2 # Mark as fully visited
return False
# Start a DFS from every unvisited node to handle disconnected graphs
return any(dfs(node) for node in graph if state[node] == 0)
# Example: 0 -> 1 -> 3 -> 1 (cycle), 0 -> 2 -> 3
graph = {0: [1, 2], 1: [3], 2: [3], 3: [1]}
print(has_cycle(graph)) # True
Alternative: Kahn’s Algorithm
Kahn’s algorithm performs a topological sort by repeatedly removing nodes with in-degree zero. If the number of processed nodes is less than the total, a cycle exists. It also runs in O(V + E) time and O(V) space, and is useful when you need a topological order as a side effect.
Common Pitfalls
Forgetting disconnected components: only starting DFS from one node misses cycles in other parts of the graph.
Confusing visiting with visited: a node marked 2 (fully visited) must not be treated as a cycle; only state 1 triggers it.
Using a list as a queue with pop(0): if implementing Kahn’s algorithm, use collections.deque since list.pop(0) is O(n).
Recursion depth: a deep path can exceed Python’s default recursion limit (~1000); for large graphs, convert the DFS to an explicit stack or raise the limit.
Self-loops: a node with an edge to itself is a cycle of length 1 — the code above handles this naturally since the neighbor equals the visiting node.
Follow-up: interviewers often ask how to find the actual cycle path, or how to adapt the approach for an undirected graph (where you must ignore the parent node you came from).
57
How do graphs and trees differ as data structures?
KnownRevise
A tree is a specialized graph: it is connected and contains no cycles, while a general graph may be disconnected or contain cycles and can represent arbitrary many-to-many relationships. Trees model hierarchies; graphs model networks.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
Core Differences
Graph: Consists of vertices and edges. Edges may be directed or undirected, and vertices can have any number of neighbors.
Tree: Has a hierarchy with a root and parent-child relationships. An undirected tree with V vertices always has exactly V - 1 edges.
Connectedness: A tree must be connected. A graph may consist of one connected component or several disconnected components.
Cycles: A tree cannot contain a cycle. A graph can contain cycles.
Multiple paths: A tree has exactly one path between any two vertices. A graph may have multiple paths.
Root requirement: An undirected tree can be given a root to define parent-child relationships. A general graph does not naturally have roots or levels.
Classification: Both are non-linear, non-primitive data structures. They are conceptual structures that can be implemented with arrays, linked references, dictionaries, adjacency lists, or adjacency matrices.
Operation Costs
The following assumes a balanced binary search tree and a graph represented with a dictionary of adjacency lists. Costs depend on the selected implementation.
Operation
Tree best
Tree average
Tree worst
Graph best
Graph average
Graph worst
Access existing element
O(log n)
O(log n)
O(log n)
O(1)
O(1)
O(V)
Search by value or key
O(log n)
O(log n)
O(log n)
O(V + E)
O(V + E)
O(V + E)
Insert
O(log n)
O(log n)
O(log n)
O(1)
O(1)
O(V)
Delete
O(log n)
O(log n)
O(log n)
O(V + E)
O(V + E)
O(V + E)
Tree costs: Balanced-tree operations follow the height, giving O(log V) time. An unbalanced tree can degrade to O(V) per operation.
Graph access and insertion: Dictionary lookup and insertion are O(1) on average but O(V) in the worst case because of hash collisions.
Graph search: Scanning vertices and their adjacency lists costs O(V + E). A search for a specifically keyed vertex can be O(1) average with a dictionary.
Graph deletion: Removing a vertex may require removing all incident edges, which can cost O(V + E).
Memory and Ordering
Property
Tree
Graph
Storage including the structure
O(V)
O(V + E) with an adjacency list
Extra memory per vertex
O(1) links or references
O(1) base overhead plus adjacency references
Memory usage
One tree structure for V vertices
An adjacency matrix requires O(V²) space
Order
Supports hierarchical traversal orders
Has no inherent general ordering
The storage costs above count the tree or graph itself. If the structure is treated as input, traversal adds auxiliary space:
Tree traversal: O(H) stack space for recursive DFS, where H is the tree height measured in edges. An iterative traversal can use O(H) stack space as well.
Graph traversal: O(V) stack space for recursive DFS, or O(V) visited state for BFS/DFS. A disconnected graph requires processing every component.
Memory layout: Array-based storage is compact and cache-friendly. Linked structures add pointer or reference overhead per node and usually scatter related nodes across memory.
Choosing Between Them
Choose a tree when the data has a hierarchy and each item has one logical parent:
File systems and directory hierarchies
Organization charts
Database indexes such as B+ trees
Expression and syntax trees
Choose a graph when relationships are general or many-to-many:
Road maps and navigation
Social networks
Service dependencies
Recommendation systems
A tree is usually preferable when hierarchy, ordering, and fast ancestor or descendant operations matter. A graph is preferable when entities can have multiple connections, cycles are meaningful, or queries involve reachability between arbitrary vertices.
Common Pitfalls
A tree is a special case of a graph, not an entirely separate mathematical category.
Do not assume every graph implementation has O(V + E) storage; adjacency matrices use O(V²) space.
Do not assume tree operations are always O(log n); an unbalanced tree can require O(n).
Do not ignore hash-table worst cases when presenting graph lookup costs.
Use a visited set during graph traversal to avoid infinite loops on cycles.
When choosing a structure, identify the operation that must be fast rather than selecting by habit. Interviewers commonly ask about thread safety, persistence, or what happens when the data no longer fits in memory.
58
What is a topological sort, and what are its practical applications?
KnownRevise
Topological sorting linearly orders the vertices of a directed acyclic graph (DAG) so that every directed edge u → v places u before v. It is used to resolve dependencies before dependent work can begin; a cycle means no valid topological order exists.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
Core Idea
A topological order describes dependency precedence rather than numerical or alphabetical order. For example, if compile must run before test, and test before deploy, one valid order is:
compile → test → deploy
Important properties:
A topological order exists only when the directed graph is acyclic.
The order is usually not unique. Independent nodes can appear in different positions.
Every dependency path is respected transitively, even when only its direct edges appear in the graph.
Disconnected components can be ordered independently.
How Kahn’s Algorithm Works
Kahn’s algorithm repeatedly processes vertices with no remaining prerequisites:
Build an indegree map containing the number of incoming edges for each vertex.
Add all zero-indegree vertices to a queue.
Remove a vertex from the queue and append it to the result.
Decrease the remaining indegree of each outgoing neighbor.
Add any neighbor whose remaining indegree becomes zero.
If not all vertices were processed, the graph contains a cycle.
The implementation is iterative, so it does not risk Python’s recursion-depth limit.
Applications
Topological sorting is useful whenever tasks have prerequisites:
Build systems: compile a library before compiling an application that depends on it.
Package managers: install dependencies before dependent packages.
CI/CD pipelines: execute testing, packaging, and deployment stages in dependency order.
Task schedulers: run jobs only after their required upstream jobs complete.
Course or employee scheduling: satisfy prerequisite requirements.
Compilers and module systems: initialize modules before modules that import them.
For example, a build system might use the order generate-code → compile → test → package.
Code Example
from collections import deque
from collections.abc import Hashable, Mapping
from typing import TypeVar
Node = TypeVar("Node", bound=Hashable)
def topological_sort(graph: Mapping[Node, list[Node]]) -> list[Node]:
"""Return a dependency-first order, or raise ValueError if a cycle exists."""
indegree: dict[Node, int] = {node: 0 for node in graph}
for neighbors in graph.values():
for neighbor in neighbors:
indegree[neighbor] = indegree.get(neighbor, 0) + 1
remaining_indegree = indegree.copy()
ready: deque[Node] = deque(
node for node in indegree if remaining_indegree[node] == 0
)
order: list[Node] = []
while ready:
node = ready.popleft()
order.append(node)
for neighbor in graph.get(node, []):
remaining_indegree[neighbor] -= 1
if remaining_indegree[neighbor] == 0:
ready.append(neighbor)
if len(order) != len(indegree):
raise ValueError("The graph contains a cycle.")
return order
if __name__ == "__main__":
graph = {
"build": ["test"],
"test": ["package"],
"package": ["deploy"],
}
print(topological_sort(graph))
# Expected output: ['build', 'test', 'package', 'deploy']
Edge-case behavior:
Empty graph: returns [].
One vertex: returns that vertex.
Disconnected graph: processes every component.
Duplicate edges: counts and decrements each duplicate correctly.
Already ordered input: remains valid, although independent vertices may move.
Cycle: raises ValueError.
Negative or numeric labels: supported because any hashable vertex identifier is accepted.
Complexity
For a graph with V vertices and E edges:
Approach
Best, Average, and Worst Time
Auxiliary Space
Reason
Kahn’s algorithm
O(V + E)
O(V)
Every vertex and edge is processed once; indegree map and queue store O(V) entries
DFS-based approach
O(V + E)
O(V)
Requires visited state and O(V) recursion or explicit-stack space
The space figures exclude the input graph itself. Including the graph, total storage is O(V + E). A recursive DFS implementation may exceed Python’s default recursion limit for large graphs, so an explicit stack is preferable.
Common Pitfalls
Assuming uniqueness: many topological orders can satisfy the same dependencies.
Missing cycle detection: processing fewer than V vertices indicates that no valid order exists.
Mutating the input: the implementation above copies indegrees rather than altering adjacency lists.
Forgetting visited state in DFS: without it, nodes can be revisited and cycles mishandled.
Off-by-one or edge-count errors: decrement indegree exactly once for every edge occurrence.
Integer overflow: Python integers do not overflow, but implementations in fixed-width languages need sufficiently large counters.
A common interview follow-up is: How would you produce a stable or deterministic topological order when several nodes are ready at the same time?
59
What is Bellman-Ford’s algorithm and its time complexity?
KnownRevise
Bellman–Ford computes single-source shortest paths in a weighted graph, including graphs with negative edge weights. It repeatedly relaxes every edge and can also detect a negative-weight cycle reachable from the source.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
How It Works
Bellman–Ford operates on a directed, weighted graph:
Set the source distance to 0 and every other distance to infinity.
Relax every edge, replacing a destination distance when a shorter path is found.
Repeat the relaxation process up to V - 1 times, where V is the number of vertices.
Relax all edges once more. If any distance improves during this Vth pass, a negative-weight cycle is reachable from the source.
An edge u → v with weight w is relaxed using:
candidate = distance[u] + w
If candidate < distance[v], update distance[v].
After V - 1 passes, every shortest path that does not contain a negative cycle has been considered. Such a path can be reduced to a simple path containing at most V - 1 edges.
Small Example
Consider these directed edges:
A → B with weight 4
A → C with weight 5
B → C with weight -2
C → D with weight 3
State
A
B
C
D
Initialization
0
∞
∞
∞
After pass 1
0
4
2
5
After pass 2
0
4
2
5
The second pass produces no changes, so the algorithm stops. The shortest distance to D is 5.
Negative Cycles and Correctness
A negative-weight cycle has a total weight below zero. Traversing it repeatedly can reduce a path’s cost indefinitely, so affected vertices have no finite shortest-path distance.
A relaxation on the Vth pass indicates a reachable negative-weight cycle.
Negative cycles not reachable from the source are not detected.
Negative edge weights are otherwise supported correctly.
Edge ordering does not affect the final result, although it can affect how many passes are needed.
Complexity
Case
Time
Reason
Best
O(V + E)
Early termination after initialization and one successful scan
Average
O(VE)
Up to V scans over E edges
Worst
O(VE)
Every pass scans every edge, for V passes
Auxiliary space: O(V), excluding the input graph, for the distance dictionary and vertex set.
Recursion stack: O(1) because the implementation is iterative.
If the input graph is included, total space is O(V + E).
Code Example
from math import inf
Graph = dict[str, list[tuple[str, int | float]]]
def bellman_ford(
graph: Graph,
source: str,
) -> tuple[dict[str, int | float], bool]:
"""Return shortest distances and whether a reachable negative cycle exists."""
# A dict keeps vertices in first-seen order, so the output order is stable.
vertices = dict.fromkeys(graph)
vertices.setdefault(source)
for neighbors in graph.values():
for target, _ in neighbors:
vertices.setdefault(target)
distances: dict[str, int | float] = {
vertex: inf for vertex in vertices
}
distances[source] = 0
vertex_count = len(vertices)
for iteration in range(1, vertex_count + 1):
changed = False
for start, neighbors in graph.items():
distance_start = distances[start]
if distance_start == inf:
continue
for target, weight in neighbors:
candidate = distance_start + weight
if candidate < distances[target]:
distances[target] = candidate
changed = True
if not changed:
return distances, False
if iteration == vertex_count:
return distances, True
return distances, False
if __name__ == "__main__":
graph: Graph = {
"A": [("B", 4), ("C", 5)],
"B": [("C", -2)],
"C": [("D", 3)],
"D": [],
}
distances, has_negative_cycle = bellman_ford(graph, "A")
print(distances, has_negative_cycle)
# {'A': 0, 'B': 4, 'C': 2, 'D': 5} False
Common Pitfalls and Interview Tips
Use V - 1 passes for shortest paths and one additional pass for negative-cycle detection.
Do not use Dijkstra with negative edge weights; Bellman–Ford is the standard alternative.
Unreachable vertices retain an infinite distance.
Parallel edges are handled naturally; only an edge that improves the destination matters.
An empty graph with a specified source treats that source as an isolated vertex.
For large graphs, use sufficiently wide integers in other languages to avoid distance overflow.
A common follow-up is replacing Bellman–Ford with Dijkstra when all edge weights are known to be non-negative.
60
How can you determine whether a graph is bipartite?
KnownRevise
An undirected graph is bipartite if its vertices can be divided into two groups such that every edge connects vertices from different groups. Test this by two-coloring each connected component with BFS or DFS; an edge whose endpoints have the same color proves that the graph is not bipartite.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
Core Idea
An undirected graph is bipartite exactly when it contains no odd-length cycle.
Use two colors, 0 and 1:
Start an uncolored vertex with color 0.
Give each uncolored neighbor the opposite color.
If an edge connects two vertices with the same color, return False.
Repeat for every connected component.
For example, consider the square 0–1–3–2–0:
Color 0 as 0.
Color its neighbors 1 and 2 as 1.
Both 1 and 2 force 3 to have color 0.
Every edge connects different colors, so the graph is bipartite with sets {0, 3} and {1, 2}.
Graphs commonly model dependencies, maps, and social networks.
BFS and DFS Approaches
Approach
Time
Auxiliary space
When to use
BFS two-coloring
O(V + E)*
O(V)
Usually preferred; iterative and avoids recursion-depth limits
DFS two-coloring
O(V + E)*
O(V), including the recursion stack
Convenient for recursive graph traversal
A successful run visits every vertex and inspects every adjacency-list entry. If a coloring conflict is found earlier, the algorithm stops immediately.
*These are the standard graph-algorithm bounds when color lookup is treated as O(1). The Python dictionary caveat is described below.
Complexity
Let V be the number of vertices and E the number of edges.
Best-case time:O(1) is possible when the first examined edge produces a conflict; a successful full verification takes O(V + E).
Average-case time:O(V + E) under ordinary hashing and when most of the graph is examined; a tighter average requires an input distribution.
Worst-case time:O(V + E) with constant-time color access. With the shown dictionary implementation, ordinary hashing gives O(V + E), while pathological hash collisions can raise the bound to O(V(V + E)).
Auxiliary space:O(V) for the color map and BFS queue. The input graph is not counted; an adjacency-list representation itself occupies O(V + E) space.
Recursion stack:O(V) for recursive DFS. The iterative BFS has no recursive-call stack, although its queue still occupies O(V).
Code Example
Python has no built-in graph type, so this example uses an adjacency-list mapping. It supports disconnected components, negative integer vertex labels, duplicate edges, isolated vertices, and self-loops.
from collections import deque
from collections.abc import Mapping, Sequence
Graph = Mapping[int, Sequence[int]]
def is_bipartite(graph: Graph) -> bool:
"""Return True if the undirected graph has a valid two-coloring."""
colors: dict[int, int] = {}
for start in graph:
if start in colors:
continue
colors[start] = 0
queue = deque([start])
while queue:
vertex = queue.popleft()
for neighbor in graph.get(vertex, ()):
if neighbor not in colors:
colors[neighbor] = 1 - colors[vertex]
queue.append(neighbor)
elif colors[neighbor] == colors[vertex]:
return False
return True
# Square graph: bipartition is {0, 3} and {1, 2}.
graph = {
0: [1, 2],
1: [0, 3],
2: [0, 3],
3: [1, 2],
}
print(is_bipartite(graph)) # True
Common Pitfalls
Checking only one component: Every component must be processed because one component may contain an odd cycle.
Using only a visited set: Store each vertex’s color, not merely whether it was visited.
Ignoring cycles: Even cycles permit a bipartition, while an odd cycle or a self-loop prevents one.
Ignoring edge direction: This algorithm tests the underlying undirected graph. For directed graphs, first define whether that is the intended meaning.
Edge cases: An empty graph and a single isolated vertex are bipartite. Duplicate edges do not change the result, and negative vertex labels are supported.
Weights versus edges: Edge weights do not affect bipartiteness; the result depends on which vertices are joined by edges.
Typical follow-up: Explain how to implement the same algorithm with DFS or how to extract the two vertex partitions after coloring succeeds.
61
What is quicksort and what is its average-case time complexity?
KnownRevise
Quicksort is a divide-and-conquer comparison sorting algorithm with an average-case time complexity of O(n log n). It selects a pivot, partitions elements around it, and recursively or iteratively sorts the resulting partitions.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
How Quicksort Works
Choose a pivot element, often using a random index.
Partition the current range into three groups:
Elements smaller than the pivot
Elements equal to the pivot
Elements greater than the pivot
Sort the smaller and larger groups independently.
Stop when a range contains zero or one element.
A three-way partition is useful when duplicates are common. For example, partitioning [2, 1, 2, 3, 2] around pivot 2 produces groups containing values less than 2, values equal to 2, and values greater than 2, so all duplicate values are handled together.
Complexity
The partition operation examines each element in a range once, taking O(n) time. If the pivot produces balanced partitions, the work follows approximately:
T(n) = T(n/2) + T(n/2) + O(n)
This results in O(n log n) average time.
Case
Time
Reason
Best
O(n log n)
Partitions are consistently well balanced
Average
O(n log n)
Random pivots produce reasonably balanced partitions on average
Worst
O(n²)
Every pivot repeatedly creates highly unbalanced partitions
Practical Guidance
Quicksort is often efficient when:
In-place sorting is important because it uses little extra memory.
Good average performance is needed for large arrays.
A stable sort is not required. Quicksort is generally not stable.
Comparisons and swapping are inexpensive compared with copying data.
Python’s built-in list.sort() is usually preferable in production because it is implemented in optimized C and uses a stable sorting algorithm. Quicksort is still valuable for understanding partitioning, pivot selection, and worst-case behavior.
Common Pitfalls
Ignoring the worst case: Poor pivot selection can cause O(n²) time, especially with already sorted input and a fixed endpoint pivot.
Handling duplicates poorly: A two-way partition can repeatedly process equal elements; a three-way partition avoids this.
Using recursion without considering stack depth: A recursive implementation can exceed Python’s default recursion limit on skewed inputs.
Mutating input unexpectedly: In-place quicksort reorders the caller’s list; copy it first if the original order must be kept.
Expecting stability: Equal elements may change their relative order.
Common follow-up questions: Interviewers may ask about pivot selection, how to guarantee O(n log n) worst-case time, or how to compare quicksort with mergesort and heapsort.
62
How do depth-first search and breadth-first search differ in binary trees?
KnownRevise
Depth-first search (DFS) explores one branch as deeply as possible before backtracking, while breadth-first search (BFS) explores nodes level by level. In a binary tree, DFS can use preorder, inorder, or postorder traversal, whereas BFS uses level-order traversal.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
How They Work
DFS:
Visit the current node.
Explore one subtree, conventionally the left subtree first.
Backtrack when that branch is exhausted.
Uses a last-in, first-out stack, either explicitly or through recursion.
Is useful for backtracking, path exploration, expression evaluation, and cycle detection in general graphs, though standard binary trees are already acyclic.
BFS:
Visit the root.
Visit all nodes at the current depth.
Move to the next depth.
Uses a first-in, first-out queue, commonly collections.deque in Python.
Is useful for level-order processing, minimum-depth searches, shortest paths measured in edges, and dependency scheduling.
Costs and Trade-offs
For a binary tree containing (n) nodes, let (H) be its height measured in edges and (W) its maximum number of nodes at any level.
Operation or property
DFS
BFS
Visit every node
O(n) best, average, and worst
O(n) best, average, and worst
Find a matching target
O(1) best, O(n) average, O(n) worst
O(1) best, O(n) average, O(n) worst
First match at minimum depth
Not guaranteed
Guaranteed
Auxiliary space
O(H + 1) worst case
O(W) worst case
Main memory structure
Stack or recursion stack
Queue
Traversal order
Depth-first
Level-order
Main memory concern
A skewed tree can create a deep call stack
A wide tree can create a large queue
Time reason: Each node is visited at most once, so a complete traversal takes O(n).
Space reason: DFS stores pending branches along the current depth, while BFS stores a frontier spanning adjacent levels.
Average-search reason: If the target is uniformly distributed among positions in traversal order, approximately half the nodes are examined on average.
Input accounting: The O(n) input tree is not included in auxiliary space; the returned output also requires O(n) space if output storage is counted.
Cache behavior: DFS often follows nearby parent-child references and can be cache-friendly in some layouts. BFS may access nodes scattered across memory, especially in a wide tree.
Trade-off: Choose DFS for depth-first processing or backtracking. Choose BFS for level-order processing or when the first match must have minimum depth.
In a tree, the path between two fixed nodes is unique, so DFS can find that path; BFS’s distinguishing guarantee is that it discovers matches in nondecreasing depth order.
Example Walkthrough
Consider a tree whose root is A, whose left child B has children D and E, and whose right child C has child F.
DFS preorder:
Pop A and output it; push C, then B.
Pop B and output it; push E, then D.
Pop and output D.
Pop and output E.
Pop and output C, then pop and output F.
The result is A, B, D, E, C, F.
BFS level order:
Dequeue and output A; enqueue B and C.
Dequeue and output B; enqueue D and E.
Dequeue and output C; enqueue F.
Dequeue and output D, E, and F.
The result is A, B, C, D, E, F.
Common Pitfalls
Using a list as a queue: list.pop(0) costs O(n), making repeated dequeuing O(n²). Use deque.popleft() for O(1) removal.
Reversing child order: To produce preorder root, left, right with a stack, push the right child before the left child.
Ignoring recursion depth: Recursive DFS can exceed Python’s default recursion limit on a deeply skewed tree; an explicit stack avoids that limit.
Ignoring memory shape: A deep tree favors BFS over recursive DFS, while a wide tree can produce a large BFS queue.
Handling edge cases: Empty trees produce an empty result, and a one-node tree produces its root value. Duplicate values remain duplicate node visits, while negative values are ordinary values requiring no special handling.
Applying graph assumptions: Sorted input is irrelevant to traversal of a binary tree. Disconnected graphs and cycles apply only to general graphs, where both algorithms need a visited set.
Interview follow-up: Interviewers commonly ask about shortest-path guarantees, stack-versus-queue memory, duplicate values, and how to implement DFS iteratively.
63
How do you binary search a rotated sorted array?
KnownRevise
Use a modified binary search that identifies which half of the current interval is sorted, then determines whether the target lies in that half. The algorithm takes O(log n) time for distinct elements, while duplicates can force O(n) time.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
Core Idea
Assume the array was sorted in non-decreasing order before being rotated. At each iteration:
Return the midpoint index if it contains the target.
If duplicate boundary values make the sorted half ambiguous, shorten the interval by one.
Otherwise, determine which half is sorted.
Check whether the target lies within that half’s value range, then discard the half that cannot contain it.
For [4, 5, 6, 7, 0, 1, 2], searching for 0:
Step
Interval
Midpoint
Decision
1
[0, 6]
Index 3, value 7
[4, 5, 6, 7] is sorted; 0 is outside its range, so search indices [4, 6].
2
[4, 6]
Index 5, value 1
[0, 1] is sorted; 0 is within its range, so search indices [4, 4].
3
[4, 4]
Index 4, value 0
Return index 4.
Handling Duplicates
With distinct elements, the sorted half is always identifiable. With duplicates, nums[left] == nums[mid] or nums[mid] == nums[right] can make it ambiguous.
The implementation safely shortens the interval in those cases. If the target equaled the ambiguous value, it would already have been found at the midpoint; otherwise, the removed boundary cannot be the target.
Complexity
Approach
Best time
Average time
Worst time
Auxiliary space
When to use
Linear scan
O(1)
O(n)
O(n)
O(1)
The array is small or simplicity matters more than asymptotic speed.
Modified binary search, distinct values
O(1)
O(log n)
O(log n)
O(1)
The rotated array has distinct values.
Modified binary search, duplicates possible
O(1)
O(log n) for duplicate-light distributions; O(n) for duplicate-heavy distributions
O(n)
O(1)
Duplicates are allowed and worst-case linear time is acceptable.
Time: With distinct values, each iteration approximately halves the search interval. Duplicate boundary handling may remove only one element, producing O(n) worst-case time.
Space: Only a few index variables are used, so auxiliary space is O(1). The input list is not counted, and there is no recursion stack.
Code Example
def binary_search_rotated(nums: list[int], target: int) -> int:
"""Return the index of target in a rotated sorted list, or -1 if absent."""
left, right = 0, len(nums) - 1
while left <= right:
mid = (left + right) // 2
if nums[mid] == target:
return mid
# Duplicate boundary values make the sorted half ambiguous.
# Since the midpoint was not the target, neither boundary is it.
if nums[left] == nums[mid]:
left += 1
elif nums[mid] == nums[right]:
right -= 1
elif nums[left] <= nums[mid]:
# The left half is sorted.
if nums[left] <= target < nums[mid]:
right = mid - 1
else:
left = mid + 1
else:
# The right half is sorted.
if nums[mid] < target <= nums[right]:
left = mid + 1
else:
right = mid - 1
return -1
print(binary_search_rotated([4, 5, 6, 7, 0, 1, 2], 0)) # Output: 4
The function handles an empty list, one element, duplicates, negative values, an already sorted list, and absent targets. It does not mutate the input and returns an arbitrary matching index when duplicate targets exist.
Common Pitfalls
Assuming the whole interval is sorted: Ordinary binary search cannot be applied directly after rotation.
Using inconsistent range comparisons: Inclusive boundaries require left <= target < mid or mid < target <= right.
Ignoring duplicates: Equal boundary values can make the sorted-half decision ambiguous.
Confusing a missing target with index 0: Return -1 when the target is absent.
Interview follow-up: If many duplicates are expected, discuss how duplicate-heavy inputs can cause linear-time behavior.
64
Merge sort vs heap sort: key differences
KnownRevise
The main difference is that merge sort is a stable, divide-and-conquer algorithm with guaranteed O(n log n) time but O(n) extra space, while heap sort is an in-place, comparison-based algorithm that uses a binary heap and is not stable.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
Time and Space Complexity
Merge sort: Best, average and worst case time are all O(n log n) because it always divides the list in half and merges the results. Space is O(n) for the auxiliary array used during merging, plus O(log n) recursion stack.
Heap sort: Best, average and worst case time are all O(n log n) because building the heap is O(n) and each of the n extractions costs O(log n). Space is O(1) auxiliary since the heap is built in the input array.
How They Work
Merge sort recursively splits the list into halves until single elements remain, then merges the halves back together in sorted order.
Heap sort first builds a max-heap from the input, then repeatedly swaps the root (maximum) with the last element and heapifies the remaining heap.
Stability and In-Place
Merge sort is stable: equal elements keep their original relative order because merging preserves order. It is not in-place: it requires extra memory proportional to the input size.
Heap sort is not stable: swapping the root with the last element can reorder equal elements. It is in-place: it only uses a constant amount of extra space.
Comparison Table
Feature
Merge Sort
Heap Sort
Best time
O(n log n)
O(n log n)
Average time
O(n log n)
O(n log n)
Worst time
O(n log n)
O(n log n)
Space
O(n) extra
O(1) extra
Stable
Yes
No
In-place
No
Yes
Cache friendliness
Good (sequential merge)
Poor (random access)
Typical use
External sorting, linked lists
Memory-constrained systems
Real-World Use
Merge sort is often used for external sorting where data does not fit in memory, and for sorting linked lists because it does not require random access.
Heap sort is useful when memory is limited because it sorts in place, and it is the basis for priority queue implementations.
Common Pitfalls
Choosing a sort based on habit instead of the operations needed.
Ignoring stability requirements when sorting objects with multiple keys.
Assuming heap sort is always faster because it is in-place; its random memory access pattern can be slower than merge sort in practice.
Forgetting that both algorithms have O(n log n) worst-case time, unlike quicksort.
65
What is bucket sort and when should it be used?
KnownRevise
Bucket sort is a non-comparison sorting algorithm that distributes values into buckets, sorts each bucket independently, and then concatenates them. It is most effective when numeric values have a bounded range and are reasonably evenly distributed.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
How It Works
Find the minimum and maximum values.
Divide the value range into a chosen number of buckets.
Place each value into the bucket corresponding to its position in that range.
Sort the values within each bucket.
Concatenate the sorted buckets in order.
Bucket sort is different from counting sort: bucket sort sorts values inside buckets, while counting sort places each value into an exact bucket determined by its value.
The implementation below uses insertion sort for the buckets. This keeps the algorithm stable and demonstrates the core operation rather than delegating all sorting to a library.
Relevant edge cases include:
Empty input: returns an empty list.
One element or identical values: returns a copy directly.
Duplicates: preserves every occurrence.
Negative numbers: supported by subtracting the minimum value during distribution.
Already sorted input: handled normally.
Very large or skewed ranges: may place many values in one bucket and reduce performance.
Complexity
Let n be the number of values and k the number of buckets.
Case
Time
Reason
Best
O(n + k)
Values are distributed with little or no sorting work inside buckets.
Average
O(n + k)
With reasonably uniform data, expected bucket occupancy is low, making insertion sorting linear overall.
Worst
O(n² + k)
All values can enter one bucket, where insertion sort takes O(n²).
Auxiliary space: O(n + k), including bucket storage and the returned list, but not the original input.
Recursion stack: O(1), because the implementation is iterative.
With k chosen near √n, average time and space are O(n).
Approach
Time
Extra space
When to use
Bucket sort
O(n + k) average; O(n² + k) worst
O(n + k)
Bounded, approximately uniformly distributed numeric values
General comparison sort
O(n log n) worst
Implementation-dependent
Arbitrary values without a useful distribution or range
Example and Use Case
A practical use case is sorting floating-point measurements known to lie in a small range, such as normalized sensor readings between 0.0 and 1.0.
For [0.10, 0.40, 0.60, 0.90], the algorithm chooses two buckets:
Step
Bucket 1
Bucket 2
Distribute
[0.10, 0.40]
[0.60, 0.90]
Sort internally
[0.10, 0.40]
[0.60, 0.90]
Concatenate
[0.10, 0.40, 0.60, 0.90]
This approach is appropriate when many similar numeric values must be ordered quickly and their approximate distribution is known in advance.
Common Pitfalls
Assuming O(n) performance for every input: the O(n) average case requires reasonably uniform distribution.
Using too few buckets: large bucket occupancy increases insertion-sort cost.
Ignoring floating-point boundaries: explicitly clamp the maximum value to the final bucket.
Mutating the input unexpectedly: the example returns a new list.
Choosing by habit: use a general comparison sort when values have an unknown range, are highly skewed, or are not numeric.
Interviewers commonly ask how bucket count affects performance, how the algorithm handles negative numbers, and why bucket sort can degrade to O(n²).
66
What are the advantages of quicksort over other sorting algorithms?
KnownRevise
Quicksort is often one of the fastest general-purpose sorting algorithms because it combines average-case O(n log n) performance, low overhead, good cache locality, and in-place partitioning. Its main trade-offs are an O(n²) worst case and its lack of stability in standard implementations.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
Main Advantages
Fast average case: Randomized pivot selection gives expected O(n log n) time, comparable to mergesort and heapsort.
Low auxiliary memory: Partitioning is in-place. Processing the smaller partition first bounds the recursion or work-stack space at O(log n).
Good cache locality: Partitioning scans the array in a mostly sequential pattern, often outperforming heapsort’s less predictable access pattern.
Low constant factors: It avoids mergesort’s temporary arrays and usually requires less bookkeeping than heapsort.
Practical variants: Randomized pivots reduce predictable bad partitions, median-of-three selection helps with patterned inputs, and three-way partitioning handles duplicate-heavy data efficiently.
Parallelization potential: The two partitions produced by a pivot can be sorted concurrently, although balancing work between processors adds complexity.
Complexity Comparison
Auxiliary space excludes the input itself but includes temporary storage and the recursion or work stack.
Algorithm
Best time
Average time
Worst time
Auxiliary space
Stable?
Quicksort
O(n log n)
O(n log n) expected
O(n²)
O(log n) with smaller-side-first processing; conventional recursion is O(log n) expected and O(n) worst case
No
Mergesort
O(n log n)
O(n log n)
O(n log n)
O(n)
Yes
Heapsort
O(n log n)
O(n log n)
O(n log n)
O(1)
No
Insertion sort
O(n)
O(n²)
O(n²)
O(1)
Yes
Timsort
O(n)
O(n log n)
O(n log n)
O(n)
Yes
Quicksort: Each partition takes O(n) time. Balanced partitions produce O(log n) levels, while consistently bad pivots can produce O(n) levels and O(n²) total time.
Mergesort: It recursively sorts two halves and merges them in O(n) time, giving T(n) = 2T(n/2) + O(n) = O(n log n). Merging requires O(n) temporary storage.
Heapsort: Building the heap is O(n), and removing each of the n maximum elements costs O(log n), totaling O(n log n) time with O(1) auxiliary space.
Insertion sort: Each element may be shifted across all preceding elements, giving O(n²) time generally and O(n) for already sorted input. It uses only O(1) extra space.
Timsort: It detects and merges naturally ordered runs, producing O(n log n) worst-case time and requiring up to O(n) temporary storage.
Partitioning Example
For [3, 1, 3, 2] with pivot 3, three-way partitioning forms three logical groups:
Smaller values: [1, 2]
Values equal to the pivot: [3, 3]
Greater values: []
Only the smaller-than group needs further sorting, producing [1, 2, 3, 3]. Specialized three-way implementations can also process an all-equal input in O(n) time.
Practical Guidance
Quicksort is a strong choice for sorting in-memory arrays when average-case speed, cache efficiency, and low memory usage matter more than guaranteed worst-case performance or stability. Mergesort or Timsort is preferable when stability is required, and heapsort is preferable when O(n log n) worst-case time and O(1) auxiliary space are both required.
Common Pitfalls
Claiming quicksort is always O(n log n); its standard worst case is O(n²).
Forgetting that standard quicksort is not stable.
Ignoring the O(n) worst-case recursion stack of conventional implementations.
Assuming randomized pivots provide a worst-case guarantee; they provide an expected guarantee.
Mutating the caller’s data unexpectedly.
Using a custom quicksort in production when Python 3.12’s list.sort() already provides stable, adaptive Timsort.
A common follow-up is how to guarantee O(n log n) worst-case time. Introsort does this by switching to heapsort when quicksort recursion exceeds a depth limit.
67
What is insertion sort’s algorithm and time complexity?
KnownRevise
Insertion sort builds a sorted prefix by inserting each element into its correct position among the elements already processed. It performs well on small or nearly sorted datasets because it makes relatively few comparisons and shifts, and it is also used as part of some hybrid sorting algorithms.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
How It Works
Insertion sort treats the first element as a sorted prefix. It then considers each remaining element:
Save the current element as current.
Shift larger elements in the sorted prefix one position to the right.
Place current in the newly opened position.
Continue with the next element.
The algorithm is:
In-place: It uses constant auxiliary storage.
Stable: Equal elements retain their original relative order because an element is shifted only when it is strictly greater than the current element.
Adaptive: Its running time approaches linear on nearly sorted input.
Empty and single-element inputs are already sorted. Duplicate values retain their relative order, and negative values are handled naturally by the same comparisons used for positive values.
Complexity
Case
Time
Reason
Best
O(n)
The input is already sorted, so each element requires one comparison and no shifts.
Average
O(n²)
Random input causes a linear number of comparisons and shifts for each insertion on average.
Worst
O(n²)
Reverse-sorted input requires the maximum number of comparisons and shifts.
For this implementation, nearly sorted input takes O(n + k) time, where k is the number of shifted elements, corresponding to the number of inversions.
Auxiliary space:O(1) because only a fixed number of variables are used, excluding the input.
Total space including the stored input:O(n) because the input list occupies linear space.
Insertion sort is not generally used for large unsorted datasets because its average and worst-case running times are quadratic.
Example Walkthrough
For [3, 1, 2]:
Current list
Action
[3, 1, 2]
Treat 3 as the sorted prefix.
[1, 3, 2]
Insert 1 before 3.
[1, 2, 3]
Insert 2 between 1 and 3.
Common Pitfalls
Off-by-one errors: The sorted prefix begins at index 0, but insertion begins with index 1.
Mutating input: An in-place implementation changes the original list; copy it first if the caller requires the original order.
Using >=: Shifting equal elements can make the implementation unstable; use > instead.
Ignoring input characteristics: The algorithm is efficient for tiny datasets and nearly sorted data but inefficient for large, randomly ordered datasets.
Forgetting recursion space: This iterative approach has no recursive call stack, so its auxiliary space remains O(1).
68
What is a stable sort, and why does preserving the order of equal elements matter?
KnownRevise
A stable sort preserves the relative order of records with equal sort keys. This matters when that original order has meaning or when later stages must retain the ordering established by earlier stages.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
Definition
Suppose records contain both a score and an arrival_time. Sorting by score with a stable algorithm keeps records having the same score in their original arrival_time order.
For example, given the input order (Alice, 90), (Bob, 85), (Charlie, 90), a stable sort by score produces:
(Bob, 85)
(Alice, 90)
(Charlie, 90)
Alice remains before Charlie because they have equal keys. Stability does not change the records’ key values; it only determines the order of records considered equal under the comparison.
Why It Matters
Stable sorting is important when:
Multiple sorting stages are used: After stably sorting employees by department and then by salary, employees with equal salaries remain in department order within that salary group.
Equal elements need a meaningful secondary order: Sorting students by score can preserve their original enrollment order for ties.
Sorted runs are merged: A stable merge preserves the relative order of equivalent records when combining previously sorted runs.
Existing order carries business meaning: The input might represent priority, creation time, approval order, or another meaningful sequence.
Tie handling should follow the input: Stability makes the treatment of equal records deterministic relative to their input order.
Stability is distinct from determinism. An unstable but deterministic sorting implementation may repeatedly produce the same output for the same input; it simply does not guarantee that tied records retain their input order.
Stable and Unstable Sorting
Algorithm
Stable?
Best time
Average time
Worst time
Auxiliary space
Reason
Merge sort
Yes
O(n log n)
O(n log n)
O(n log n)
O(n)
Dividing and merging requires linear merge storage.
Timsort
Yes
O(n)
O(n log n)
O(n log n)
O(n) worst case
It detects runs and merges them using temporary storage.
Insertion sort
Yes
O(n)
O(n²)
O(n²)
O(1)
Each insertion may shift a prefix of the list.
Quicksort
Usually no
O(n log n)
O(n log n)
O(n²)
O(log n) best/average, O(n) worst
Partitioning costs O(n) per level, while recursion determines stack use.
Heap sort
Usually no
O(n log n)
O(n log n)
O(n log n)
O(1)
In-place heap construction and sift-down avoid auxiliary arrays.
These costs assume standard implementations. Auxiliary space excludes the input and includes temporary storage and the recursion stack. Stability is independent of asymptotic complexity; making a sorting algorithm stable does not by itself determine its time or space cost.
Python 3.12 Behavior
Python guarantees that both sorted() and list.sort() are stable. Records with equal keys therefore retain their relative input order. list.sort() modifies the existing list, while sorted() returns a new list.
Both operations use Python’s adaptive Timsort implementation: O(n) in the best case and O(n log n) on average and in the worst case. They may use O(n) temporary storage in the worst case. sorted() additionally returns an O(n)-element list; the input is excluded from these space calculations.
A sorting key must produce mutually comparable values when necessary. Stability does not make invalid comparisons, such as ordering mixed strings and integers, valid.
Relevant edge cases behave naturally:
Empty and one-element inputs remain unchanged.
Duplicate keys are exactly where stability becomes observable.
Negative numeric keys follow the same ordering rules as positive keys.
Already sorted input benefits from Timsort’s run detection and runs in O(n) time.
Invalid or mutually incomparable key types can raise TypeError.
Common Pitfalls
Confusing stability with sorting accuracy: A stable sort still orders elements correctly by the key; stability only preserves order among ties.
Assuming every sorting algorithm is stable: Stability must be checked for the particular algorithm and implementation.
Applying a key-based sort incorrectly: The key should define equality. Adding a unique field removes ties and may change the intended result.
Ignoring multi-pass ordering: Every stage must use a stable sort to preserve earlier ordering among ties.
Equating stability with reproducibility: Stability defines tie order relative to the input, but it is not the same property as deterministic execution.
Interview Tips
A useful follow-up is: “How would you sort by one field while preserving another field as the tie-breaker?” Use a stable sort with the primary key and rely on its existing input order, or construct a composite key containing both fields when that precisely matches the requirement. Also mention that stability affects ordering semantics, not the sorting algorithm’s asymptotic complexity by itself.
69
How does the counting sort algorithm work?
KnownRevise
Counting sort is a non-comparison sorting algorithm that sorts integer values by counting how many times each value occurs, then calculating their final positions. It is efficient when the range of possible values, k, is small relative to the number of elements, n.
Read full answerHide full answer
Applies toPython 3.12
How It Works
Counting sort typically follows these steps:
Find the minimum and maximum values to determine the input range.
Create a count array where each index represents a possible value.
Count the occurrences of every value.
Convert those counts into cumulative totals, giving the ending position of each value.
Traverse the input from right to left and place each value into its final position.
Traversing backward makes the implementation stable: elements with equal keys retain their original relative order.
The algorithm supports positive numbers, negative numbers, and duplicates. Negative values are handled by subtracting the minimum value when calculating count-array indexes.
Complexity
Let:
n be the number of elements.
k = max(values) - min(values) + 1 be the number of possible integer values.
Case
Time
Auxiliary space
Best
O(n + k)
O(n + k)
Average
O(n + k)
O(n + k)
Worst
O(n + k)
O(n + k)
Time: Counting and placement each take O(n), while calculating cumulative counts takes O(k).
Auxiliary space: The count array uses O(k), and the sorted output uses O(n). This excludes the original input; including it, total memory remains O(n + k).
Example Walkthrough
For the input:
[4, 2, 2, 8, 3, 3, 1]
The range is 1 through 8, so the count array has eight positions.
Occurrence counts are [1, 2, 2, 1, 0, 0, 0, 1].
Cumulative counts are [1, 3, 5, 6, 6, 6, 6, 7].
Traversing backward places each value into the position immediately before its cumulative count.
The result is [1, 2, 2, 3, 3, 4, 8].
Comparison With Other Approaches
Approach
Time
Extra space
When to use
Repeatedly find the minimum
O(n²)
O(1)
Only as a simple baseline
General comparison sorting
O(n log n)
Typically O(n) or less
Large or unbounded value ranges
Counting sort
O(n + k)
O(n + k)
Small integer ranges relative to n
Counting sort avoids the O(n log n) comparison lower bound because it does not compare values against one another. It is most effective when k is close to n; otherwise, its count array can consume excessive memory.
Code Example
def counting_sort(values: list[int]) -> list[int]:
"""Return a stably sorted copy of a list of integers using counting sort."""
if len(values) < 2:
return values.copy()
minimum = min(values)
maximum = max(values)
value_range = maximum - minimum + 1
# Count each value, using an offset so negative values are supported.
counts = [0] * value_range
for value in values:
counts[value - minimum] += 1
# Convert occurrence counts into cumulative ending positions.
for index in range(1, value_range):
counts[index] += counts[index - 1]
# Work backward to preserve the original order of equal values.
sorted_values = [0] * len(values)
for value in reversed(values):
index = value - minimum
counts[index] -= 1
sorted_values[counts[index]] = value
return sorted_values
result = counting_sort([4, 2, 2, 8, 3, 3, 1])
print(result) # [1, 2, 2, 3, 3, 4, 8]
Common Pitfalls
Large value range: If k is millions while n is small, use a comparison sort or radix sort instead.
Incorrect cumulative count: Each value’s final position comes from the cumulative count, not its occurrence count alone.
Losing stability: Place elements by traversing the original sequence backward; forward placement can reverse equal elements.
Unnecessary assumptions: Counting sort is intended for integer-like keys. It does not directly sort arbitrary strings or floating-point values without an appropriate key transformation.
Input mutation: The implementation returns a new list and leaves the original unchanged.
Edge cases: Empty and one-element inputs are handled directly; duplicates and negative numbers are supported. Already sorted or reverse-sorted inputs still take O(n + k) time.
Interview Tips
Interviews often ask how you would handle a very large range, whether the algorithm is stable, or why it can outperform comparison sorting. Explain that counting sort trades additional memory proportional to the value range for linear-time operation over the input and range.
70
How do you sort a linked list using merge sort?
KnownRevise
Use merge sort to sort a linked list in (O(n log n)) time. It is well suited to linked lists because merging two sorted lists only requires relinking nodes, while avoiding the repeated indexing and copying associated with array-based sorting.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
Why Merge Sort
Linked lists are linear, linked-memory structures: each node stores a value and a reference to the next node.
Merge sort is stable: elements with equal values retain their original relative order.
It avoids an (O(n^2)) worst case, unlike naïve quicksort on a linked list.
The implementation below is bottom-up, so it does not use recursion and is not constrained by Python’s recursion limit.
How It Works
Initially, treat every individual node as a sorted run of length one.
Merge adjacent runs of the same size.
Double the run size after each pass until the entire list is one sorted run.
For the list:
4 -> 2 -> 5 -> 1 -> 3
The merge passes are:
Pass
Sorted runs merged
Result
1
4 and 2, 5 and 1, then 3 alone
2 -> 4 -> 1 -> 5 -> 3
2
2, 4 and 1, 5, then 3 alone
1 -> 2 -> 4 -> 5 -> 3
3
1, 2, 4, 5 and 3
1 -> 2 -> 3 -> 4 -> 5
During each merge, the smaller head node is selected and appended to the merged list. Using <= preserves the order of duplicate values.
Code Example
from __future__ import annotations
class Node:
"""A singly linked list node containing an integer."""
def __init__(self, value: int, next: Node | None = None) -> None:
self.value = value
self.next = next
def _split(head: Node | None, size: int) -> Node | None:
"""Detach the first size nodes and return the rest of the list."""
for _ in range(size - 1):
if head is None:
return None
head = head.next
if head is None:
return None
rest = head.next
head.next = None
return rest
def _merge(left: Node | None, right: Node | None, tail: Node) -> Node:
"""Append the merge of two sorted runs after tail; return the new tail."""
while left is not None and right is not None:
# <= keeps equal values in their original order (stability).
if left.value <= right.value:
tail.next, left = left, left.next
else:
tail.next, right = right, right.next
tail = tail.next
tail.next = left if left is not None else right
while tail.next is not None:
tail = tail.next
return tail
def sort_linked_list(head: Node | None) -> Node | None:
"""Sort an acyclic singly linked list in ascending order."""
if head is None or head.next is None:
return head
# Detect cycles with Floyd's tortoise-and-hare algorithm.
slow = head
fast = head
while fast is not None and fast.next is not None:
slow = slow.next
fast = fast.next.next
if slow is fast:
raise ValueError("The linked list contains a cycle.")
# Count the nodes so the merge width can grow until it covers the list.
node_count = 0
cursor = head
while cursor is not None:
node_count += 1
cursor = cursor.next
dummy = Node(0, head)
width = 1
while width < node_count:
tail = dummy
remaining = dummy.next
while remaining is not None:
# Detach two runs of up to `width` nodes each.
left = remaining
right = _split(left, width)
remaining = _split(right, width)
# Merge them and attach the result after the sorted prefix.
tail = _merge(left, right, tail)
width *= 2
return dummy.next
head = None
for value in reversed([4, 2, 5, 1, 3]):
head = Node(value, head)
sorted_head = sort_linked_list(head)
values = []
while sorted_head is not None:
values.append(sorted_head.value)
sorted_head = sorted_head.next
print(values) # [1, 2, 3, 4, 5]
Complexity
Measure
Best
Average
Worst
Reason
Time
(O(n log n))
(O(n log n))
(O(n log n))
There are (O(log n)) merge passes, and each pass processes all nodes.
Auxiliary space
(O(1))
(O(1))
(O(1))
The iterative implementation uses a constant number of references and no recursion stack.
Total space including the input list
(O(n))
(O(n))
(O(n))
The list’s existing nodes are reused; they are not copied.
Edge Cases and Trade-offs
Empty list: returns None.
One element: returns the original node.
Duplicate values: remain stable because the merge uses <=.
Negative numbers: are handled normally by numeric comparison.
Already sorted input: still takes (O(n log n)), although it remains stable.
Cyclic list: the implementation detects the cycle and raises ValueError instead of looping indefinitely.
Small or nearly sorted lists: insertion sort may be simpler and can be (O(n)) when nearly sorted, but its worst case is (O(n^2)).
Python has no built-in linked-list type, so applications commonly define a Node class or use another library.
Common Pitfalls
Do not use an array as a temporary buffer unnecessarily; relinking nodes is the key advantage of merge sort.
Ensure that the original segment is detached correctly before merging.
Use <= rather than < if stability is required.
A recursive merge-sort implementation uses (O(log n)) recursion-stack space and may exceed Python’s default recursion limit for very long lists.
In interviews, discuss whether the list is sorted in ascending or descending order, whether duplicates must be stable, and whether the input might contain a cycle.
71
What are the properties of a red-black tree?
KnownRevise
A red-black tree is a self-balancing binary search tree that uses node colors to guarantee logarithmic height. Its invariants prevent long root-to-leaf paths, so search, insertion, and deletion remain efficient even in the worst case.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
Core Properties
A red-black tree satisfies these five invariants:
Every node is either red or black: The color is stored as part of each node.
The root is black: This provides a consistent starting point for tree operations.
Every leaf is black: Leaves are usually represented by shared NIL sentinel nodes rather than allocated objects.
A red node has black children: This prevents consecutive red nodes from creating long paths with too few black nodes.
Equal black-height: Every path from a node to its descendant NIL leaves contains the same number of black nodes. Black-height normally excludes the starting node.
The equal black-height property implies that the tree height is O(log n). More precisely, a red-black tree containing n nodes has height at most 2 log₂(n + 1).
How It Maintains Balance
Insertion begins like an ordinary binary search tree insertion, but the new node is initially colored red. Coloring it red avoids changing the number of black nodes on existing paths. The tree may then violate the red-parent and root-color rules.
The implementation repairs these violations using:
Recoloring: Changes node colors to distribute black nodes more evenly.
Rotations: Restructure local parts of the tree without changing its in-order sequence.
A left rotation moves a right child upward.
A right rotation moves a left child upward.
Deletion can remove a black node and reduce the black-height of some paths. A deletion fix-up procedure uses recoloring and rotations to restore all invariants.
Classification and Python Mapping
Category: Non-linear, dynamic, non-primitive tree structure.
Memory layout: Linked nodes rather than contiguous storage.
Ordering: Maintains keys in sorted order.
Python 3.12: No built-in red-black tree exists. A custom implementation is required; dict and set are hash tables, while heapq is a binary min-heap, so neither is a red-black tree.
Operation Complexity
The tree remains balanced after standard insertion and deletion fix-ups, so average and worst-case bounds are the same.
Operation
Average
Worst case
Reason
Search by key
O(log n)
O(log n)
Tree height is O(log n)
Insert
O(log n)
O(log n)
Search plus O(log n) recoloring and rotations
Delete
O(log n)
O(log n)
Search plus O(log n) deletion fix-up
Minimum or maximum
O(log n)
O(log n)
Follows one root-to-leaf path
In-order traversal
O(n)
O(n)
Visits every node
Build using n inserts
O(n log n)
O(n log n)
Each insertion costs O(log n)
The stored tree requires O(n) space, excluding the input. Search and iterative balancing use O(1) auxiliary space; recursive traversal or deletion uses O(log n) stack space because the tree height is logarithmic.
Comparison with Other Search Trees
Structure
Access by known node
Search by key
Insert
Delete
Extra memory per element
Key order
Red-black tree
O(1) average and worst
O(log n) average and worst
O(log n) average and worst
O(log n) average and worst
O(1): links, color, and key/value
Sorted
AVL tree
O(1) average and worst
O(log n) average and worst
O(log n) average and worst
O(log n) average and worst
O(1): links and balance metadata
Sorted
Unbalanced BST
O(1) average and worst
O(n) average and worst
O(n) average and worst
O(n) average and worst
O(1): links and key/value
Sorted
Red-black trees provide predictable logarithmic performance and generally require fewer rotations than AVL trees during insertion. AVL trees are an alternative when read-heavy workloads justify their stricter balancing and slightly higher update cost. Positional access, such as finding the k-th smallest key, is not provided by these trees unless additional metadata is stored.
Common Pitfalls
Do not confuse black-height with the total path length; black-height counts only black nodes.
Do not claim that all leaves are at the same depth—only their black-heights are equal.
Remember that NIL leaves are black and that the root is black.
A red node cannot have a red child.
Mutations such as insertion and deletion must complete their fix-up procedure.
An interviewer may follow up by asking why rotations preserve sorted order, how deletion fix-up works, how the tree differs from an AVL tree, or how the structure could be made thread-safe.
72
What is a B-tree and where is it used?
KnownRevise
A B-tree is a self-balancing, non-linear tree designed to store sorted keys efficiently, especially when data is kept on disks or SSDs. Its nodes contain many keys and child pointers, reducing the number of storage reads required compared with a binary search tree.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
Structure and Properties
A B-tree has several important characteristics:
Non-linear and non-primitive: It is built from linked nodes rather than a single contiguous array.
Balanced: All leaves are at the same depth, so the height remains logarithmic.
Multi-way: Each node can have many children, unlike a binary tree, which has at most two.
Sorted: Keys are maintained in ascending order within each node.
Dynamic: Nodes split during insertion and merge or redistribute during deletion.
Storage-friendly: A node is commonly sized to fit within a disk page or storage block.
If a node has k keys, it has k + 1 children. The exact minimum occupancy depends on the tree’s degree, and definitions vary between textbooks. A common degree-t formulation allows at most 2t - 1 keys per node and at least t - 1 keys in every non-root node.
Unlike a binary search tree, a B-tree is not limited to two children. This high fan-out keeps the tree shallow and reduces costly storage I/O.
How It Works
Search compares the target key with the sorted keys in the current node:
If the key matches, the search succeeds.
If the target is smaller than the first key, descend into the first child.
Otherwise, compare it with the remaining keys to identify the appropriate child.
Continue until the key is found or a leaf is reached.
Insertion descends to the appropriate leaf and inserts the key there. If a node exceeds its capacity, it splits, and an entry is promoted to its parent. Splitting continues toward the root when necessary.
Deletion removes the key and restores the tree’s occupancy rules. This can involve borrowing a key from a sibling or merging nodes. Both operations preserve equal leaf depth.
Complexity
The following costs assume a fixed maximum node size and a tree containing n keys:
Operation
Best
Average
Worst
Reason
Search
O(1)
O(log n)
O(log n)
Descends through a logarithmic-height tree
Insert
O(log n)
O(log n)
O(log n)
Searches for the leaf, then performs bounded node splits
Delete
O(log n)
O(log n)
O(log n)
Searches, removes, and performs bounded rebalancing
In-order traversal
O(n)
O(n)
O(n)
Visits every key
Total space: O(n), including stored keys, values, child pointers, and node metadata.
Search auxiliary space: O(1) with an iterative implementation, excluding the tree itself.
Recursive traversal space: O(log n) for the recursion stack; an iterative traversal can use O(1) auxiliary space.
The tree height is O(log n) because minimum node occupancy prevents long chains of sparsely populated nodes.
Practical Uses
B-trees and closely related B+ trees are common in:
Database indexes: Used for equality lookups, sorted iteration, prefix searches, and range queries such as finding all orders placed after a date.
File-system indexes: Some systems use B-tree variants to locate files without scanning every directory entry.
Persistent maps and ordered key-value stores: Useful when data exceeds memory and must be accessed from disk or flash storage.
Database and filesystem implementations: B+ trees are especially common because they keep records in leaves and link those leaves for efficient range scans.
A classic B-tree may store values in both internal and leaf nodes. A B+ tree usually stores values only in leaves, making internal nodes smaller and range traversal more efficient.
Python 3.12 Availability
Python 3.12 has no built-in B-tree implementation. The standard-library alternatives have different trade-offs:
dict and set provide O(1) average lookups but do not support efficient ordered range traversal.
list supports binary search with bisect, but inserting into the middle is O(n).
Third-party libraries may provide sorted or disk-backed mappings, but the exact implementation and guarantees must be checked separately.
Common Pitfalls
Calling every database B-tree a B+ tree: Many database indexes use B+ trees, which differ in where values are stored.
Ignoring storage locality: A B-tree’s main advantage is its high fan-out, not merely its O(log n) search.
Confusing balance with sorted output: A B-tree maintains sorted keys, but obtaining a fully sorted sequence may still require an in-order traversal.
Overlooking worst cases: Hash tables are O(1) on average but O(n) after heavy collisions; B-trees retain logarithmic height because they are balanced.
Common follow-up questions: Interviewers may ask about B+ trees, node splits and merges, range queries, concurrency, or how a B-tree compares with a hash table.
73
What is a segment tree used for?
KnownRevise
A segment tree is a tree-based data structure that represents an array as a hierarchy of intervals, allowing range queries and point updates to be performed efficiently. It is commonly used for range aggregation, such as calculating traffic totals, scores, or sensor readings over time intervals.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
Core Concept
A segment tree is a non-linear, non-primitive, static tree structure: its topology is normally fixed for a given array size, although the aggregates stored in it can change.
Leaves store individual array elements or padding values.
Internal nodes store an aggregate of their children, such as a sum, minimum, or maximum.
Each node represents a contiguous interval.
The root represents the complete underlying range.
The usual one-aggregate-per-node design assumes an associative combining operation with an identity value. For sums, the identity is 0; for minimums, it is positive infinity.
Python does not include a built-in segment tree. It is commonly implemented using a contiguous list, which provides better cache locality than pointer-based node objects.
How It Works
For the array [1, 2, 3, 4, 5], an implementation may pad it to eight leaves:
Leaves: 1 2 3 4 5 0 0 0
A query for the half-open interval [1, 4) proceeds as follows:
Select the leaf containing 2.
Select the internal interval containing 3 and 4.
Combine the selected values: 2 + (3 + 4) = 9.
Stop because the requested range is fully covered.
A point update changes one leaf and recalculates its ancestors. Only that leaf-to-root path is affected.
Range updates, such as adding a value to every element in an interval, normally require lazy propagation so that deferred work is propagated only when a later query needs it.
Complexity
For an array containing n elements:
Operation
Time
Reason
Build
O(n)
Every tree node is calculated once
Point update
O(log n)
One leaf and its ancestors are recalculated
Range query
O(log n)
The range is decomposed into O(log n) tree intervals
Storage
O(n)
The tree stores O(n) nodes
These deterministic operations have the same best, average, and worst-case bounds.
The tree requires O(n) auxiliary space, excluding the input, and O(n) total space when the input is included. A recursive implementation may additionally use O(log n) recursion-stack space, whereas an iterative implementation uses O(1) stack space. These bounds assume n > 0; an empty tree requires O(1) space.
Practical Application
A practical use is maintaining aggregate sensor or network readings over a time series:
Each leaf represents one time interval’s reading.
Queries calculate the total, minimum, or maximum between two timestamps.
A new reading updates one leaf in O(log n).
Many requests can obtain interval totals without rescanning the underlying data.
Other suitable aggregates include range maximum, range minimum, and other associative values. Segment trees are less suitable for general-purpose insertion and deletion of array positions because their fixed interval structure makes those operations comparatively expensive.
Common Pitfalls
Off-by-one errors: Clearly define whether ranges use inclusive or half-open endpoints.
Incorrect padding: Padding must contain the aggregate operation’s identity value.
Confusing update types: Point updates do not require lazy propagation; range updates normally do.
Ignoring edge cases: An empty array permits only empty-range queries, a one-element tree is valid, and duplicates and negative values work with sums.
Unnecessary assumptions: Input order does not matter, so the source array does not need to be sorted.
Memory overhead: A segment tree stores multiple aggregate values per element rather than only the original data.
A common interview follow-up is: “How would you support range updates and range queries?” The expected answer usually involves lazy propagation.
74
How does a suffix tree enable efficient substring searching?
KnownRevise
A suffix tree is a compressed trie containing all suffixes of a string, allowing a pattern to be located by following one tree path instead of testing every possible starting position. It is a non-linear, usually static indexing structure used for repeated substring searches, repeated-sequence detection, and longest-common-prefix calculations.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
Structure and Purpose
The source string S is augmented with a unique end marker, such as $, that cannot appear in the original text.
Every suffix of S$ is represented by a root-to-leaf path.
Edges are compressed: an edge labeled ana represents three characters without storing a separate node for each one.
A suffix link points from a node to the node representing a related suffix. Suffix links are useful during construction and advanced operations, but are not required for basic substring search.
Unlike a suffix array, a suffix tree stores the suffix relationships directly as paths.
For example, the suffixes of banana$ include banana$, anana$, nana$, ana$, na$, a$, and $.
How Substring Search Works
To search for a pattern:
Start at the root.
Select the transition matching the pattern’s first character.
Compare the remaining pattern characters with the compressed edge label.
If a mismatch occurs, the pattern is absent.
If the pattern ends inside an edge or at a node, a matching suffix exists.
For example, ana follows the a path and then matches the edge labeled na. The matching subtree represents the suffixes anana$ and ana$, so the pattern occurs at positions 1 and 3.
Searching for all occurrences means collecting the suffix endings in the matched subtree. With a pattern of length m and z occurrences, this takes O(m + z) time.
Complexity
For a fixed alphabet and constant-time transition lookup:
Ukkonen construction: best, average, and worst case Θ(n) time and Θ(n) additional space, excluding the original input.
Existence query: best, average, and worst case Θ(m) time and O(1) auxiliary space, excluding the stored index.
All-occurrence query: Θ(m + z) time and O(z) output space.
The implementation below uses direct suffix insertion for clarity. Its build complexity is best Θ(n), average Θ(n log n) under a random fixed-alphabet model, and worst Θ(n²); it uses Θ(n) space.
Construction and querying are iterative, so there is no recursion-stack cost.
Approach
Preprocessing
Worst-case query
Extra space
When to use
Brute-force scanning
None
O(nm)
O(1)
One-off search on a short string
Suffix tree with Ukkonen
O(n)
O(m)
O(n)
Many queries over a large corpus
Direct suffix insertion
O(n²) worst case
O(m)
O(n)
Teaching and small examples
Example Walkthrough
Searching for ana in banana$:
Step
Current location
Action
1
Root
Choose the a transition
2
Edge beginning with a
Match a; the pattern still needs na
3
Following edge
Match na
4
Matched subtree
Pattern ends; suffixes at positions 1 and 3 matched
Code Example
from __future__ import annotations
from dataclasses import dataclass, field
@dataclass
class _Node:
# character -> (child node, edge start, edge length)
children: dict[str, tuple[_Node, int, int]] = field(default_factory=dict)
class SuffixTree:
def __init__(self, text: str) -> None:
"""Build an educational compressed suffix tree for text."""
self.original_length = len(text)
# Choose an end marker that cannot occur in the original text.
self.sentinel = chr(0)
while self.sentinel in text:
self.sentinel = chr(ord(self.sentinel) + 1)
self.source = text + self.sentinel
self.root = _Node()
for start in range(len(self.source)):
self._insert_suffix(start)
def _insert_suffix(self, start: int) -> None:
suffix_length = len(self.source) - start
node = self.root
offset = 0
while offset < suffix_length:
key = self.source[start + offset]
entry = node.children.get(key)
if entry is None:
leaf = _Node()
node.children[key] = (
leaf,
start + offset,
suffix_length - offset,
)
return
child, edge_start, edge_length = entry
limit = min(edge_length, suffix_length - offset)
matched = 0
while (
matched < limit
and self.source[start + offset + matched]
== self.source[edge_start + matched]
):
matched += 1
if matched == limit:
node = child
offset += matched
if offset == suffix_length:
return
continue
# Split the existing edge at the mismatch position.
middle = _Node()
node.children[key] = (middle, edge_start, matched)
middle.children[self.source[edge_start + matched]] = (
child,
edge_start + matched,
edge_length - matched,
)
# Store the new suffix as a leaf below the split node.
leaf = _Node()
middle.children[self.source[start + offset + matched]] = (
leaf,
start + offset + matched,
suffix_length - offset - matched,
)
return
def contains(self, pattern: str) -> bool:
"""Return whether pattern occurs in the indexed original text."""
if not pattern:
return True
if self.sentinel in pattern:
return False
node = self.root
pattern_index = 0
while pattern_index < len(pattern):
entry = node.children.get(pattern[pattern_index])
if entry is None:
return False
child, edge_start, edge_length = entry
matched = 0
while (
matched < edge_length
and pattern_index + matched < len(pattern)
):
if (
self.source[edge_start + matched]
!= pattern[pattern_index + matched]
):
return False
matched += 1
pattern_index += matched
if pattern_index == len(pattern):
return True
node = child
return True
def substring_exists(tree: SuffixTree, pattern: str) -> bool:
"""Return whether pattern is a substring of the indexed text."""
return tree.contains(pattern)
tree = SuffixTree("banana")
print(substring_exists(tree, "ana")) # True
print(substring_exists(tree, "ananas")) # False
Common Pitfalls
Confusing suffix trees with suffix arrays: a suffix tree stores suffix paths; a suffix array stores suffix starting positions in sorted order.
Requiring a complete leaf match: the pattern may end inside a compressed edge and still be a valid substring.
Using an end marker already present in the text: this can create invalid suffix relationships.
Ignoring construction cost: a O(m) query is useful only when the same index serves many searches; use Ukkonen’s O(n) construction in production.
Forgetting memory overhead: the tree stores Θ(n) nodes and edge metadata in addition to the source text.
A common interview follow-up is: “How would you retrieve every occurrence, or calculate the longest common prefix between two suffixes?”
75
What is a trie, and when should you use one?
KnownRevise
A trie, or prefix tree, stores strings by sharing their common prefixes. It is useful when many lookups need exact matching, prefix matching, or autocomplete.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
What It Is
A trie is a non-linear, dynamic, non-primitive tree structure. It implements the behavior of a prefix dictionary: each node represents a path from the root, and a terminal marker indicates whether that path forms a complete key.
Prefix sharing: "cat" and "car" share the path "ca".
Dynamic size: the structure grows as keys are inserted.
Linked memory: nodes reference their children rather than storing all paths in one contiguous array.
Python mapping: Python 3.12 has no built-in trie; dict and set use hash tables instead.
How It Works
Each node normally contains:
A map from the next character to another node.
A flag indicating whether a key ends at that node.
For a word of length L:
Insertion: O(L) best, average, and worst time with a bounded character alphabet.
Exact search: O(L) best, average, and worst time.
Prefix search: O(P) time, where P is the prefix length.
Enumerating a prefix: O(P + K), where K is the number and total size of returned keys.
The stored structure uses O(M) space, where M is the total number of characters across all stored keys. Searches use O(1) additional space. Input strings are not counted separately, and these operations are iterative, so there is no recursion stack.
When to Use It
A trie is a good choice when the required operation is prefix-based:
Autocomplete and typeahead search
Spell-checking and word games
Dictionary prefix matching
IP-route lookup using a binary trie
Searching for all keys beginning with a specified prefix
It is less suitable for only exact membership testing, where a dict or set is usually simpler and has lower constant overhead. A trie also consumes more memory than a hash table when keys share few prefixes.
Alternatives and Costs
The costs below assume bounded alphabet size, balanced trees, and constant-time comparison or precomputed hashing.
Structure
Access: best / average / worst
Search: best / average / worst
Insert: best / average / worst
Delete: best / average / worst
Extra memory per element
Order kept
Trie
No random access; prefix path O(L)
O(L) / O(L) / O(L)
O(L) / O(L) / O(L)
O(L) / O(L) / O(L)
O(M) total; roughly O(1) metadata and references per node
Prefix relationships, but not full sorted order unless traversal is sorted
Hash table (dict, set)
O(1) / O(1) / O(n)
O(1) / O(1) / O(n)
O(1) / O(1) / O(n)
O(1) / O(1) / O(n)
O(n) total, about O(1) per key
No
Balanced BST
O(log n) / O(log n) / O(log n)
O(log n) / O(log n) / O(log n)
O(log n) / O(log n) / O(log n)
O(log n) / O(log n) / O(log n)
O(n) total, including pointers and metadata
Yes
Hashing an L-character string itself costs O(L), while comparing long strings in a balanced BST can also add a factor proportional to the compared string length.
Common Pitfalls
Ignoring key length: trie operations are not truly O(1) for strings of unbounded length.
Forgetting the terminal marker: a matching prefix is not necessarily a complete stored key.
Underestimating memory: every distinct prefix may require a separate node and child map.
Assuming sorted output: dictionary iteration order does not imply lexicographic trie order.
Ignoring edge cases: duplicate insertion is idempotent, an empty string is represented by the root, and matching is normally case-sensitive.
A common interview follow-up is how to reduce trie memory using compressed edges, producing a radix tree, or how to enumerate all matching words without scanning the entire collection.
76
When is a Bloom filter better than a hash table?
KnownRevise
A Bloom filter is a probabilistic set that answers whether an item is probably present using a compact bit vector. It is useful when false positives are acceptable, such as avoiding unnecessary database or cache lookups, because it uses substantially less memory than a traditional hash table.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
How It Works
A Bloom filter stores a bit vector of size m and derives several bit positions from each inserted key using hash functions.
Initialize all m bits to zero.
When inserting an item, compute k hash positions and set those bits to one.
When checking an item, compute the same positions.
If any bit is zero, the item is definitely absent.
If all bits are one, the item is probably present, but it might be a false positive.
A properly implemented Bloom filter has no false negatives, although false positives become more likely as it fills. For n inserted items, m bits, and k hash functions, the approximate false-positive probability is:
[ p approx (1-e^{-kn/m})^k ]
The usual space-efficient choices are:
[ m approx -frac{nln(p)}{(ln 2)^2}, qquad k approx frac{m}{n}ln 2 ]
For example, targeting a 1% false-positive rate requires roughly 10 bits per inserted item and about seven hash functions.
Bloom Filter vs. Hash Table
The complexities below count storage owned by the data structure and exclude the original input. No recursion stack is involved.
Structure
Access
Search / Membership
Insert
Delete
Extra Space
Order Kept
Bloom filter
Exact value retrieval is unavailable
O(k) best, average, and worst; probabilistic result
O(k) best, average, and worst
Not supported by the standard design
O(m) bits, usually O(n) bits for a fixed target error rate
No
Hash table
O(1) best and average; O(n) worst
O(1) best and average; O(n) worst
O(1) average and amortized with resizing; O(n) worst
O(1) average; O(n) worst
O(n) entries and slots, excluding input
Usually not guaranteed
A hash table stores actual keys or references to values, so it can retrieve exact results, iterate through its contents, update entries, and delete them. A Bloom filter only represents membership, so it cannot return the stored value or distinguish between a genuinely present key and a false positive.
Small Example
Assume a Bloom filter has 16 bits and uses two positions per key:
Inserting A sets bits 1 and 7.
Inserting B sets bits 2 and 9.
Inserting C sets bits 3 and 12.
Querying D computes positions 1 and 12. Both are set, so the filter reports “probably present,” even though D was never inserted.
Querying E computes positions 4 and 10. Both are clear, so the filter correctly reports “definitely absent.”
Increasing m lowers the false-positive rate at the cost of memory. k has an optimum near (m/n) ln 2; moving it away from that value in either direction raises the rate.
When to Use It
Use a Bloom filter when:
Most membership queries are for absent items.
A moderate false-positive rate is acceptable.
Memory usage is the main constraint.
Exact retrieval is unnecessary.
The filter will not require ordinary deletion.
Typical uses include database read filters, cache admission checks, large URL or file-existence checks, and distributed membership protocols.
A practical pattern is to query the Bloom filter first and consult the authoritative hash table or database only when the filter reports “probably present.” This avoids expensive storage lookups for many absent keys.
Common Pitfalls
Do not treat “probably present” as proof. A false positive is possible, while a false negative should not occur in a correct implementation.
Standard Bloom filters do not support deletion. Clearing bits could remove evidence belonging to another key. Use a counting Bloom filter or another deletion-capable variant when required.
Do not confuse compact storage with exact storage. A Bloom filter saves memory by discarding the original keys and values.
Replacing a hash table entirely is usually incorrect. The common architecture is a Bloom filter in front of a hash table or database.
Choose m and k for the expected insertion count and target error rate. Adding too many keys without resizing sharply increases false positives.
Watch edge cases: an empty filter reports every item absent, duplicates do not indicate duplicate counts, and the filter should be rebuilt rather than grown unsafely when the expected item count changes.
77
What is a disjoint set and how does it solve network connectivity?
KnownRevise
A disjoint-set (also called union-find) is a data structure that manages a collection of elements partitioned into non-overlapping sets. It supports two core operations: find, which identifies the set containing an element, and union, which merges two sets. In network connectivity, it efficiently determines whether two nodes are connected by tracking which nodes share a common set.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
How It Works
Each element initially belongs to its own singleton set.
Elements are stored in a parent array where every node points to its parent; a node that points to itself is a root, representing the set.
find(x) walks up parent pointers until it reaches the root, then applies path compression by making every node on the path point directly to the root.
union(x, y) merges the sets containing x and y by linking the root of one tree to the root of the other.
Union by rank (or size) attaches the shorter tree under the taller one, keeping the structure flat and preventing degenerate chains.
Key Operations and Complexity
Operation
With both optimizations
With union by rank only
Without optimizations
find
O(α(n)) amortized
O(log n) amortized
O(n) worst case
union
O(α(n)) amortized
O(log n) amortized
O(n) worst case
Space
O(n)
O(n)
O(n)
α(n) is the inverse Ackermann function, which grows so slowly that it is effectively a small constant (≤ 4 for any realistic input size). The amortized cost means that a sequence of operations is fast even if a single operation occasionally takes longer. Space is O(n) for the parent and rank arrays, and the input itself is not counted.
Network Connectivity Use Case
Model each node as an element and each edge as a union call between its endpoints.
Two nodes are connected if and only if find(x) == find(y).
The number of connected components equals the number of roots (nodes where parent[i] == i).
This is the core of Kruskal’s algorithm for minimum spanning trees, where edges are processed in weight order and union is skipped if the endpoints are already connected.
Common Pitfalls
Forgetting path compression or union by rank turns find into O(n) worst case, which fails on large inputs.
Using a list as the parent array and mutating it incorrectly can break the tree structure; always update the root’s parent, not an arbitrary node.
Treating union as successful when both elements are already in the same set — always check find(x) == find(y) first.
Interviewers often follow up by asking how to count connected components, or how to extend this to Kruskal’s algorithm or dynamic connectivity with deletions.
78
What is a Fenwick tree and when should you use it?
KnownRevise
A Fenwick tree, also called a binary indexed tree, is an array-based structure for efficient point updates and prefix-sum queries. It is especially useful when values change frequently but queries mainly ask for prefix or range sums.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
How It Works
A Fenwick tree stores aggregates rather than the original elements individually. It uses one-based indexing and the expression index & -index, called the lowbit, to determine the block represented by each tree entry:
tree[index] stores the sum from index - lowbit(index) + 1 through index.
For values [5, 3, 7], the internal tree is:
Index
Stored value
Covered group
1
5
[5]
2
8
[5, 3]
3
7
[7]
A prefix query for the first three elements adds tree[3] and tree[2], producing 7 + 8 = 15.
The supported operations are:
Point update: Add a delta to one element in O(log n) time.
Prefix sum: Sum positions 1 through index in O(log n) time.
Range sum: Calculate prefix_sum(right) - prefix_sum(left - 1) in O(log n) time.
Linear-time construction: Build the tree in O(n) time.
Order-statistic query: With nonnegative weights, binary lifting can find the smallest index whose prefix sum reaches a target in O(log n) time.
Structurally, a Fenwick tree is a non-primitive, non-linear, tree-shaped structure implemented with contiguous array storage. It is normally fixed-capacity for a fixed sequence length, although its values remain mutable. It is a specialized implementation of the prefix-sum interface, not a general-purpose ordered map or set. Contiguous storage also makes it relatively cache-friendly. Python 3.12 has no built-in specialized Fenwick tree, although a Python list can store its aggregate array.
Complexity
All costs are deterministic, so average and worst-case costs are the same.
Operation
Time
Reason
Build
O(n)
Each entry is propagated to its parent once
Point update
O(log n)
Visits a chain of Fenwick ancestors
Prefix or range sum
O(log n)
Visits disjoint groups or combines two prefixes
Exact-value search
O(n)
The tree does not directly index arbitrary values
Space is O(n) for the tree, excluding the input, or O(n) when the original input is also counted. Each operation uses O(1) auxiliary space and an O(1) recursion stack because the implementation is iterative.
When to Use It
A Fenwick tree is a good choice when:
Prefix or range sums are queried frequently.
Individual values are updated frequently.
Memory usage matters; it usually needs fewer aggregate slots than a segment tree.
Values represent frequencies, such as in dynamic rankings, counting, inversion counts, and order statistics.
A real-world system needs features such as a leaderboard whose scores change frequently.
It is less suitable when queries need range minimum, maximum, or other non-additive operations, or when arbitrary range updates are required.
Structure
Access (average; worst)
Exact-value search (average; worst)
Point update (average; worst)
Insert new position (average; worst)
Delete fixed slot (average; worst)
Extra memory per element
Sorted-key order
Fenwick tree
O(log n); O(log n)
O(n); O(n)
O(log n); O(log n)
O(n); O(n)
O(log n); O(log n)
O(1), about one slot per element
No
Segment tree
O(log n); O(log n)
O(n); O(n)
O(log n); O(log n)
O(n); O(n)
O(log n); O(log n)
O(1), usually a larger constant
No
Static prefix-sum array
O(1); O(1)
O(n); O(n)
O(n); O(n)
O(n); O(n)
O(n); O(n)
O(1)
No
For the Fenwick tree and segment tree, “delete fixed slot” means setting an existing indexed value to zero without shifting later positions. A true insertion or removal that shifts sequence positions takes O(n).
A segment tree is the better alternative when queries require composable operations such as range minimum or maximum, for example in interval analytics. A plain prefix-sum array is faster for static cumulative totals, such as read-only revenue summaries, but must be rebuilt after an update.
Common Pitfalls
Indexing confusion: Fenwick positions are one-based, while Python lists are zero-based.
Wrong traversal direction: Updates move upward with index += index & -index; queries move downward with index -= index & -index.
Negative-value assumptions: Ordinary sum queries permit negative values and deltas, but binary-lifting order-statistic searches require nonnegative weights and monotonically increasing prefix sums.
Integer overflow: Python integers do not overflow, but implementations in languages such as C++ or Java may require 64-bit sums.
Edge cases: An empty tree supports only the empty prefix sum of zero. One element, duplicate values, and already sorted input need no special handling. Disconnected graphs and cycles are not applicable because a Fenwick tree represents a linear sequence.
Interviewers commonly follow up by comparing Fenwick trees with segment trees, asking how range sums are derived from prefixes, or how frequency counts support rank queries.
79
How do AVL trees differ from red-black trees?
KnownRevise
AVL trees and red-black trees are both self-balancing binary search tree implementations, but AVL trees maintain stricter balance and generally provide faster lookups, while red-black trees require fewer structural changes during updates. Both preserve keys in sorted order and support the same core operations in O(log n) time.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
Core Differences
Both structures are:
Non-linear tree data structures.
Non-primitive, because each node stores data and references to other nodes.
Dynamic, since they can grow and shrink as elements are inserted or removed.
Typically implemented as linked structures, with each node containing child references and possibly a parent reference.
Their balancing rules differ:
AVL tree: For every node, the heights of its left and right subtrees differ by at most one.
Red-black tree: Nodes are colored red or black and satisfy balancing constraints, including equal black-node counts along paths through the tree.
AVL trees are therefore usually shallower. Their height is approximately O(log n) with a smaller constant than red-black trees, whose height is at most approximately 2 log2(n + 1). A shallower tree generally means fewer nodes and cache misses during a lookup.
Operation Costs
Operation or property
AVL tree
Red-black tree
Access by key
O(log n) average and worst case
O(log n) average and worst case
Search by key
O(log n) average and worst case
O(log n) average and worst case
Insert
O(log n) average and worst case
O(log n) average and worst case
Delete
O(log n) average and worst case
O(log n) average and worst case
Find minimum or maximum
O(log n) worst case
O(log n) worst case
Extra metadata per node
O(1), including a height value
O(1), including a color value
Total node storage
O(n)
O(n)
Key order preserved
Yes, in-order traversal is sorted
Yes, in-order traversal is sorted
Balancing repair
Usually more rotations
Usually fewer rotations
The update operation includes locating the key, modifying the tree, and restoring balance. Although both use O(log n) time, red-black trees commonly perform fewer rotations, making them efficient for workloads with frequent updates.
For O(n) total tree storage, a recursive implementation may also use an O(log n) auxiliary recursion stack. An iterative implementation can use O(1) auxiliary space. These auxiliary bounds exclude the input tree itself; if the tree is considered the stored data rather than input, its O(n) node storage must also be counted.
Rotation and Rebalancing Behavior
An AVL tree is restored using rotations after an insertion or deletion changes a subtree height beyond the permitted difference. Insertions generally require at most two rotations, while deletions can require several.
A red-black tree is restored using recoloring and rotations. Recoloring changes only metadata, while rotations restructure links. Because its balance condition is less strict, red-black trees usually require less work after updates.
Both algorithms rely on the following balancing principle:
A rotation preserves in-order key order.
Recalculating heights or black heights identifies the violated invariant.
Repairs are localized near the modified node, so they take O(log n) time or less.
When to Use Each
Choose an AVL tree when lookups and searches dominate the workload. Its stricter balance usually reduces the number of visited nodes. Ordered database indexes, read-heavy caches, and lookup-intensive in-memory structures are typical candidates.
Choose a red-black tree when insertions and deletions are frequent. Fewer rotations and recolorings often make updates cheaper while preserving logarithmic lookup time. Red-black trees are commonly used for ordered maps and sets in language libraries and runtimes.
Alternative: If ordering is not required, a hash table such as Python’s dict or set usually provides O(1) average-case access and insertion. Its worst case is O(n), however, and it does not preserve sorted iteration order.
Common Pitfalls
Choosing by habit: Select the structure based on the dominant operation—lookups, updates, ordering, or memory usage.
Ignoring worst cases: Both trees guarantee O(log n) operations, unlike basic unbalanced binary search trees, which can degrade to O(n).
Underestimating metadata: Every node needs child references; AVL trees additionally store height information, while red-black trees store color information.
Confusing them with hash tables: Trees preserve sorted order and support range queries, while hash tables provide average O(1) lookup without sorted order.
Interview follow-up: A common question asks which structure is faster. The usual answer is that AVL trees favor read-heavy workloads, while red-black trees favor update-heavy workloads; the actual result depends on implementation and workload.
80
Rope data structure implementation and use cases
KnownRevise
A rope is a binary tree of strings that stores text in its leaves, allowing efficient concatenation, insertion, and deletion on very large strings without copying the entire content. It is commonly used in text editors, version control systems, and any application that frequently modifies long strings.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
How It Works
Each leaf node holds a substring and has no children.
Each internal node stores a size equal to the total length of its left subtree, enabling fast splits.
Operations like concat, insert, and delete work by splitting the tree at a position and rebuilding pointers, which is O(log n) on average when the tree is balanced.
The worst case is O(n) if the tree becomes unbalanced, similar to a linked list.
Space complexity is O(n) because every character is stored once, plus overhead for node pointers.
Rope vs. Plain String
Operation
Rope (avg)
Rope (worst)
String
Insert
O(log n)
O(n)
O(n)
Delete
O(log n)
O(n)
O(n)
Concat
O(1)
O(1)
O(n)
Order kept
Yes
Yes
Yes
Extra memory
Node pointers per chunk
Node pointers per chunk
None
Code Example
class RopeNode:
"""A node in a rope: leaves hold text, internal nodes hold children."""
def __init__(self, text: str = "") -> None:
self.text = text
self.left: "RopeNode | None" = None
self.right: "RopeNode | None" = None
self.size = len(text)
def split(self, pos: int) -> tuple["RopeNode", "RopeNode"]:
"""Split this node at index `pos` into two nodes."""
if self.left is None: # leaf node
left_text = self.text[:pos]
right_text = self.text[pos:]
return RopeNode(left_text), RopeNode(right_text)
if pos <= self.left.size:
left, right = self.left.split(pos)
# Combine right part with self.right
new_right = RopeNode()
new_right.left = right
new_right.right = self.right
new_right.size = right.size + (self.right.size if self.right else 0)
return left, new_right
else:
left, right = self.right.split(pos - self.left.size)
new_left = RopeNode()
new_left.left = self.left
new_left.right = left
new_left.size = self.left.size + left.size
return new_left, right
class Rope:
"""A rope data structure for efficient string manipulation."""
def __init__(self, text: str | RopeNode = "") -> None:
self.root = text if isinstance(text, RopeNode) else RopeNode(text)
@property
def size(self) -> int:
return self.root.size
def concat(self, other: "Rope") -> "Rope":
"""Join two ropes into a new rope."""
if self.size == 0:
return other
if other.size == 0:
return self
new_root = RopeNode()
new_root.left = self.root
new_root.right = other.root
new_root.size = self.root.size + other.root.size
return Rope(new_root)
def split(self, pos: int) -> tuple["Rope", "Rope"]:
"""Split the rope at index `pos` into two ropes."""
if pos <= 0:
return Rope(""), self
if pos >= self.size:
return self, Rope("")
left_node, right_node = self.root.split(pos)
return Rope(left_node), Rope(right_node)
def insert(self, pos: int, text: str) -> "Rope":
"""Insert `text` at index `pos`."""
left, right = self.split(pos)
return left.concat(Rope(text)).concat(right)
def to_string(self) -> str:
"""Reconstruct the full string from the rope."""
def traverse(node: RopeNode) -> str:
if node.left is None:
return node.text
return traverse(node.left) + traverse(node.right)
return traverse(self.root)
# Example: insert "xy" into "hello" at index 2
rope = Rope("hello").insert(2, "xy")
print(rope.to_string()) # Output: hexyllo
Walkthrough
Rope("hello") creates a leaf node with text="hello".
insert(2, "xy") calls split(2), which splits the leaf into "he" and "llo".
left.concat(Rope("xy")) creates an internal node with children "he" and "xy".
The result is concatenated with "llo", forming a tree with root size 7.
to_string() traverses the tree, returning "hexyllo".
Common Pitfalls
Unbalanced trees: without balancing, operations degrade to O(n); use a self-balancing variant (e.g., weight-balanced rope) in production.
Memory overhead: each node adds pointer storage, so ropes are best for large strings where frequent edits outweigh memory cost.
Forgetting size: internal nodes must maintain accurate size values; otherwise, splits will misplace characters.
Follow-up: interviewers often ask how to make the rope thread-safe or how to handle strings that don’t fit in memory.
81
Which data structure supports frequent updates and fast retrieval by key?
KnownRevise
Use a hash table, exposed as a map or dictionary, when data must be updated and retrieved frequently by a key. It provides average O(1) lookup, insertion, and deletion, making it substantially faster than scanning an array for every request.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
Structure Choice and Classification
Abstract data type: Map or dictionary, supporting operations such as get, set, remove, and contains.
Implementation: Hash table containing a bucket array and key-value entries.
Classification: Dynamic, non-linear, and non-primitive.
Python implementation: dict uses a hash table and preserves insertion order, although it does not keep keys sorted.
Memory layout: The bucket array is contiguous, but individual entries may be stored separately, unlike the contiguous elements of an array.
For a single-process application, use a dict. For a distributed system, use a sharded key-value service or distributed hash table, adding replication, consistency, persistence, and failure-handling mechanisms.
Operation Costs
Structure
Access
Search
Insert
Delete
Extra Memory per Element
Order Kept
Hash table
Best O(1), average O(1), worst O(n)
Best O(1), average O(1), worst O(n)
Average O(1) amortized, worst O(n)
Average O(1), worst O(n)
O(1) bucket and entry overhead
Python dict preserves insertion order; general hash tables need not
Balanced BST
O(log n)
O(log n)
O(log n)
O(log n)
O(1) pointers and metadata
Keys remain sorted
Dynamic array
Access O(1); search O(n)
O(n)
Middle O(n); append O(1) amortized
Middle O(n); end deletion O(1) amortized
Low O(1) storage overhead
Positional order
The hash table’s worst case occurs when many keys collide and land in the same bucket. Resizing can also temporarily make one insertion O(n), but repeated insertions remain O(1) amortized.
System Design Considerations
Choose a stable key: A unique identifier such as a user ID produces predictable hashing and retrieval behavior.
Define update semantics: Decide whether repeated updates overwrite values, increment counters, or create version history.
Handle concurrency: Protect shared mutable state with locks or use a thread-safe concurrent map where appropriate.
Partition large datasets: Shard keys across multiple nodes, while using replication or consensus to tolerate failures.
Plan for persistence: An in-memory hash table loses data on restart unless writes are also recorded in durable storage.
Handle hot keys: A frequently updated key can overload one shard; spreading or splitting its load may be necessary.
Choose an alternative when needed: Use a balanced BST when ordered traversal or range queries are primary requirements.
Complexity and Edge Cases
For the example below, let n be the initial record count, m the update count, and k the number of requested keys.
Best time: O(n + m + k) because every input item must be processed.
Average time: O(n + m + k) because dictionary operations take average O(1).
Worst time: O((n + m + k)n) if hash collisions consistently degrade each operation to O(n).
Space: O(n + k) excluding the supplied input lists; O(n + m + k) including them. The recursion stack uses O(1) space.
Empty inputs produce an empty result, one-element inputs work normally, and negative values are preserved. Duplicate initial records or updates use the last value encountered. Requesting a missing key raises KeyError; use dict.get() when a default is appropriate.
Code Example
def apply_updates(
initial_records: list[tuple[str, int]],
updates: list[tuple[str, int]],
retrieval_keys: list[str],
) -> dict[str, int]:
"""Apply updates by key and return the requested records."""
records = dict(initial_records)
for key, value in updates:
records[key] = value
return {key: records[key] for key in retrieval_keys}
result = apply_updates(
initial_records=[("user1", 10), ("user3", 3)],
updates=[("user1", 11), ("user3", 13)],
retrieval_keys=["user1", "user3"],
)
print(result)
# {'user1': 11, 'user3': 13}
Common Pitfalls
Using a list as a queue or repeatedly calling list.pop(0), which costs O(n) per removal.
Ignoring hash collisions, load factors, and the difference between average and worst-case performance.
Forgetting that hash tables have more per-element overhead than contiguous arrays.
Assuming an in-memory dictionary provides durability or multi-process consistency.
Failing to define behavior for missing keys, duplicate updates, concurrent writes, and deleted records.
A common follow-up is: “How would the design change if the data must survive restarts or exceed memory?”
82
How would you implement autocomplete with a trie?
KnownRevise
Autocomplete with a trie stores words along shared prefixes, then traverses only the subtree matching the user’s prefix. A production implementation adds frequency, popularity, or recency ranking and returns the highest-scoring k suggestions.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
How It Works
A trie is a non-linear, dynamic, non-primitive structure whose nodes are linked through references rather than stored contiguously. Each node contains:
A dictionary mapping existing next characters to child nodes.
A flag indicating whether the prefix is a complete word.
Optional metadata such as frequency or popularity.
The workflow is:
Normalize indexed words and query prefixes consistently.
Insert each word, recording its frequency at the terminal node.
Follow the query prefix from the root.
Return no suggestions if that path does not exist.
Traverse the matching subtree with an explicit stack.
Rank candidates and return the top k.
Python has no built-in trie, so a node-based implementation is normally required.
Complexity
Let T be the total number of inserted characters, P the prefix length, M the matching subtree’s node count, R its terminal-word count, C the sum of those words’ lengths, d the maximum children per node, and H = min(k, R).
Build: best and average O(T) time because each character is processed once; theoretical worst case O(Td) under pathological dictionary collisions.
Prefix lookup: best and average O(P) time; theoretical worst case O(Pd).
Suggestions: best and average O(P + M + C + R log(H + 1)) time; theoretical worst case O(Pd + Md + C + R log(H + 1)). The mutable path avoids repeatedly copying prefix strings, while ranking compares integer frequencies.
Persistent space:O(T) for trie nodes and child dictionaries.
Auxiliary space:O(M + C + R) for the explicit traversal stack, current path, and candidates. The input corpus is not counted, and no recursion stack is used.
Structure Comparison
Python’s list is a contiguous dynamic array, dict and set implement hash tables, and a trie is normally implemented with custom linked nodes.
Structure
Access
Search
Insert
Delete
Extra Memory and Order
Typical Use
Sorted list with bisect
O(1) by index
O(log n) average/worst with binary search
O(n) average/worst
O(n) average/worst
O(1) reference per entry; keeps sorted order
Lexicographic catalogs and prefix ranges
Hash table (dict/set)
O(1) average, O(n) worst by key
O(1) average, O(n) worst
O(1) average amortized, O(n) worst
O(1) average, O(n) worst
O(1) table entry/reference per element; dict keeps insertion order, set does not
Caches, indexes, and frequency maps
Trie
O(L) average, O(Ld) worst for exact access
O(L) average, O(Ld) worst for exact search
O(L) average, O(Ld) worst
O(L) average, O(Ld) worst
Up to O(L) new nodes per word plus overhead; no inherent order
Autocomplete and prefix search
Code Example
from __future__ import annotations
from collections.abc import Iterator
from dataclasses import dataclass, field
import heapq
import unicodedata
@dataclass(slots=True)
class TrieNode:
children: dict[str, TrieNode] = field(default_factory=dict)
is_word: bool = False
frequency: int = 0
class Trie:
"""Provide frequency-ranked autocomplete using a prefix trie."""
def __init__(self) -> None:
self.root = TrieNode()
@staticmethod
def _normalize(text: str) -> str:
return unicodedata.normalize("NFKC", text).casefold()
def insert(self, word: str, frequency: int = 1) -> None:
normalized_word = self._normalize(word)
if not normalized_word:
raise ValueError("word must not be empty")
if frequency < 0:
raise ValueError("frequency must be non-negative")
node = self.root
for character in normalized_word:
if character not in node.children:
node.children[character] = TrieNode()
node = node.children[character]
node.is_word = True
node.frequency += frequency
def autocomplete(self, prefix: str, limit: int = 5) -> list[str]:
"""Return up to limit matching words, ordered by descending frequency."""
if limit <= 0:
return []
normalized_prefix = self._normalize(prefix)
node = self.root
for character in normalized_prefix:
child = node.children.get(character)
if child is None:
return []
node = child
candidates: list[tuple[int, str]] = []
path = list(normalized_prefix)
stack: list[
tuple[TrieNode, Iterator[tuple[str, TrieNode]]]
] = [(node, iter(node.children.items()))]
if node.is_word:
candidates.append((node.frequency, normalized_prefix))
while stack:
current, children_iterator = stack[-1]
item = next(children_iterator, None)
if item is None:
stack.pop()
if stack:
path.pop()
continue
character, child = item
path.append(character)
if child.is_word:
candidates.append((child.frequency, "".join(path)))
stack.append((child, iter(child.children.items())))
ranked = heapq.nsmallest(
limit,
candidates,
key=lambda candidate: -candidate[0],
)
return [word for _, word in ranked]
trie = Trie()
for word, frequency in [("cat", 10), ("car", 7), ("cart", 5), ("dog", 9)]:
trie.insert(word, frequency)
print(trie.autocomplete("ca")) # ['cat', 'car', 'cart']
Common Pitfalls
Empty input: An empty trie returns no suggestions; an empty prefix matches every indexed word. Empty words are rejected.
One element: A single indexed word appears only when the prefix matches it.
Duplicates: Repeated insertions accumulate frequency rather than duplicating results.
Negative values: Negative frequencies are rejected; strings containing negative numbers are ordinary text.
Sorted input: The corpus does not need to be sorted.
Graph-only cases: Disconnected graphs and cycles do not apply because a trie is a tree.
Worst cases and memory: Hash collisions can worsen dictionary operations, while per-node Python objects consume substantial memory.
Normalization: Apply the same Unicode normalization and case-folding policy when indexing and querying.
Interview follow-up: Discuss caching frequent prefixes, maintaining subtree-level ranking metadata, and replacing object-heavy nodes with compact arrays for large corpora.
83
How can you design a data structure with constant-time insert, delete, and random access?
KnownRevise
An array-backed indexed set supports insert, delete, and random element retrieval in expected constant time. It combines a dynamic array with a hash map that tracks each value’s current index.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
How It Works
The indexed set is the abstract data type—the operations it supports—while the dynamic array and hash map are its concrete implementation:
Dynamic array: stores values contiguously and provides O(1) indexed access.
Hash map: maps each value to its array position, enabling expected O(1) lookup and deletion.
Insertion appends the value and records its index. Deletion finds the value through the map, replaces it with the final array element, and updates the moved element’s index.
This pattern is useful for maintaining a pool of unique participants for random draws. More generally, dynamic arrays support compact lookup tables and image buffers, while hash maps support caches and database indexes.
Operation Costs
Approach
Access / Random Selection
Search
Insert
Delete
Extra Memory per Element
Order Kept
Array + hash map
O(1) best/average/worst
O(1) average; O(n) worst
O(1) best and average/amortized; O(n) worst for one operation
O(1) best and average/amortized; O(n) worst for one operation
O(1)
No insertion-order guarantee
Array only
O(1) best/average/worst
O(n) best/average/worst
Append: O(1) best and amortized average, O(n) worst for a resize; middle insertion: O(n)
Known end: O(1) amortized; by value: O(n) best/average/worst
O(1)
Insertion order
Balanced BST
O(n) by index
O(log n) best/average/worst
O(log n) best/average/worst
O(log n) best/average/worst
O(1)
Sorted order
Insert: expected amortized O(1); an array resize or pathological hash collisions can make one operation O(n).
Delete: expected amortized O(1); hash collisions or array contraction can make one operation O(n).
Random selection: O(1) by choosing a valid array index uniformly.
Space: O(n) including stored values; treating the values themselves as existing input, the list and map require O(n) auxiliary space. The recursion stack is O(1) because the implementation is iterative.
Strict worst-case constant time is not guaranteed by ordinary dynamic arrays and hash tables.
Walkthrough
Insert 10: the array becomes [10], and the map records 10 → 0.
Insert 20: the array becomes [10, 20], and the map records 20 → 1.
Insert 30: the array becomes [10, 20, 30], and the map records 30 → 2.
Delete 20: replace index 1 with 30, update the map to 30 → 1, and pop the final element.
The array is now [10, 30], so random selection returns either value with equal probability.
Code Example
from collections.abc import Hashable
from random import Random
from typing import Generic, TypeVar
T = TypeVar("T", bound=Hashable)
class RandomSet(Generic[T]):
"""Store unique hashable values with expected constant-time operations."""
def __init__(self, rng: Random | None = None) -> None:
self._values: list[T] = []
self._positions: dict[T, int] = {}
self._rng = rng if rng is not None else Random()
def insert(self, value: T) -> bool:
"""Insert a unique value, returning False if it already exists."""
if value in self._positions:
return False
self._positions[value] = len(self._values)
self._values.append(value)
return True
def delete(self, value: T) -> bool:
"""Delete a value in expected O(1) time using swap-and-pop."""
if value not in self._positions:
return False
index = self._positions.pop(value)
last_value = self._values.pop()
# Move the final element into the deleted element's position.
if index < len(self._values):
self._values[index] = last_value
self._positions[last_value] = index
return True
def get_random(self) -> T:
"""Return a uniformly random stored value."""
if not self._values:
raise IndexError("cannot select from an empty RandomSet")
index = self._rng.randrange(len(self._values))
return self._values[index]
def __len__(self) -> int:
return len(self._values)
values = RandomSet[int]()
print(values.insert(10))
print(values.insert(20))
print(values.insert(30))
print(values.delete(20))
print(values.delete(10))
print(values.get_random())
# Expected output:
# True
# True
# True
# True
# True
# 30
Common Pitfalls
Using list.remove(value) performs an O(n) search and shift; use the map and swap-and-pop instead.
Forgetting to update the moved element’s index leaves the map inconsistent.
An empty set must raise an explicit error from get_random().
A one-element set returns its only value; deleting it leaves the set empty.
Duplicate values are rejected because the map stores one index per value.
Negative and other hashable values work without special handling.
Values must be hashable and support the equality semantics used by dict; ordering comparisons are not required.
The hash map increases memory usage but makes value-based deletion fast.
Interviewers commonly ask how duplicates could be supported or how insertion order could be preserved.
84
How do you design a parking lot with object-oriented principles?
KnownRevise
A well-designed parking lot uses object-oriented design to separate responsibilities among the lot, individual spaces, vehicles, and parking tickets. The system should expose a small public API while keeping each object responsible for maintaining its own invariants.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
Core Object Model
Object
Responsibility
ParkingLot
Coordinates parking and unparking operations and owns the available spaces and active tickets
ParkingSpot
Encapsulates whether it is occupied and prevents invalid occupancy transitions
Vehicle
Represents the vehicle being parked, including its identity and size
Ticket
Provides proof of an active parking session
FeePolicy
Optional extension that calculates charges through hourly, flat-rate, or dynamic-pricing strategies
This design follows several principles:
Encapsulation: ParkingSpot.occupy() and ParkingSpot.release() protect the rule that a space has at most one vehicle.
Single responsibility: Each class manages one concept instead of placing all behavior in ParkingLot.
Composition: A parking lot contains spots, vehicles, and tickets rather than depending on a deep inheritance hierarchy.
Open/closed principle: Future pricing or allocation rules can be isolated behind strategy interfaces, keeping the core parking lifecycle stable.
Abstraction: Callers interact with park() and unpark() rather than manipulating spots directly.
How the Parking Lifecycle Works
The lot initializes one parking spot for each configured space.
Available spaces are grouped by vehicle size in collections.deque queues.
park() rejects a vehicle that is already parked, removes a suitable free space from its queue, occupies it, and issues a ticket.
unpark() validates the ticket, releases the space, and returns the space to the appropriate queue.
A more flexible implementation could introduce an AllocationPolicy that lets compact vehicles occupy larger spaces or reserves spaces for electric vehicles.
Complexity
Let S be the number of spaces and V the number of currently parked vehicles.
Operation
Best time
Average time
Worst time
Additional space
Reason
Construct lot
O(S)
O(S)
O(S²)
O(S)
Creates every space and queue entry; dictionary insertion is O(S) average and O(S²) under pathological hash collisions
park()
O(1)
O(1)
O(S)
O(1)
Queue operations are O(1); dictionary lookups are O(1) average but O(S) under pathological hash collisions
unpark()
O(1)
O(1)
O(S)
O(1)
Ticket and vehicle lookups use hash tables
Check availability
O(1)
O(1)
O(1)
O(1)
There are only two fixed VehicleSize keys, and reading a deque’s length is O(1)
The retained model uses O(S + V) space, counting stored spots, queue entries, active ticket records, and references to active vehicles as O(1) records each. The caller’s original spot_sizes mapping and other unretained input are not counted; retaining that mapping as well still gives O(S) space. No recursion stack is used, and each transaction adds O(1) auxiliary space.
Code Example
from collections import deque
from dataclasses import dataclass, field
from enum import Enum
from itertools import count
class VehicleSize(Enum):
COMPACT = "compact"
FULL = "full"
class NoAvailableSpotError(Exception):
pass
@dataclass(frozen=True, slots=True)
class Vehicle:
plate: str
size: VehicleSize
@dataclass(slots=True)
class ParkingSpot:
spot_id: str
size: VehicleSize
_vehicle: Vehicle | None = field(default=None, init=False, repr=False)
@property
def is_occupied(self) -> bool:
return self._vehicle is not None
def occupy(self, vehicle: Vehicle) -> None:
if self.is_occupied:
raise RuntimeError(f"Spot {self.spot_id} is already occupied")
self._vehicle = vehicle
def release(self) -> Vehicle:
if self._vehicle is None:
raise RuntimeError(f"Spot {self.spot_id} is not occupied")
vehicle = self._vehicle
self._vehicle = None
return vehicle
@dataclass(frozen=True, slots=True)
class Ticket:
ticket_id: str
vehicle: Vehicle
spot_id: str
class ParkingLot:
def __init__(self, spot_sizes: dict[str, VehicleSize]) -> None:
self._spots = {
spot_id: ParkingSpot(spot_id, size)
for spot_id, size in spot_sizes.items()
}
self._free_spots: dict[VehicleSize, deque[str]] = {}
for spot_id, size in spot_sizes.items():
self._free_spots.setdefault(size, deque()).append(spot_id)
self._active_tickets: dict[str, Ticket] = {}
self._vehicle_spots: dict[str, str] = {}
self._ticket_numbers = count(1)
def park(self, vehicle: Vehicle) -> Ticket:
if vehicle.plate in self._vehicle_spots:
raise ValueError(f"Vehicle {vehicle.plate} is already parked")
free_spots = self._free_spots.get(vehicle.size)
if not free_spots:
raise NoAvailableSpotError(
f"No {vehicle.size.value} space is available"
)
spot_id = free_spots.popleft()
self._spots[spot_id].occupy(vehicle)
ticket = Ticket(
ticket_id=f"T-{next(self._ticket_numbers)}",
vehicle=vehicle,
spot_id=spot_id,
)
self._active_tickets[ticket.ticket_id] = ticket
self._vehicle_spots[vehicle.plate] = spot_id
return ticket
def unpark(self, ticket: Ticket) -> None:
active_ticket = self._active_tickets.get(ticket.ticket_id)
if active_ticket is None or active_ticket != ticket:
raise KeyError("Invalid or inactive parking ticket")
released_vehicle = self._spots[active_ticket.spot_id].release()
self._vehicle_spots.pop(released_vehicle.plate)
del self._active_tickets[ticket.ticket_id]
self._free_spots[released_vehicle.size].append(active_ticket.spot_id)
def available_spots(self, size: VehicleSize) -> int:
return len(self._free_spots.get(size, deque()))
def demo() -> None:
"""Demonstrate parking and unparking a vehicle."""
lot = ParkingLot({
"A1": VehicleSize.COMPACT,
"A2": VehicleSize.FULL,
})
ticket = lot.park(Vehicle("ABC123", VehicleSize.COMPACT))
print(ticket.spot_id) # A1
lot.unpark(ticket)
print(lot.available_spots(VehicleSize.COMPACT)) # 1
if __name__ == "__main__":
demo()
Common Pitfalls
Empty or single-space lots: The implementation handles both, but an empty lot raises NoAvailableSpotError when parking is attempted.
Duplicate vehicles: A plate number may have only one active parking session at a time.
Invalid tickets: Reusing or altering a ticket raises KeyError rather than releasing an unrelated space.
Wrong queue structure: Use deque.popleft() and deque.append(); list.pop(0) is O(n).
Mutable public state: Exposing internal dictionaries allows callers to violate occupancy and ticket invariants.
Concurrency: Multiple entrances can race during park(); a lock or database transaction is required in a concurrent system.
Irrelevant edge cases: Negative values, sorted input, disconnected graphs, and cycles do not apply to this parking-lot model.
Common follow-up questions ask about concurrency, persistence across restarts, multiple levels or entrances, dynamic pricing, and how to prevent duplicate vehicles from entering through different gates.
85
How can a sparse matrix be stored efficiently?
KnownRevise
A dictionary-based sparse matrix stores only nonzero values, using (row, column) tuples as keys and the matrix values as entries. This avoids allocating space for the matrix’s mostly empty cells and supports fast updates to individual elements.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
How It Works
A sparse matrix has dimensions (m times n), but contains relatively few nonzero elements. Let (nnz) be the number of nonzero values.
A dictionary representation maintains:
(row, column) -> nonzero value
For example, storing the values 7 at (0, 1) and 4 at (2, 0) requires only two entries rather than nine cells for a 3 × 3 matrix.
Create an empty dictionary for the matrix.
Insert (row, column) with its value when the value is nonzero.
Remove the entry when the value becomes zero.
Return zero when a missing coordinate is accessed.
Python’s dict is implemented as a hash table, so coordinate lookup is expected to be constant time. This representation is particularly suitable when the matrix is mutable or its dimensions are not known in advance.
Complexity
The total storage is O(nnz). This counts the sparse representation itself; no separate copy of the input is needed, and there is no recursion stack.
Get or update a known coordinate: O(1) best and average case, O(nnz) worst case because of hash collisions.
Delete a known coordinate: O(1) best and average case, O(nnz) worst case.
Search for a value without knowing its coordinate: O(nnz), since all entries may need inspection.
Extra space per operation: O(1), excluding the matrix’s O(nnz) storage.
Structure Comparison
“Search” below means finding a value when its coordinate is unknown. Costs assume a mutable matrix of fixed dimensions.
Representation
Access
Search
Insert or Update
Delete
Memory and Order
Dictionary of nonzero entries
O(1) average; O(nnz) worst
O(nnz)
O(1) average; O(nnz) worst
O(1) average; O(nnz) worst
O(nnz) total; no mathematical order
CSR matrix
O(log r) with binary search over the r nonzeros in that row
O(nnz)
O(nnz) to rebuild
O(nnz) to rebuild
O(m + nnz); row-major order
Dense two-dimensional list
O(1)
O(mn)
O(1) for an existing cell
O(1) for an existing cell
O(mn); row-major order
A Compressed Sparse Row (CSR) representation is often better for repeated numerical computations because it stores contiguous values and row offsets. A dictionary is usually more convenient when individual coordinates are frequently inserted, removed, or updated.
Storing explicit zeros: This increases nnz and defeats the purpose of sparse storage.
Ignoring worst-case behavior: Hash-table operations are O(1) on average but can degrade to O(nnz).
Confusing access with search: Looking up a known coordinate differs from scanning for an unknown value.
Using the wrong representation: CSR or the coordinate (COO) format is usually preferable for static numerical workloads and compatibility with numerical libraries.
Coordinate validation: Negative coordinates and coordinates beyond the declared dimensions must be rejected.
Language-specific overflow: In fixed-width integer languages, ensure values and index calculations cannot overflow.
86
How do you implement a file system directory tree?
KnownRevise
A logical directory hierarchy without links is best modeled as a rooted tree, with each directory storing its immediate children in a hash table such as dict. This provides an average O(1) lookup for a child by name while preserving the hierarchy.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
Data Model
Tree classification: A directory hierarchy is a non-linear, dynamic, non-primitive structure. It is not limited to two children like a binary tree.
Node types: Each node represents either a directory or a file.
Child index: Each directory owns a mapping from names to child nodes. Duplicate names within one directory are therefore disallowed.
Interface versus implementation: The file-system API is an abstract data type supporting operations such as mkdir, add_file, remove, and resolve; the rooted tree plus per-directory dict is one implementation.
Python has no built-in general-purpose tree or linked list. The tree here uses classes, while dict provides the child index.
Complexity and Storage
Let k be the number of children in a directory, d the number of path components, and kᵢ the number of children in the ith visited directory. The table assumes bounded-length names.
Operation
dict child index
list child index
Access an existing child by name
Best O(1); average O(1); worst O(k)
Best O(1) if first; average O(k); worst O(k)
Search by resolving a path
Best O(d); average O(d); worst O(∑kᵢ)
Best O(d) if every target is first; average O(∑kᵢ); worst O(∑kᵢ)
Insert a child
Best O(1); average O(1) amortized; worst O(k)
Best O(1) for append; average O(1) amortized; worst O(k) during reallocation
Delete a child by name
Best O(1); average O(1); worst O(k)
Best O(1) for the last child; average O(k); worst O(k) for lookup or shifting
Extra storage per child
O(1) table slot and reference, plus the node and name; insertion order preserved
O(1) pointer, plus the node and name; explicit order preserved
For the selected dict implementation:
Adding a child: Average O(1) amortized; worst O(k) because of hash collisions or resizing.
Resolving a path: Average O(d) and worst O(∑kᵢ) for bounded-length names. For an arbitrary-length path of L characters, expected time is O(L) because the names must also be hashed and copied.
Removing a child by name: Average O(1) and worst O(k).
Stored space: O(N + S), where N is the number of nodes and S is the total size of stored names and file contents.
Auxiliary space: Path resolution uses O(L) space for split, or O(d) with bounded-length components. The input path is not counted, and there is no recursion stack.
Example Walkthrough
root.mkdir("docs") adds a directory under "docs".
docs.add_file("readme.txt", "Hello") adds a file under "readme.txt".
resolve(root, "docs/readme.txt") looks up "docs" in the root and then "readme.txt" in that directory.
The resolved node is a FileNode whose content is "Hello".
Code Example
from __future__ import annotations
from dataclasses import dataclass, field
@dataclass(slots=True)
class FileNode:
content: str = ""
@dataclass(slots=True)
class DirectoryNode:
children: dict[str, FileNode | DirectoryNode] = field(default_factory=dict)
@staticmethod
def _validate_name(name: str) -> None:
"""Reject empty, relative, and path-like child names."""
if not name or name in {".", ".."} or "/" in name or "\\" in name:
raise ValueError(f"Invalid directory entry name: {name!r}")
def mkdir(self, name: str) -> DirectoryNode:
"""Create and return an empty child directory."""
self._validate_name(name)
if name in self.children:
raise FileExistsError(name)
directory = DirectoryNode()
self.children[name] = directory
return directory
def add_file(self, name: str, content: str = "") -> FileNode:
"""Create and return a child file."""
self._validate_name(name)
if name in self.children:
raise FileExistsError(name)
file = FileNode(content)
self.children[name] = file
return file
def remove(self, name: str) -> FileNode | DirectoryNode:
"""Remove a file or an empty child directory."""
self._validate_name(name)
try:
node = self.children[name]
except KeyError:
raise FileNotFoundError(name) from None
if isinstance(node, DirectoryNode) and node.children:
raise OSError("Cannot remove a non-empty directory")
del self.children[name]
return node
def resolve(
root: DirectoryNode,
path: str,
) -> FileNode | DirectoryNode:
"""Resolve a slash-separated path relative to the root."""
current: FileNode | DirectoryNode = root
for component in path.split("/"):
if component in {"", "."}:
continue
if component == "..":
raise ValueError("Parent traversal is not supported")
if not isinstance(current, DirectoryNode):
raise NotADirectoryError(path)
child = current.children.get(component)
if child is None:
raise FileNotFoundError(path)
current = child
return current
root = DirectoryNode()
docs = root.mkdir("docs")
docs.add_file("readme.txt", "Hello")
resolved_file = resolve(root, "docs/readme.txt")
assert isinstance(resolved_file, FileNode)
print(resolved_file.content) # Hello
Common Pitfalls
Path edge cases: An empty path or "/" resolves to the root, while a one-component path performs one lookup. Missing entries raise FileNotFoundError, and descending through a file raises NotADirectoryError.
Duplicate names: mkdir and add_file raise FileExistsError when the requested name already exists in that directory.
Traversal and deletion: ".." is rejected, and removing a non-empty directory raises OSError.
Cycles: This implementation cannot create cycles because it has no link operation. Symbolic links would require cycle detection when followed.
Ordering assumptions: Python dictionaries preserve insertion order. Sort entries explicitly only when the API promises an order such as lexical order.
Production requirements: A real operating-system file system also needs persistent storage, permissions, symbolic-link handling, concurrency control, and efficient lookup across large directories.
87
How should you implement an efficient phone book?
KnownRevise
Use a hash table, represented by Python’s dict, as the primary phone-book index. Keying contacts by a unique, immutable ID gives constant-time average lookup, insertion, deletion, and modification; a secondary name index can support lookup by name when duplicate names are allowed.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
Design and Classification
Abstract data type: A map or dictionary supports operations such as get, put, delete, and update.
Implementation: A hash table stores each key in a bucket determined by a hash function. It is dynamic, non-primitive, and nonlinear.
Primary key: Use a stable contact ID rather than a name. Names may be duplicated, misspelled, or changed.
Name lookup: Maintain a secondary dict[str, set[int]] mapping normalized names to contact IDs. This still provides average O(1) lookup and allows multiple contacts with the same name.
Python mapping: dict implements the mapping ADT using a hash-table-based implementation. Unlike a linked list, its entries are bucketed rather than connected through per-entry node references.
Ordering: Python dict preserves insertion order, but it does not maintain alphabetical key order. Use a balanced search tree when sorted traversal and range queries are required.
Complexity and Alternatives
Structure
Access by key
Search
Insert
Delete
Modify
Extra memory per element
Order
Hash table (dict)
O(1) best/average; O(n) worst
O(1) best/average; O(n) worst
O(1) expected and amortized; O(n) worst
O(1) average; O(n) worst
O(1) average; O(n) worst
O(1) average
Insertion order only
Balanced search tree
O(log n) in all cases
O(log n) in all cases
O(log n) in all cases
O(log n) in all cases
O(log n) in all cases
O(1)
Sorted key order
The hash table is the best choice when exact-key operations dominate. A balanced search tree is preferable when alphabetical browsing, predecessor/successor queries, or ordered ranges are common. Python has no built-in balanced search tree; using bisect with a sorted list does not provide O(log n) insertion because shifting elements still costs O(n).
For n stored contacts, total space is O(n) when the stored records and indexes are counted. An individual operation uses O(1) auxiliary space, excluding the stored phone-book input. No recursion stack is involved.
Example Walkthrough
Step
Operation
Result
1
Insert ID 1, name Ada
One contact stored
2
Insert ID 2, name Linus
Two contacts stored
3
Modify ID 1’s phone number
Ada’s record is replaced
4
Look up ID 1
Returns Ada’s updated record
5
Delete ID 2
Linus is removed
Relevant edge cases include:
Empty book: Lookup raises KeyError; modification and deletion report that the contact was absent.
Single contact: All operations behave like the multi-contact case.
Duplicate ID: Rejected to prevent accidental replacement.
Duplicate name: Supported when names are not the primary key; use a secondary index containing a set of IDs.
Negative ID: Valid unless the application explicitly disallows it.
Code Example
from dataclasses import dataclass
@dataclass(frozen=True, slots=True)
class Contact:
contact_id: int
name: str
phone: str
class PhoneBook:
def __init__(self) -> None:
"""Store contacts by unique ID for efficient CRUD operations."""
self._contacts: dict[int, Contact] = {}
def add(self, contact_id: int, name: str, phone: str) -> None:
"""Add a contact while rejecting duplicate IDs."""
if contact_id in self._contacts:
raise ValueError(f"Contact ID {contact_id} already exists")
self._contacts[contact_id] = Contact(contact_id, name, phone)
def lookup(self, contact_id: int) -> Contact:
"""Return a contact by ID or raise KeyError when absent."""
return self._contacts[contact_id]
def modify(
self,
contact_id: int,
*,
name: str | None = None,
phone: str | None = None,
) -> bool:
"""Modify selected fields and return whether the ID existed."""
if contact_id not in self._contacts:
return False
current = self._contacts[contact_id]
self._contacts[contact_id] = Contact(
contact_id=current.contact_id,
name=current.name if name is None else name,
phone=current.phone if phone is None else phone,
)
return True
def delete(self, contact_id: int) -> bool:
"""Delete a contact and return whether it existed."""
return self._contacts.pop(contact_id, None) is not None
if __name__ == "__main__":
phone_book = PhoneBook()
phone_book.add(1, "Ada", "555-0100")
phone_book.add(2, "Linus", "555-0101")
phone_book.modify(1, phone="555-0199")
print(phone_book.lookup(1))
print(phone_book.delete(2))
# Output:
# Contact(contact_id=1, name='Ada', phone='555-0199')
# True
Common Pitfalls
Using names as unique keys: Duplicate names silently overwrite records. Use IDs and a secondary name index.
Ignoring worst-case costs: Hash operations are O(1) on average, but collisions can produce O(n) behavior.
Using a list for frequent deletions: list.pop(0) is O(n) because every remaining element must shift.
Failing to synchronize indexes: When renaming a contact, update both the primary record and any secondary name index.
Assuming sorted order: dict preserves insertion order, not alphabetical order.
Ignoring production requirements: Interviewers may follow up about persistence, concurrent access, duplicate names, encryption, or handling a phone book larger than memory.
88
Which data structures efficiently support insertion, deletion, and character retrieval in a text editor?
KnownRevise
A text editor should represent its document with a piece table, usually indexed by a balanced tree, rather than repeatedly copying one large string. This supports efficient random edits while retaining the original text and inserted text without copying unchanged characters.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
Core Structures
A piece table combines several components:
Original buffer: An immutable copy of the initial document. Characters are never modified in place.
Add buffer: An append-only area containing newly inserted text.
Piece: A small descriptor such as (buffer_id, start, length) that identifies a range of characters in either buffer.
Piece index: A balanced tree, such as a treap or AVL tree, ordered by document position. Each node also stores the total number of characters in its subtree.
Undo stack: Optional, but usually needed for editor commands. It can store inverse operations or text-buffer snapshots.
The piece table is a dynamic, non-primitive, non-linear structure. Its backing buffers are contiguous, while the tree provides logarithmic expected positioning among pieces. Python 3.12 has no built-in piece table or rope implementation.
Operation Costs
Let p be the number of pieces, g the gap size, and k the number of inserted characters.
Structure
Character access
Exact substring search
Insert
Delete
Extra memory per element
Order
Gap buffer
O(1)
O(n) scan
Best O(1), average O(g), worst O(n)
Best O(1), average O(g), worst O(n)
O(1) per character; contiguous
Yes
Piece table with treap
Best O(1), average O(log p), worst O(p)
O(n) scan
Best O(1), average O(log p), worst O(p)
Best O(1), average O(log p), worst O(p)
O(1) per piece plus text storage
Yes
For the piece table, total storage is O(S + A + p), where S is the original text size and A is all text ever appended. The auxiliary index uses O(p) space; the recursive treap stack uses O(log p) space on average and O(p) in the worst case. Original and newly supplied text are counted as stored document data, not auxiliary metadata.
Code Example
This implementation demonstrates a piece table with a treap index:
from __future__ import annotations
from dataclasses import dataclass, field
import random
Piece = tuple[int, int, int] # buffer_id, start, length
@dataclass(slots=True)
class _Node:
piece: Piece
priority: int
left: _Node | None = None
right: _Node | None = None
total: int = field(init=False)
def __post_init__(self) -> None:
self.total = self.piece[2]
def _update(node: _Node) -> None:
node.total = (
node.piece[2]
+ (node.left.total if node.left else 0)
+ (node.right.total if node.right else 0)
)
def _merge(left: _Node | None, right: _Node | None) -> _Node | None:
if left is None:
return right
if right is None:
return left
if left.priority > right.priority:
left.right = _merge(left.right, right)
_update(left)
return left
right.left = _merge(left, right.left)
_update(right)
return right
def _split(
node: _Node | None, offset: int
) -> tuple[_Node | None, _Node | None]:
"""Split a treap at a character offset."""
if node is None:
return None, None
left_size = node.left.total if node.left else 0
piece_end = left_size + node.piece[2]
if offset < left_size:
before, after = _split(node.left, offset)
node.left = after
_update(node)
return before, node
if offset > piece_end:
before, after = _split(node.right, offset - piece_end)
node.right = before
_update(node)
return node, after
if offset == left_size:
before = node.left
node.left = None
_update(node)
return before, node
if offset == piece_end:
after = node.right
node.right = None
_update(node)
return node, after
buffer_id, start, length = node.piece
first_length = offset - left_size
first = _Node((buffer_id, start, first_length), node.priority)
second = _Node(
(buffer_id, start + first_length, length - first_length),
node.priority,
)
return _merge(node.left, first), _merge(second, node.right)
class PieceTable:
"""Store text as pieces backed by immutable original and add buffers."""
def __init__(self, source: str) -> None:
self._source = source
self._add_buffers: list[str] = []
self._random = random.Random(0)
self._root: _Node | None = (
_Node((0, 0, len(source)), self._random.getrandbits(64))
if source
else None
)
def __len__(self) -> int:
return self._root.total if self._root else 0
def _piece_text(self, piece: Piece) -> str:
buffer_id, start, length = piece
buffer = (
self._source
if buffer_id == 0
else self._add_buffers[buffer_id - 1]
)
return buffer[start : start + length]
def insert(self, index: int, text: str) -> None:
if not 0 <= index <= len(self):
raise IndexError("insertion index out of range")
if not text:
return
buffer_id = len(self._add_buffers) + 1
self._add_buffers.append(text)
before, after = _split(self._root, index)
node = _Node((buffer_id, 0, len(text)), self._random.getrandbits(64))
self._root = _merge(_merge(before, node), after)
def delete(self, start: int, end: int) -> None:
if not 0 <= start <= end <= len(self):
raise IndexError("deletion range out of range")
before, remainder = _split(self._root, start)
removed, after = _split(remainder, end - start)
self._root = _merge(before, after)
def char_at(self, index: int) -> str:
if not 0 <= index < len(self):
raise IndexError("character index out of range")
node = self._root
while node is not None:
left_size = node.left.total if node.left else 0
if index < left_size:
node = node.left
elif index < left_size + node.piece[2]:
offset = index - left_size
return self._piece_text(node.piece)[offset]
else:
index -= left_size + node.piece[2]
node = node.right
raise IndexError("character index out of range")
@property
def text(self) -> str:
parts: list[str] = []
stack: list[_Node] = []
node = self._root
while stack or node is not None:
while node is not None:
stack.append(node)
node = node.left
node = stack.pop()
parts.append(self._piece_text(node.piece))
node = node.right
return "".join(parts)
editor = PieceTable("Hello world")
editor.insert(6, "brave ")
assert editor.char_at(6) == "b"
editor.delete(0, 6)
assert editor.text == "brave world"
print(editor.text) # brave world
Practical Design
Piece table: Best general choice for large documents with edits at arbitrary positions.
Gap buffer: Simple and effective when edits occur near the cursor; shifting the gap costs O(g).
Rope: Useful when the document is divided into logical chunks or needs annotations.
Add a separate line index for fast line navigation, since piece tables natively locate characters rather than lines or substrings.
Common Pitfalls
Copying immutable strings: Inserting into one Python str can copy the entire document.
Using a list as a queue-like edit structure: list.insert(0, value) and list.pop(0) are O(n).
Ignoring worst cases: A treap provides expected O(log p) operations but can degrade to O(p).
Recursion depth: The recursive treap code can exceed Python’s roughly 1,000-call limit in pathological cases; use an iterative implementation when that matters.
Unbounded append storage: Deleted text remains in the add buffer until the editor periodically compacts its backing storage.
89
Which data structure supports efficient range queries and updates?
KnownRevise
For an in-memory numeric array, a segment tree is a strong default: it stores aggregates such as sums, minima, or maxima in tree nodes, supporting range queries and updates in O(log n) time. For a large on-disk collection of records, a B+ tree index is usually more practical.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
Choosing the Structure
The required operations define a range-aggregate abstraction, while a segment tree is one implementation of it.
Use a segment tree when:
Data is an indexed sequence such as sensor readings, prices, or scores.
Queries cover contiguous index ranges.
Values must be updated frequently.
An aggregate such as sum, minimum, maximum, or count is required.
A segment tree is a non-linear tree structure with a fixed logical size. Its implementation is commonly stored in contiguous array slots, which is relatively cache-friendly compared with pointer-heavy linked structures.
How It Works
Leaves store the original values.
Internal nodes store an aggregate of their children.
A range query decomposes the requested interval into O(log n) tree segments.
An update changes the relevant nodes and recomputes their ancestors.
For range additions, lazy propagation stores pending changes until descendants are visited.
For example, with [1, 2, 3, 4, 5]:
Querying indices 1..3 returns 2 + 3 + 4 = 9.
Adding 10 to that range changes the array to [1, 12, 13, 14, 5].
Querying 1..3 again returns 39.
Complexity
Build: O(n) because every tree node is initialized once.
Point update: O(log n) because only one root-to-leaf path changes.
Range update: O(log n) with lazy propagation because only boundary paths and covered nodes are processed.
Range query: O(log n) because the interval is represented by O(log n) segments.
Extra space: O(n), excluding the input; O(n) including the input. The recursion stack uses O(log n) space and is already included in the extra-space bound.
Best, average, and worst cases: identical for a deterministic segment tree under the standard RAM model.
Comparing Alternatives
Unless noted otherwise, best, average, and worst-case costs are the same.
Structure
Access
Search or Range Query
Insert
Delete
Update
Extra Memory
Order
Segment tree
O(1) to a stored leaf
O(log n) aggregate query
O(n) rebuild/grow
O(n) rebuild
O(log n)
O(1) amortized slots per element
Positional
Fenwick tree
O(1) if the backing array is retained
O(log n) prefix/range sum
O(n) due to shifting
O(n) due to shifting
O(log n)
O(1) per element
Positional
B+ tree
O(log n) by key
O(log n + k) for k results
O(log n)
O(log n)
O(log n)
O(1) amortized plus node overhead
Key order
A Fenwick tree is more memory-efficient for point updates and prefix sums. A B+ tree is better for large persistent datasets because it minimizes disk-page access and supports ordered range scans.
Common Pitfalls
Define whether ranges are inclusive or exclusive; mixing conventions causes off-by-one errors.
Push lazy tags before descending into children.
Recompute parent aggregates after partial updates.
Do not use list.pop(0) or list.insert(0, value) to simulate a queue; both are O(n).
The implementation rejects empty input, handles one element, and supports negative values and duplicates. Input order is preserved, so already sorted data has no special effect.
Interviewers often follow up with: “What if updates are point updates only?”, “How would this work on disk?”, or “How would you support range minimum instead of range sum?”
90
What data structure supports real-time collaborative document editing?
KnownRevise
For a shared document editor, use a conflict-free replicated data type (CRDT)—typically a sequence CRDT backed by a balanced sequence tree or rope—to store the document and merge concurrent edits. Operational Transformation (OT) is an alternative collaboration model used by centrally ordered editors; it is not itself the document’s local storage structure.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
Recommended Design
Sequence CRDT: Represents the document as an ordered sequence while allowing concurrent insertions, deletions, and updates to merge deterministically on every replica.
Balanced tree or rope: Provides efficient positional access and editing while preserving document order.
Linked list: Can support a simple prototype, but locating an edit position and accessing distant text both require O(n) traversal.
Dynamic array: Is simple and cache-friendly, but inserting or deleting in the middle shifts subsequent elements.
A balanced tree or rope is a non-linear, dynamic, non-primitive, linked-memory structure. A linked list is linear, dynamic, non-primitive, and linked-memory; a dynamic array is linear, dynamic, non-primitive, and contiguous-memory.
How Collaboration Works
Each client maintains a local replica of the shared sequence.
An edit creates an operation containing enough unique metadata to identify its intended location.
The operation is sent to other clients or stored for later synchronization.
Remote operations are applied locally; a CRDT uses commutative and idempotent merge rules so replicas converge even when operations arrive in different orders.
OT-based systems instead transform each new operation against concurrent operations, usually with a server determining their global order.
Both approaches support offline editing, although CRDTs are particularly well suited to peer-to-peer and intermittent-connectivity scenarios.
Complexity and Storage
Local sequence representation
Access
Search
Insert/delete
Extra memory
Order kept
Dynamic array (list)
O(1) average and worst
O(n) average and worst for arbitrary content
O(n) average and worst in the middle because elements shift; append is O(1) amortized
Contiguous O(n) storage with amortized spare capacity
Yes
Linked list
O(n) average and worst
O(n) average and worst
O(1) average and worst at a known node; finding the position is O(n)
O(1) link per node, O(n) total
Yes
Balanced sequence tree or rope
O(log n) average and worst by index when subtree sizes are tracked
O(n) average and worst for arbitrary content unless additionally indexed
O(log n) average and worst at a known position because splits, merges, and rebalancing are logarithmic
O(1) links and metadata per node, O(n) total
Yes
Counting the document itself, each representation uses O(n) total space. Excluding the input, a linked-list node has O(1) overhead, while a balanced tree has O(n) node overhead and O(log n) temporary stack space for recursive implementations; an iterative implementation can use an explicit O(log n) stack. A CRDT may also retain metadata or tombstones for prior operations, adding O(m) space when one metadata record is retained for each of m operations.
Trade-offs and Real-World Uses
Balanced tree or rope: Best general-purpose choice for large editable text because it combines ordered access with logarithmic local edits. Text editors commonly use rope-like structures.
Dynamic array: Simple and cache-friendly, and useful for lookup tables or small block buffers, but expensive for repeated middle edits.
Linked list: Avoids shifting elements and is useful in structures such as an LRU cache, but its pointer overhead and poor random access make it less attractive for large documents.
CRDT: Suitable for collaborative applications, offline-capable editors, and distributed synchronization.
OT: Suitable when a central server can order operations and clients are consistently connected.
Hash table: Often useful as an auxiliary index, such as mapping identifiers to positions, but not as the primary ordered representation of the document.
Edge Cases and Common Pitfalls
Handle an empty document, a one-character document, and duplicate text; CRDT items generally use unique identifiers rather than text values as identities.
Define deterministic behavior for concurrent inserts at the same position, concurrent delete/update operations, and replicas that reconnect after being offline.
Do not treat a known-node linked-list update as O(1) if locating that node requires O(n) traversal.
Account for CRDT metadata and tombstone growth, which can eventually dominate storage.
If edit operations are processed as a FIFO queue, use collections.deque rather than list.pop(0), which is O(n).
Avoid mutating shared input buffers without an explicit copy, and remember that auxiliary hash indexes can degrade from O(1) average to O(n) worst case under collisions.
A common follow-up is how the design handles offline edits, conflict semantics, garbage collection of tombstones, or documents too large to keep in memory.
91
Which data structure implements browser back and forward buttons?
KnownRevise
Browser back and forward navigation is commonly modeled with two stacks: one for backward history and one for forward history, with the current page held separately. A stack is a non-primitive, linear abstract data type; Python’s list is a dynamic-array implementation of it.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
How It Works
Back stack: stores pages behind the current page. Its top is the next page reached by going back.
Forward stack: stores pages ahead of the current page. Its top is the next page reached by going forward.
Current page: stores the page currently being displayed.
Visit a new URL: push the current page onto the back stack, clear the forward stack, and make the new URL current.
Go back: if the back stack is empty, do nothing; otherwise pop its top, push the old current page onto the forward stack, and make the popped page current.
Go forward: perform the symmetric operation.
The following states show the stacks from bottom to top:
Action
Current
Back stack
Forward stack
Visit A
A
[]
[]
Visit B
B
[A]
[]
Go back
A
[]
[B]
Go forward
B
[A]
[]
Complexity
Let H be the number of pages in the maintained history and F the number of pages in the forward stack.
Operation
Best
Average / Amortized
Worst
Reason
Read current page
O(1)
O(1)
O(1)
The current page is stored directly
Go back or forward
O(1)
O(1) amortized
O(H)
Python list operations may occasionally resize the list
Visit a new URL
O(1)
O(1) amortized
O(H)
Clearing costs O(F) per visit, but discarded entries are charged to earlier pushes
Search for a historical page
O(H)
O(H)
O(H)
The history must be scanned
Space
O(H)
O(H)
O(H)
Each maintained page is stored once across the current page and two stacks
The maintained history is included in the space complexity. No recursion stack is required, and URL string lengths are excluded.
Alternatives
Implementation
Access: Average / Worst
Search: Average / Worst
Insert: Average / Worst
Delete: Average / Worst
Extra Memory per Page
Order
Two stacks
Current O(1) / O(1); arbitrary O(H) / O(H)
O(H) / O(H)
Push O(1) amortized / O(H)
Pop O(1) amortized / O(H); clear O(F)
One list reference plus capacity overhead
Preserved
Doubly linked list with current pointer
Current O(1) / O(1); arbitrary O(H) / O(H)
O(H) / O(H)
Near a known node O(1) / O(1)
At a known node O(1) / O(1)
Node storage plus two pointers
Preserved
Two stacks are simpler for strictly sequential back/forward navigation and benefit from Python lists’ contiguous, cache-friendly storage. A doubly linked list is preferable when adjacent entries must be traversed in either direction or when history is represented as a branching structure; it is also commonly used in LRU-cache designs. Unlike lists, linked nodes add pointer overhead and scatter related nodes across memory.
Common Pitfalls
Do not forget to clear the forward stack after visiting a new URL; otherwise, forward navigation could reach an abandoned branch.
Do not use list.pop(0) to remove the back-stack top. It shifts every remaining element and takes O(H); use pop() instead.
Keep the current page separate from both stacks to avoid ambiguous state transitions.
Empty back or forward navigation should be a safe no-op. One-page histories and duplicate URLs should work normally.
A production browser may also need URL metadata, history persistence, direct access to arbitrary entries, eviction policies, and thread synchronization. Interviewers commonly ask how the design would change if users could jump directly to any historical page.
92
How can a dynamic array expand and contract with user activity?
KnownRevise
A dynamic array supports a changing number of elements by increasing its capacity when insertions outgrow the current allocation and decreasing it when removals leave excessive unused space. This is useful for workloads such as active-session buffers, where the number of stored items fluctuates over time.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
Design
A dynamic array is a linear, dynamic, contiguous data structure whose indexed elements are stored in consecutive slots.
The implementation below uses two thresholds:
Expansion: When the array is full, double its logical capacity, with a configurable minimum.
Contraction: When occupancy falls to one quarter or less, halve the capacity, but never below the initial capacity.
Hysteresis: Different expansion and contraction thresholds prevent the array from repeatedly resizing around the same load level.
Python’s list manages resizing automatically, but its capacity and resize thresholds are not part of its public API. This class therefore tracks capacity and applies an explicit contraction policy over a list of slots.
Complexity
Let n be the number of active elements, c the logical capacity, and m the fixed capacity of a conventional array.
Amortized append: Each expansion doubles capacity, so the total copying cost across a sequence of appends is O(n), giving O(1) amortized work per append.
Memory locality: Contiguous slots are generally more cache-friendly than linked nodes, although Python integers referenced by a list are separate objects.
Walkthrough
With an initial capacity of 4:
Action
Size
Capacity
Reason
Create
0
4
Initial allocation
Append 4 values
4
4
Array is full
Append a fifth value
5
8
Capacity doubles
Remove one value
4
8
Occupancy is above 25%
Remove one value
3
8
Occupancy is above 25%
Remove one value
2
8
Occupancy reaches 25%, so capacity becomes 4
Code Example
class DynamicArray:
"""A dynamic array of integers with automatic expansion and contraction."""
def __init__(self, initial_capacity: int = 4) -> None:
if initial_capacity < 1:
raise ValueError("initial_capacity must be at least 1")
self._items: list[int] = [0] * initial_capacity
self._size = 0
self._capacity = initial_capacity
self._initial_capacity = initial_capacity
def __len__(self) -> int:
return self._size
@property
def capacity(self) -> int:
return self._capacity
def __getitem__(self, index: int) -> int:
if index < 0:
index += self._size
if not 0 <= index < self._size:
raise IndexError("array index out of range")
return self._items[index]
def append(self, value: int) -> None:
if self._size == self._capacity:
# Expand before writing beyond the current capacity.
new_capacity = max(
self._initial_capacity,
self._capacity * 2,
)
self._items.extend([0] * (new_capacity - self._capacity))
self._capacity = new_capacity
self._items[self._size] = value
self._size += 1
def remove_at(self, index: int) -> int:
if not 0 <= index < self._size:
raise IndexError("array index out of range")
removed_value = self._items[index]
# Shift active elements left and clear the vacated slot.
for position in range(index, self._size - 1):
self._items[position] = self._items[position + 1]
self._items[self._size - 1] = 0
self._size -= 1
if (
self._capacity > self._initial_capacity
and self._size <= self._capacity // 4
):
# Contract the backing list and its logical capacity together.
new_capacity = max(
self._initial_capacity,
self._capacity // 2,
)
self._items = self._items[:new_capacity]
self._capacity = new_capacity
return removed_value
example = DynamicArray(initial_capacity=4)
for value in [10, 20, 30, 40, 50]:
example.append(value)
print(example.capacity, len(example)) # 8 5
for _ in range(3):
example.remove_at(0)
print(example.capacity, len(example)) # 4 2
Common Pitfalls
Off-by-one errors: Track the number of active elements separately from capacity; unused slots are not valid elements.
Unstable thresholds: Expanding and shrinking at the same threshold can cause frequent resizing.
Memory overhead: Capacity can remain larger than the active size, especially immediately after growth.
Duplicate values: Removal must use an index or identity-based comparison rather than assuming values are unique.
Concurrency: This implementation is not thread-safe; concurrent updates require external synchronization.
Empty and single-element inputs: They are supported, while an initial capacity below 1 is rejected.
Fixed-width integers: Python 3.12 integers have arbitrary precision; ports to fixed-width languages require overflow checks.
Follow-up question: Interviewers may ask how the implementation would change for thread safety, persistence, or fixed-width values.
93
How do data structures affect database indexing?
KnownRevise
Database indexes rely on specific data structures to organize pointers to table rows so that queries can locate data without scanning every row. The structure determines which operations are fast (point lookups, range scans, ordered iteration) and which are slow, as well as the storage overhead and concurrency behavior. Choosing the right index structure is the single biggest factor in query performance for read-heavy and write-heavy workloads alike.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
Core Index Structures
B+ Tree: The default in most relational databases (PostgreSQL, MySQL InnoDB, SQL Server). Internal nodes store only keys for navigation; all row pointers live in the linked leaf level. This makes range scans and ordered iteration efficient while keeping tree height low (typically 3–4 levels for billions of rows).
Hash Index: Uses a hash table mapping key values directly to row pointers. Provides O(1) average point lookups but cannot support range queries or ordering. Used in MySQL MEMORY engine and PostgreSQL hash indexes (rarely chosen by default).
LSM Tree (Log-Structured Merge Tree): Optimized for high write throughput. Writes go to an in-memory sorted component (memtable), then flush to immutable sorted files (SSTables) on disk. Reads may check multiple levels; compaction merges files in the background. Used in RocksDB, Cassandra, DynamoDB.
Inverted Index: Maps terms to the list of documents/rows containing them. Essential for full-text search (PostgreSQL GIN, Elasticsearch, Lucene).
Bitmap Index: Represents each distinct value as a bitset of row IDs. Extremely compact for low-cardinality columns (gender, status flags) and enables fast Boolean combinations (AND/OR). Common in data warehouses (Oracle, ClickHouse).
Operation Cost Comparison
Structure
Point Lookup
Range Scan
Insert
Delete
Space Overhead
Order Preserved
B+ Tree
O(log n)
O(log n + k)
O(log n)
O(log n)
~20–30%
Yes
Hash
O(1) avg
Not supported
O(1) avg
O(1) avg
~30–50%
No
LSM Tree
O(log n)
O(log n + k)
O(1) amortized
O(1) amortized
Low (compressed)
Yes (per level)
Bitmap
O(1) per bit
Fast bitwise
Slow (rebuild)
Slow (rebuild)
Very low
N/A
k = number of matching rows
How Structure Choice Impacts Performance
Read-heavy, mixed queries: B+ tree wins because it handles both point lookups and range predicates (WHERE id > 100 AND id < 200) with the same structure.
Write-heavy, append-only: LSM trees turn random writes into sequential writes, avoiding the B+ tree’s random I/O on leaf pages. Compaction cost is paid asynchronously.
Low-cardinality filters: Bitmap indexes let the optimizer combine multiple conditions with bitwise operations before touching the table.
Full-text search: Inverted indexes with positional information enable phrase and proximity queries that B+ trees cannot express.
Memory and Cache Behavior
B+ trees exhibit good spatial locality when scanning ranges because leaf pages are linked and often laid out sequentially on disk. However, random point lookups traverse multiple levels, causing cache misses at each level.
LSM trees keep the hot memtable in memory (cache-friendly) but reads may hit multiple SSTable levels; bloom filters reduce unnecessary disk reads.
Hash indexes have poor locality for range operations but excellent locality for point lookups if the bucket array fits in memory.
Pointer overhead: B+ tree nodes and LSM SSTable blocks are typically 4–16 KB pages, amortizing pointer costs. In-memory structures (skip lists, ART trees) pay a pointer per entry.
MySQL InnoDB: Clustered B+ tree on primary key; secondary B+ trees store primary key values as row pointers.
MongoDB: B-tree (WiredTiger) for all indexes; supports compound, text (inverted), geospatial (2dsphere), and hashed indexes.
Cassandra/ScyllaDB: LSM-based SSTables with partition keys hashed to nodes; clustering keys sorted within partitions.
ClickHouse: Primary key is a sparse index on sorted data (MergeTree); secondary indexes include bloom filters, min-max, and set indexes.
Common Pitfalls
Assuming all indexes are B-trees: Hash indexes cannot satisfy ORDER BY or range predicates; the optimizer will fall back to a full scan.
Over-indexing write-heavy tables: Each index adds write amplification (B+ tree: ~3–4x; LSM: compaction overhead). Measure insert latency with and without the index.
Ignoring cardinality: Bitmap indexes on high-cardinality columns explode in size; B+ trees on low-cardinality columns may not be selective enough for the optimizer to choose them.
Forgetting covering indexes: A B+ tree that includes all queried columns (INCLUDE in PostgreSQL, covering index in SQL Server) avoids the heap lookup entirely.
Not monitoring index bloat: Deletes in B+ trees leave dead tuples; REINDEX or pg_repack reclaims space. LSM compaction handles this automatically but consumes I/O bandwidth.
Interview follow-up: “How would you implement a secondary index on a distributed database?” — answer involves partitioning strategy (local vs global index) and consistency trade-offs.
94
Which data structures should power a search engine index?
KnownRevise
A search engine index is usually implemented as an inverted index, combining a hash table or trie for term lookup with posting lists that identify matching documents. This hybrid supports fast candidate retrieval while preserving the information needed for relevance ranking.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
Search Engine Index Structure
The inverted index maps each indexed term to a list of documents containing it. For example, an index entry might be:
"python" → [(12, 4), (27, 1), (41, 3)]
Each tuple can contain a document ID, term frequency, positions, or field metadata. A typical query follows these steps:
Tokenize and normalize the query.
Look up each query term in the term dictionary.
Retrieve its posting list.
Intersect or merge posting lists to obtain candidate documents.
Rank candidates using signals such as BM25, TF-IDF, document length, or click data.
The index is an architecture, not one primitive data structure. Its main components are:
Term dictionary: A map ADT, commonly implemented with a hash table, balanced search tree, trie, or finite-state transducer.
Posting lists: Dynamic arrays containing document references and optional term metadata.
Document store: Usually a forward index or separate storage mapping document IDs to content and ranking metadata.
Choosing the Components
Structure
Access
Search
Insert
Delete
Extra memory per element
Order kept
Typical search-engine use
Hash table
O(1) average, O(n) worst
O(1) average, O(n) worst
O(1) average/amortized, O(n) worst
O(1) average, O(n) worst
O(1) slot plus key storage
No semantic order; Python dict preserves insertion order
Exact term lookup and caches
Balanced BST
O(log n)
O(log n)
O(log n)
O(log n)
O(1) node plus pointers
Sorted
Ordered term dictionaries and range queries
Trie
O(L) for a term of length L
O(L)
O(L)
O(L)
O(1) per character/node
Prefix and lexicographic order
Autocomplete and prefix matching
Dynamic array
O(1)
O(n) linear or O(log n) with binary search
O(n) in middle; O(1) amortized at end
O(n) in middle; O(1) amortized at end
O(1) slot per posting
Yes
Posting lists
Here, n is the number of entries or postings, and L is the term length.
A hash table is usually the best default for exact term lookup because it provides expected constant-time access and does not require sorted terms. A trie is useful when prefix matching is a primary requirement, but storing every suffix or position in a trie can consume substantial memory. A balanced BST is appropriate when both exact lookup and ordered iteration are frequently required.
Posting Lists
Posting lists are linear, dynamic, contiguous-memory structures. Keeping document IDs sorted enables binary search and efficient merging of multiple query terms. If the IDs are sorted, retrieving documents containing both "python" and "index" can be done by merging the two lists rather than searching every document.
Common optimizations include:
Variable-length integer encoding: Uses fewer bytes for small document IDs and frequencies.
Skip lists: Store selected document IDs so intersections can skip large ranges.
Position lists: Record token positions for phrase and proximity searches.
Field metadata: Distinguish matches in titles, headings, and body text.
Impact scores: Prioritize rare terms, which usually eliminate more candidates.
Complexity
Let P be the total number of postings, T the number of unique terms, q the number of query terms, and C the number of candidate documents.
Index construction: O(corpus size) expected for tokenization and posting insertion; sorting posting lists can add O(P log P).
Term lookup: O(q) expected using a hash table.
Candidate retrieval: O(sum of selected posting-list lengths) when scanning; skip structures can reduce the effective work.
Index space: O(T + P) plus document metadata. Storing positions increases space toward O(total token occurrences).
These space bounds count the stored index and auxiliary metadata, but not the original corpus. If the corpus is included, add its size to the total. There is no recursion stack.
Trade-offs
Hash table plus posting lists gives the best general-purpose balance of lookup speed, ranking flexibility, and implementation simplicity.
Trie-based indexing improves prefix and autocomplete performance but often has higher memory overhead.
Sorted arrays are compact and cache-friendly, but dynamic insertion can be expensive.
Compressed posting lists reduce memory and improve cache locality, at the cost of more complex decoding and ranking code.
Distributed indexes are necessary when the corpus is too large for one machine; they introduce network latency, shard-selection complexity, and consistency concerns.
Common Pitfalls
Choosing a structure by habit instead of identifying the dominant operation, usually exact term lookup.
Confusing fast term lookup with the separate cost of retrieving and ranking documents.
Assuming hash-table operations are always O(1); collisions can produce O(n) worst-case behavior.
Underestimating memory consumed by posting lists, positions, offsets, and document metadata.
Using unsorted posting lists when efficient intersections or binary search are required.
Ignoring spelling correction, stemming, synonyms, and phrase queries during query processing.
Forgetting that ranking quality usually matters more than shaving a small amount of latency from candidate lookup.
Interviewers commonly follow up with: “How would you handle prefix searches?”, “How would you rank results?”, “How would you compress the index?”, or “What changes when the index no longer fits in memory?”
95
How should high-frequency data be cached for minimal retrieval latency?
KnownRevise
Cache frequently requested data in a fast, bounded in-memory key-value store so requests avoid slower disk or network operations. For one process, use a bounded LRU cache for recency-based eviction; a plain dict gives the lowest direct-lookup overhead but has no automatic capacity limit or eviction policy, so the application must bound it explicitly. Use a shared cache such as Redis across instances when they need common state.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
Recommended Architecture
Use cache-aside loading: Check the cache first; on a miss, read the source database or service, then populate the cache.
Cache only hot data: Promote repeatedly accessed records and bound capacity to prevent memory exhaustion.
Choose or combine policies:
LRU: Evicts the least recently used entry. Suitable when recency predicts future demand.
LFU: Evicts the least frequently used entry. Suitable when a small set remains consistently popular.
TTL: Expires entries after a fixed lifetime. Combine it with LRU or LFU when freshness is also required.
Use local memory for single-process latency and a shared distributed cache when instances need common state.
Protect against cache stampedes: When a popular key expires, coordinate concurrent requests so only one reloads it.
Operation Costs
Let n be the number of cached entries.
Structure
Access
Search
Insert
Delete
Extra memory per entry
Order
Python dict
O(1) best/average; O(n) worst
O(1) best/average; O(n) worst
O(1) best; O(1) amortized average; O(n) worst
O(1) best/average; O(n) worst
O(1) hash-table overhead plus key/value storage
Insertion order
OrderedDict-backed LRU
O(1) best/average; O(n) worst
O(1) best/average; O(n) worst
O(1) best; O(1) amortized average; O(n) worst
O(1) best/average; O(n) worst
O(1) hash-table and linked-list overhead plus key/value storage
Recency order
Hash lookup takes O(1) average time but O(n) in pathological collision cases. Once a key is located, OrderedDict recency updates and oldest-entry eviction take O(1).
Counting bounded-size cached entries, total space is O(n), including stored keys and values. The source dataset is not counted, but copied payloads stored in the cache are. Ordinary lookups and updates use O(1) auxiliary workspace; a hash-table resize can require O(n) transient space. No recursion stack is used.
Example Flow
Operation
Cache state, least recent to most recent
Result
put("a", 1)
a
Stored
put("b", 2)
a, b
Stored
get("a")
a, b
Hit; a becomes most recent
put("c", 3)
a, c
b is evicted
get("b")
a, c
Miss
get("a")
a, c
Hit
Code Example
from collections import OrderedDict
from typing import Generic, Hashable, TypeVar
Key = TypeVar("Key", bound=Hashable)
Value = TypeVar("Value")
class LruCache(Generic[Key, Value]):
"""A bounded, thread-unsafe LRU cache for hashable keys."""
def __init__(self, capacity: int) -> None:
if capacity <= 0:
raise ValueError("capacity must be positive")
self._capacity = capacity
self._items: OrderedDict[Key, Value] = OrderedDict()
def __contains__(self, key: object) -> bool:
return key in self._items
def get(self, key: Key) -> Value | None:
if key not in self._items:
return None
value = self._items[key]
self._items.move_to_end(key)
return value
def put(self, key: Key, value: Value) -> None:
if key in self._items:
self._items[key] = value
self._items.move_to_end(key)
return
if len(self._items) >= self._capacity:
self._items.popitem(last=False)
self._items[key] = value
cache = LruCache[str, int](capacity=2)
cache.put("a", 1)
cache.put("b", 2)
print(cache.get("a")) # 1
cache.put("c", 3) # Evicts "b"
print(cache.get("b")) # None
print(cache.get("a")) # 1
Best Practices
Set a maximum cache size based on measured memory limits.
Add TTL when data becomes stale, and use versioned keys or explicit invalidation for updates.
Pre-warm important keys before or during a rollout.
Monitor hit ratio, miss latency, eviction rate, and memory usage.
Use a single-flight lock or request-coalescing mechanism around expensive reloads.
Treat process-local caches as potentially inconsistent across instances; distributed caches also require an explicit consistency strategy.
Common Pitfalls
An unbounded cache eventually exhausts memory.
Assuming every hash-table operation is always O(1) ignores collision-related worst cases.
Caching mutable objects without invalidation creates stale results.
Expiring a popular key without request coordination can cause a cache stampede.
A local cache does not synchronize state across processes or machines.
get() returning None is ambiguous if None is also cached; use key in cache before calling get() when that distinction matters.
Edge cases: An empty cache is a miss, capacity one remains valid, and updating an existing key refreshes recency without increasing the cache size. Keys must be hashable.
A common interview follow-up is: How would you invalidate cached data across multiple application instances?
96
What data structures implement a social network feed
KnownRevise
A social-network feed is usually implemented with a follow graph, a post store, and either read-time pull merge or write-time push fan-out. Hybrid systems combine both approaches to balance freshness, read latency, and fan-out cost.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
Main Structures
Follow graph: A non-linear graph represented by adjacency lists, such as dict[int, set[int]] in Python 3.12. The forward mapping stores who each user follows; a reverse index stores followers for push fan-out. Hash-set membership, insertion, and deletion are O(1) on average and O(n) in the worst case because of collisions.
Post store: A hash table maps each user ID to an append-only log, commonly dict[int, list[Post]]. Dynamic-array appends are O(1) amortized, but inserting at the front is O(n). Therefore, maintain posts in append order and sort by timestamp when necessary, or use a timestamp-indexed store.
Ranking structure: A binary heap supports O(log n) insertion and removal plus O(1) peek operations. It is useful for ranking candidates or performing a k-way merge of chronologically ordered post logs.
Materialized feed and work queue: A list or collections.deque can hold a precomputed feed, while a deque also models buffered fan-out work. Both allow O(1) operations at their ends. Lists are contiguous and cache-friendly; node-based linked structures use additional pointer storage and scatter objects in memory.
Feed Generation Strategies
Pull model: At read time, retrieve each followee’s sorted post log and merge the logs with a heap. With F sources and K returned posts, heap construction costs O(F), and each result requires O(log(F + 1)) heap work, giving O(F + K log(F + 1)) total time. Additional space is O(F) for the heap and O(K) for the result.
Push model: At write time, append or insert the new post into each of the author’s R followers’ materialized feeds. With append-only O(1) amortized writes, fan-out costs O(R). Maintaining exact timestamp order through ordered-list insertion can instead cost O(RC) for a feed capacity of C.
Hybrid model: Push to ordinary followers, but pull posts from high-fan-out authors at read time. This is the usual approach for real systems because celebrity fan-out is too expensive on the write path.
Persistent pull storage is O(V + E + P), where V is the number of users, E is the number of follow edges, and P is the number of posts. A push system also needs O(U × C) bounded feed slots for U materialized users. These figures count stored data; transient output is stated separately above. No recursion is required, so the recursion-stack space is O(1).
Data-Structure Costs
Structure
Access or peek
Search
Insert
Delete
Extra space per item
Order
Adjacency list with hash sets
O(1) average, O(n) worst
O(1) average, O(n) worst
O(1) average, O(n) worst
O(1) average, O(n) worst
O(1) average per edge
No
Dynamic list
O(1) by index
O(1) best, O(n) average/worst
O(1) amortized at end; O(n) worst for one resize
O(1) at end; O(n) by index
O(1) amortized
Yes
Binary heap
O(1) peek
O(1) best, O(n) average/worst
O(log n)
O(log n)
O(1)
Priority only
Deque
O(1) at ends, O(n) by index
O(n)
O(1) at ends
O(1) at ends
O(1) plus block overhead
FIFO
Hash collisions explain the hash-table worst case; list resizing explains why an individual append can be O(n) even though a sequence of appends is amortized O(1). Python dictionaries preserve insertion order, while sets do not guarantee a useful ordering.
Edge Cases
Empty follow or post collections produce an empty feed.
One source or one post requires no special merge complexity.
Negative timestamps are valid when comparisons use signed numeric values.
Already sorted source logs can be merged directly.
Disconnected graph components are independent, and follow cycles are valid; any traversal should use a visited set.
Common Pitfalls
Do not use list.pop(0) as a queue operation; it is O(n). Use collections.deque or heapq.
Do not push to celebrities without accounting for their fan-out.
Keep forward and reverse follow indexes consistent.
Bound materialized feeds and account for metadata and index overhead, not only post contents.
Choose structures based on required operations and worst cases rather than habit.
Common follow-ups are “How would this support real-time updates?”, “How would you make it thread-safe?”, and “How would you shard it?”
97
How can data structures implement a gaming platform leaderboard?
KnownRevise
A gaming leaderboard typically combines a hash map for fast player-score lookups with an ordered structure for ranking. Use an order-statistics tree for exact ranks or an indexed heap when only the top (K) players are required.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
Recommended Architecture
Player state: A hash table such as Python dict maps player_id to score, version, and metadata. A lookup or update takes (O(1)) average time because of hashing, with (O(n)) worst case after severe collisions; total space is (O(n)).
Ranking key: Order players by (-score, player_id). Negating the score gives descending score order, while player_id provides a deterministic ascending tie-breaker.
Exact ranks: An augmented balanced binary search tree stores subtree sizes. Rank selection and keyed updates take (O(log n)) time because the tree height is (O(log n)); total space is (O(n)).
Top (K) only: An indexed min-heap stores at most (K) players (its root is the weakest of the current top (K), so it is the one evicted), and a map records each player’s heap position. Updating a player takes (O(log K)) time because the heap is reordered after the change; total space is (O(K)). Python has no built-in indexed heap, and heapq does not efficiently update or remove an arbitrary entry.
Regional boards: Merge sorted regional streams with a min-heap. Each extracted candidate costs (O(log R)) for ® regions, with (O®) heap space. Each region’s top (K) is enough for the global top (K), but exact global ranks require additional rank/count metadata or a globally ordered service.
Structure Costs
Caller-owned input is excluded from space figures. No recursion is used in the example; a recursive balanced-tree operation may use an (O(log n)) stack.
Structure
Access
Search
Insert
Delete
Extra Memory per Element
Typical Gaming Use
Hash table
(O(1)) average, (O(n)) worst by key
(O(1)) average, (O(n)) worst
(O(1)) average/best, (O(n)) worst
(O(1)) average/best, (O(n)) worst
(O(1)), (O(n)) total
Player state and score lookup
Sorted list
(O(1)) by position
(O(log n)) average/worst
(O(n)) average/worst, (O(1)) best at end
(O(n)) average/worst, (O(1)) best at end
(O(1)), (O(n)) total
Small in-memory leaderboard
Order-statistics tree
(O(log n)) average/worst by rank
(O(log n)) average/worst
(O(log n)) average/worst
(O(log n)) average/worst
(O(1)) metadata, (O(n)) total
Exact large-player ranking
Binary heap
(O(1)) top only; arbitrary rank unsupported
(O(n)) average/worst by score; (O(1)) average by player with an index map
(O(log n)) average/worst, (O(1)) best
(O(log n)) average/worst when indexed
(O(1)) fields plus optional (O(n)) index map
Live top-(K) display
Example Walkthrough
For ana=120, bo=130, and cy=130:
bo ranks first because bo sorts before cy for equal scores.
cy ranks second.
ana ranks third.
A top-two query returns bo and cy.
Code Example
This implementation combines a hash map with a sorted list. It supports score replacement, top-(K) retrieval, and exact rank lookup under the defined tie-breaking rule.
from bisect import bisect_left, insort
class Leaderboard:
"""Maintain players by descending score, then ascending player ID."""
def __init__(self) -> None:
self._entries: list[tuple[int, str]] = []
self._scores: dict[str, int] = {}
def update(self, player_id: str, score: int) -> None:
if player_id in self._scores:
old_key = (-self._scores[player_id], player_id)
index = bisect_left(self._entries, old_key)
del self._entries[index]
insort(self._entries, (-score, player_id))
self._scores[player_id] = score
def top_k(self, k: int) -> list[tuple[str, int]]:
if k <= 0:
return []
limit = min(k, len(self._entries))
return [
(self._entries[index][1], -self._entries[index][0])
for index in range(limit)
]
def rank(self, player_id: str) -> int | None:
try:
score = self._scores[player_id]
except KeyError:
return None
key = (-score, player_id)
index = bisect_left(self._entries, key)
if index == len(self._entries) or self._entries[index] != key:
return None
return index + 1
board = Leaderboard()
board.update("ana", 120)
board.update("bo", 130)
board.update("cy", 130)
print(board.top_k(2))
# [('bo', 130), ('cy', 130)]
update takes (O(n)) average and worst time because deletion and insertion shift list elements, with (O(1)) best case for an empty board. top_k takes (O(min(k,n))) time for (k>0) and (O(1)) otherwise. rank takes (O(log n)) average time and (O(n)) worst case because of hash-table collisions. Stored space is (O(n)), auxiliary space is (O(1)), and returned output uses (O(min(k,n))) space; the recursion stack is (O(1)).
Production Design
Use a balanced order-statistics tree or database sorted set for exact ranks and large populations.
Use a top-(K) heap for leaderboard pages, matchmaking queues, or top-player rewards.
Version score updates to reject delayed or duplicate events.
Specify whether ranks use a deterministic tie-breaker, competition ranking, or dense ranking.
Edge cases include empty and one-player boards, duplicate scores, negative scores, replacing an existing player, and (k le 0) or (k > n). Already sorted input does not change the stated asymptotic costs.
Interview Tips
Common mistakes include sorting the whole board per request, using list.pop(0) during merges, ignoring hash-table worst cases, and failing to define tie semantics. Interviewers commonly ask: “How would you support millions of players?”, “How do you handle concurrent updates?”, or “How would you recover from a failed regional service?”
98
How do GPS systems use data structures for shortest path?
KnownRevise
GPS systems model roads as a weighted graph and use a priority queue to repeatedly process the most promising route toward the destination.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
How It Works
Map matching associates the GPS coordinates with a nearby road or intersection.
The road network becomes a directed graph: intersections are vertices, and road segments are edges. One-way streets make the graph directed.
Edge weights represent the objective, such as distance, travel time, or a combination of time, tolls, and congestion.
Dijkstra’s algorithm expands the vertex with the smallest known cost. A* improves practical performance by ordering vertices with g(n) + h(n), where g(n) is the cost already incurred and h(n) estimates the remaining cost.
Production GPS engines often accelerate queries with bidirectional search, ALT bounds, contraction hierarchies, cached routes, and time-dependent traffic weights.
An A* heuristic must never overestimate the remaining cost if an optimal route is required. A consistent heuristic also guarantees that a vertex need not be reopened once settled. Straight-line distance is suitable only when the weights represent geographic distance, not arbitrary travel times.
Core Data Structures
Structure
Access or peek
Search
Insert or delete
Extra memory and order
GPS role
Dict-based adjacency list
O(1) average/O(V) worst to find a vertex
O(1) average/O(deg(v)) worst for an edge from a known vertex
O(1) average/O(V) worst
O(1) per vertex and edge; no general order
Stores intersections and connected road segments
heapq min-heap
O(1) peek; O(1) indexed access
O(n)
O(log n) push/pop
O(n); priority order only
Holds candidate vertices to process next
Distance dict
O(1) average/O(V) worst
O(1) average/O(V) worst
O(1) average/O(V) worst
O(1) per entry; insertion order preserved
Stores the best known cost for each vertex
Visited set
O(1) average/O(V) worst
O(1) average/O(V) worst
O(1) average/O(V) worst
O(1) per member; no meaningful order
Prevents repeatedly settling a vertex
Time and Space Complexity
For a simple graph with V vertices and E edges:
Approach
Time
Auxiliary space
Conditions
Dijkstra
O((V + E) log V) worst case
O(V + E)
Non-negative edge weights
A*
O((V + E) log V) worst case
O(V + E)
Consistent heuristic; admissible for optimality
The distance map and visited set use O(V) space. Because heapq does not support decrease-key, stale entries can increase the heap to O(E).
Auxiliary space excludes the O(V + E) input graph and includes an O(1) recursion stack because the implementation is iterative.
Dijkstra has no generally tighter average-case bound; A* can expand far fewer vertices in practice but has no uniformly better worst-case bound under the stated conditions.
Code Example
import heapq
def dijkstra_shortest_path(
graph: dict[str, dict[str, int]],
start: str,
end: str,
) -> int:
"""Return the shortest non-negative distance, or -1 if unreachable."""
if start not in graph or end not in graph:
raise ValueError("Both nodes must exist in the graph.")
for node, neighbors in graph.items():
for neighbor, weight in neighbors.items():
if neighbor not in graph:
raise ValueError(f"Unknown neighbor {neighbor!r} of {node!r}.")
if weight < 0:
raise ValueError("Dijkstra requires non-negative edge weights.")
if start == end:
return 0
distances: dict[str, int] = {start: 0}
visited: set[str] = set()
priority_queue: list[tuple[int, str]] = [(0, start)]
while priority_queue:
current_distance, current_node = heapq.heappop(priority_queue)
if current_node in visited:
continue
visited.add(current_node)
if current_node == end:
return current_distance
for neighbor, weight in graph[current_node].items():
if neighbor in visited:
continue
new_distance = current_distance + weight
if neighbor not in distances or new_distance < distances[neighbor]:
distances[neighbor] = new_distance
heapq.heappush(priority_queue, (new_distance, neighbor))
return -1
graph = {
"A": {"B": 1, "C": 4},
"B": {"C": 2, "D": 5},
"C": {"D": 1},
"D": {},
}
print(dijkstra_shortest_path(graph, "A", "D")) # 4
Common Pitfalls
Edge cases: Missing endpoints raise ValueError; an empty graph has no valid endpoints; start == end returns 0; zero-weight edges and cycles are supported; an unreachable target returns -1; negative weights are rejected.
Duplicates: A dictionary adjacency representation keeps one weight per vertex pair. If multiple parallel edges are possible, retain the minimum relevant weight.
Heuristic correctness: An inadmissible or inconsistent A* heuristic can produce a non-optimal route or repeated expansion.
Negative cycles: Bellman-Ford or Floyd-Warshall can handle some negative edges, but a reachable negative cycle has no finite shortest-path distance.
Mutable data: Do not modify the graph during a search without synchronizing the routing index and cached routes.
Numerical safety: In fixed-width languages, distance additions can overflow; use a sufficiently wide integer or floating-point type.
Follow-up: Interviewers commonly ask how to reconstruct the complete path using predecessor pointers, or how to account for turn restrictions and time-dependent traffic.
99
Which data structures power ride-sharing apps like Uber and Lyft?
KnownRevise
Graphs, hash tables, priority queues, spatial indexes, and ordered indexes are the core data structures in ride-sharing systems. Together, they locate nearby drivers, rank candidates, calculate routes, process ride events, and maintain rapidly changing trip and driver state.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
Selecting the closest or fastest candidate drivers
Queue
Linear, dynamic, linked or chunked storage
collections.deque
Ride requests, dispatch events, notifications, background work
Balanced search tree
Non-linear, dynamic, linked nodes
Balanced BST or database B+ tree
Sorted history, range queries, database indexes
Spatial index
Non-linear, dynamic, tree-based
R-tree, Quadtree, geospatial index
Finding drivers and pickup locations near a coordinate
Dynamic array
Linear, dynamic, contiguous
list, array.array
Candidate lists, route metadata, batched results
In Python, list and array.array are non-primitive containers backed by contiguous storage, while dict and set are hash-table implementations. Python retains dict insertion order, but does not maintain keys in sorted order.
Operation Costs
These bounds assume a table of n elements. “Extra memory” means overhead beyond the stored payload.
Structure
Access
Search
Insert
Delete
Extra memory
Order
Dynamic array
O(1)
O(n)
O(n) in middle; append O(1) amortized
O(n) in middle
O(1) per element
Yes
Queue/deque
O(1) at either end
O(n)
O(1) enqueue
O(1) dequeue
O(1) per element
FIFO
Hash table
O(1) average; O(n) worst
O(1) average; O(n) worst
O(1) average; O(n) worst
O(1) average; O(n) worst
O(1) average per entry
Not sorted
Balanced BST
O(log n)
O(log n)
O(log n)
O(log n)
O(1) pointers per node
Sorted
Binary heap
O(n) to arbitrary element; O(1) peek
O(n)
O(log n)
O(log n) at root
O(1) per element
No
Graph adjacency list
O(1) to a vertex
O(V + E) for connectivity
O(1) amortized per edge
O(degree) for a known edge
O(V + E) total
No inherent order
For hash tables, the best and average costs are O(1), while pathological collisions produce O(n) worst-case operations. Dynamic-array append is O(1) amortized because occasional resizing copies all elements.
End-to-End Workflow
A ride request enters a queue, where deque provides O(1) FIFO processing.
The rider’s location is queried against a spatial index to obtain nearby candidate drivers.
Driver records and availability flags are retrieved from a hash table using O(1) average lookups.
Candidates are ranked in a priority queue, commonly a min-heap ordered by ETA or pickup distance.
The selected route is computed over a road graph; Dijkstra’s algorithm uses non-negative edge weights.
The completed trip is stored in an ordered database index for history, reporting, and analytics.
Python Implementation Choices
Use dict for exact-key access to drivers, vehicles, requests, and ride states.
Use deque for work queues; avoid list.pop(0), which costs O(n).
Use heapq for a binary min-heap; negate values only when a max-heap is required.
Use list for compact sequential data and array.array when compact typed storage matters.
Python has no built-in linked list, tree, trie, spatial index, or graph; these require custom classes or specialized libraries.
For “find the nearest driver,” a spatial index plus heap is better than scanning every driver, which costs O(D) for D drivers. If exact key lookup is the dominant operation, choose a hash table instead; if sorted range queries dominate, choose a balanced tree.
Memory and Algorithm Complexity
For a road graph with V vertices and E edges:
Adjacency-list storage: O(V + E) total space.
Dijkstra with a binary heap: O((V + E) log V) time and O(V) auxiliary heap space.
BFS for unweighted paths: O(V + E) time and O(V) auxiliary space.
Top-k candidate selection: O(D + k log D) time using heap construction and k removals, with O(D) heap space.
These auxiliary bounds exclude the input graph, driver records, and existing indexes unless explicitly stated. The graph representation itself is O(V + E); no recursion stack is required by the iterative versions. Spatial-index query complexity depends on the implementation, so it should be benchmarked with realistic geographic distributions.
Common Pitfalls
Using list.pop(0) as a queue causes O(n) shifts on every dequeue.
Scanning all drivers instead of using a spatial index wastes time as the fleet grows.
Assuming hash-table operations are always O(1) ignores collision-related worst cases.
Applying Dijkstra’s algorithm to negative travel-time weights is invalid.
Forgetting disconnected roads, cycles, duplicate candidates, an empty driver pool, or rider locations with no nearby drivers.
Ignoring graph, index, and cache memory costs when the fleet and trip volume become large.
Interviewers commonly follow up by asking how the matching algorithm changes under a much larger fleet, how stale driver locations are handled, or how the system remains responsive during a surge in demand.
100
How do data structures personalize recommendation systems?
KnownRevise
A recommendation system uses purpose-specific data structures to personalize results: hash tables retrieve profiles, dense vectors calculate similarity, graphs model relationships, and heaps select the highest-scoring items.
Read full answerHide full answer
Applies tolanguage-independent (examples in Python 3.12)
Data Structures and Operation Costs
The table assumes (n) elements, a graph with (V) vertices and (E) edges, and the implementations indicated.
Structure
Access
Search
Insert
Delete
Extra space per element and total
Ordering
Dynamic array
(O(1))
(O(n))
(O(1)) amortized append, (O(n)) worst reallocation; (O(n)) in middle
(O(1)) amortized at end, (O(n)) worst resize; (O(n)) in middle
(O(1)) per slot; (O(n)) total
Yes
Hash table
(O(1)) average, (O(n)) worst
(O(1)) average, (O(n)) worst
(O(1)) average, (O(n)) worst
(O(1)) average, (O(n)) worst
(O(1)) expected metadata per entry; (O(n)) total
No inherent order; Python dict preserves insertion order
Balanced BST
(O(log n)) by key
(O(log n))
(O(log n))
(O(log n))
(O(1)) pointer fields per node; (O(n)) total
Sorted
Binary min-heap
(O(1)) by index or peek
(O(n))
(O(log n))
(O(log n)) for minimum; (O(n)) arbitrary deletion
(O(1)) per slot; (O(n)) total
Heap order only
Adjacency list using dict of sets
(O(1)) average, (O(V)) worst
(O(V+E)) traversal
(O(1)) average, (O(V)) worst per edge
(O(1)) average, (O(V)) worst per edge
(O(1)) per vertex or edge; (O(V+E)) total
No global order
collections.deque
(O(1)) at ends, (O(n)) by arbitrary index
(O(n))
(O(1)) at ends
(O(1)) at ends
(O(1)) per element; (O(n)) total
FIFO
Direct indexing explains constant-time array access, balanced trees limit searches to their height, heaps keep a shallow tree, and graph traversal may scan every reachable vertex and edge. Hash tables are fast on average but degrade under pathological collisions. Contiguous arrays are cache-friendly; linked structures add pointer overhead and generally scatter objects in memory.
Structures in the Recommendation Pipeline
Hash tables or dictionaries for profiles: Store preferences, watched items, clicks, and feature vectors. Lookups and updates are (O(1)) average and (O(n)) worst under collisions.
Compact arrays for embeddings: Python list acts as a dynamic array, while array.array provides compact typed storage. Scoring one (d)-dimensional vector takes (O(d)); storing (N) vectors takes (O(Nd)) space.
Graphs for related content: Users, items, creators, and interactions become vertices and edges. An adjacency list uses (O(V+E)) space and supports collaborative, social, and graph-neighbor recommendations. Python has no built-in general graph type.
Heaps for top-(k) selection: Incrementally retaining the best (k) candidates takes (O(Nlog(k+1))) time and (O(k)) selection space. heapq provides a binary min-heap on a list.
Balanced trees for ordered indexes: They find a key in (O(log n)) time, answer a range query returning (m) items in (O(log n + m)), and iterate all keys in sorted order in (O(n)), using (O(n)) space. Python has no built-in balanced BST.
Queues for streaming work: collections.deque supports event buffering and in-process task scheduling with (O(1)) enqueue and dequeue operations and (O(n)) storage.
Recommendation Flow and Trade-offs
Retrieve the user’s profile from a hash table.
Generate candidates using interactions, graph traversal, or vector similarity.
Score each candidate using its embedding.
Retain the best (k) candidates with a heap.
Sort the selected (k) items when descending output is required.
Use a hash table when lookup dominates, a graph when relationships matter, and a heap for top-(k) selection. A balanced BST is the alternative when sorted range queries are essential. Graph storage scales as (O(V+E)), while embedding storage scales as (O(Nd)).
Code Example
heapq.nlargest uses heap-based selection and returns the selected items in descending score order.
from heapq import nlargest
def recommend_top_k(
user_id: str,
profiles: dict[str, list[float]],
item_vectors: dict[str, list[float]],
candidate_ids: list[str],
k: int,
) -> list[str]:
"""Return up to k distinct candidates ranked by dot-product similarity."""
if k <= 0 or user_id not in profiles:
return []
user_vector = profiles[user_id]
def score(item_id: str) -> float:
vector = item_vectors[item_id]
if len(vector) != len(user_vector):
raise ValueError("User and item vectors must have equal dimensions")
return sum(left * right for left, right in zip(user_vector, vector))
candidates = list(dict.fromkeys(candidate_ids))
return nlargest(k, candidates, key=score)
profiles = {"u1": [1.0, 0.0]}
item_vectors = {
"a": [1.0, 0.0],
"b": [0.0, 1.0],
"c": [1.0, 0.0],
}
print(recommend_top_k("u1", profiles, item_vectors, ["a", "b", "c", "a"], 2))
# ['a', 'c']