Interview Q&A · questions with model answers
Data Structures Interview Questions
Part of the study plan for: Machine Learning Engineer
Ordered by how often they're asked. Most-asked answers are free to read in full; members see every answer and can rate their confidence on each.
-
Most asked Explain a list and its typical time complexities.
Ordered mutable sequence; amortised O(1) append; O(n) insert/delete in middle.
-
Most asked Explain a dict and its typical time complexities.
Hash map of keys to values; average O(1) get/set; keys must be hashable.
-
Most asked Explain a set and its typical time complexities.
Unordered unique hashable elements; O(1) average membership.
-
Most asked Explain a hash table and its typical time complexities.
Array + hash function + collision strategy (chaining/open addressing).
-
Most asked Describe BFS and when you would use it.
Level-order graph/tree traversal using a queue; shortest path in unweighted graphs.
-
Most asked Describe DFS and when you would use it.
Deep exploration with stack/recursion; cycle detection, topological sort building block.
-
Most asked Describe binary search and when you would use it.
O(log n) search on sorted arrays; careful mid/overflow and boundaries.
-
Most asked Describe two pointers and when you would use it.
Coordinate indices from ends or same direction to solve array problems in O(n).
-
Most asked Describe sliding window and when you would use it.
Maintain a window invariant for subarray/substring problems.
-
Most asked Describe dynamic programming and when you would use it.
Optimal substructure + overlapping subproblems; memoize or tabulate.
-
Most asked Describe LRU cache design and when you would use it.
Hash map + doubly linked list for O(1) get/put.
-
Most asked Explain a deque and its typical time complexities.
Double-ended queue with O(1) append/pop from both ends.
-
Most asked Explain a heapq / priority queue and its typical time complexities.
Binary heap for min-priority operations in O(log n).
-
Explain a tuple and its typical time complexities.
Ordered immutable sequence; hashable if contents are; good for fixed records.
-
Explain a BST and its typical time complexities.
Left < node < right; average O(log n) search if balanced.
-
Explain a graph (adjacency list) and its typical time complexities.
Vertices + neighbor lists; space-efficient for sparse graphs.
-
Explain a stack and its typical time complexities.
LIFO; use list append/pop or deque.
-
Explain a queue and its typical time complexities.
FIFO; prefer deque over list pop(0).
-
Explain a linked list and its typical time complexities.
Nodes with pointers; O(1) insert given node but O(n) index access.
-
Explain a binary tree and its typical time complexities.
Hierarchical nodes with ≤2 children; basis for BST/heap.
-
Explain a trie and its typical time complexities.
Prefix tree for string dictionaries and autocomplete.
-
Explain a union-find (DSU) and its typical time complexities.
Disjoint sets with near O(1) union/find amortized.
-
Describe prefix sums and when you would use it.
Precompute cumulative sums for O(1) range sum queries.
-
Describe sorting: quicksort and when you would use it.
Average O(n log n) partition-based; worst O(n²) without care.
-
Describe sorting: mergesort and when you would use it.
Stable O(n log n) divide-and-conquer; needs extra space.
-
Describe Dijkstra and when you would use it.
Shortest paths with non-negative weights using priority queue.
-
Describe topological sort and when you would use it.
Linear order of DAG nodes; Kahn's BFS or DFS finishing times.
-
Describe greedy algorithms and when you would use it.
Locally optimal choices; prove exchange argument or use when safe.
-
Describe backtracking and when you would use it.
Build candidates and prune; classic for subsets/permutations/N-Queens.
-
Describe Kadane's algorithm and when you would use it.
Maximum subarray sum in O(n).
-
Describe detect cycle in linked list and when you would use it.
Floyd's tortoise/hare two pointers.
-
Amortized analysis?
Average cost per operation over a sequence (e.g., dynamic array growth).
-
Best/average/worst case?
Performance depending on input distribution; state assumptions.
-
In Python DSA interviews, explain: __hash__ and __eq__.
Hashable objects must have consistent hash/eq; mutable containers are unhashable.
-
In Python DSA interviews, explain: generators and memory.
Lazy iteration reduces peak memory vs materializing lists.
-
Explain a frozenset and its typical time complexities.
Immutable set; can be a dict key.
-
Explain a AVL / red-black tree and its typical time complexities.
Self-balancing BSTs guaranteeing O(log n).
-
Explain a graph (adjacency matrix) and its typical time complexities.
V×V matrix; fast edge checks; heavy for sparse graphs.
-
Explain a bloom filter and its typical time complexities.
Probabilistic set membership with false positives, no false negatives.
-
Explain a segment tree and its typical time complexities.
Range query / update structure in O(log n).
-
Describe sorting: heapsort and when you would use it.
O(n log n) using heap; in-place but not stable.
-
Describe Bellman-Ford and when you would use it.
Shortest paths allowing negative edges; detects negative cycles.
-
Describe KMP / string matching and when you would use it.
Linear-time pattern search with prefix table.
-
Describe serialize binary tree and when you would use it.
Preorder with null markers or level-order encoding.
-
In-place algorithm?
Uses O(1) extra memory beyond input (aside from recursion stack sometimes).
-
In Python DSA interviews, explain: dict insertion order.
CPython 3.7+ preserves insertion order as language guarantee.
-
In Python DSA interviews, explain: list comprehension cost.
Still O(n); builds a full list—use generators for streaming.
-
In Python DSA interviews, explain: bisect module.
Binary search helpers on sorted lists.
-
In Python DSA interviews, explain: setdefault / Counter.
Useful patterns for frequency maps and grouping.
-
Explain the time complexity of common dictionary operations in Python.
Python dictionaries use hash tables.