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.

  1. Most asked Explain a list and its typical time complexities.

    Ordered mutable sequence; amortised O(1) append; O(n) insert/delete in middle.

  2. 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.

  3. Most asked Explain a set and its typical time complexities.

    Unordered unique hashable elements; O(1) average membership.

  4. Most asked Explain a hash table and its typical time complexities.

    Array + hash function + collision strategy (chaining/open addressing).

  5. Most asked Describe BFS and when you would use it.

    Level-order graph/tree traversal using a queue; shortest path in unweighted graphs.

  6. Most asked Describe DFS and when you would use it.

    Deep exploration with stack/recursion; cycle detection, topological sort building block.

  7. Most asked Describe binary search and when you would use it.

    O(log n) search on sorted arrays; careful mid/overflow and boundaries.

  8. 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).

  9. Most asked Describe sliding window and when you would use it.

    Maintain a window invariant for subarray/substring problems.

  10. Most asked Describe dynamic programming and when you would use it.

    Optimal substructure + overlapping subproblems; memoize or tabulate.

  11. Most asked Describe LRU cache design and when you would use it.

    Hash map + doubly linked list for O(1) get/put.

  12. Most asked Explain a deque and its typical time complexities.

    Double-ended queue with O(1) append/pop from both ends.

  13. Most asked Explain a heapq / priority queue and its typical time complexities.

    Binary heap for min-priority operations in O(log n).

  14. Explain a tuple and its typical time complexities.

    Ordered immutable sequence; hashable if contents are; good for fixed records.

  15. Explain a BST and its typical time complexities.

    Left < node < right; average O(log n) search if balanced.

  16. Explain a graph (adjacency list) and its typical time complexities.

    Vertices + neighbor lists; space-efficient for sparse graphs.

  17. Explain a stack and its typical time complexities.

    LIFO; use list append/pop or deque.

  18. Explain a queue and its typical time complexities.

    FIFO; prefer deque over list pop(0).

  19. Explain a linked list and its typical time complexities.

    Nodes with pointers; O(1) insert given node but O(n) index access.

  20. Explain a binary tree and its typical time complexities.

    Hierarchical nodes with ≤2 children; basis for BST/heap.

  21. Explain a trie and its typical time complexities.

    Prefix tree for string dictionaries and autocomplete.

  22. Explain a union-find (DSU) and its typical time complexities.

    Disjoint sets with near O(1) union/find amortized.

  23. Describe prefix sums and when you would use it.

    Precompute cumulative sums for O(1) range sum queries.

  24. Describe sorting: quicksort and when you would use it.

    Average O(n log n) partition-based; worst O(n²) without care.

  25. Describe sorting: mergesort and when you would use it.

    Stable O(n log n) divide-and-conquer; needs extra space.

  26. Describe Dijkstra and when you would use it.

    Shortest paths with non-negative weights using priority queue.

  27. Describe topological sort and when you would use it.

    Linear order of DAG nodes; Kahn's BFS or DFS finishing times.

  28. Describe greedy algorithms and when you would use it.

    Locally optimal choices; prove exchange argument or use when safe.

  29. Describe backtracking and when you would use it.

    Build candidates and prune; classic for subsets/permutations/N-Queens.

  30. Describe Kadane's algorithm and when you would use it.

    Maximum subarray sum in O(n).

  31. Describe detect cycle in linked list and when you would use it.

    Floyd's tortoise/hare two pointers.

  32. Amortized analysis?

    Average cost per operation over a sequence (e.g., dynamic array growth).

  33. Best/average/worst case?

    Performance depending on input distribution; state assumptions.

  34. In Python DSA interviews, explain: __hash__ and __eq__.

    Hashable objects must have consistent hash/eq; mutable containers are unhashable.

  35. In Python DSA interviews, explain: generators and memory.

    Lazy iteration reduces peak memory vs materializing lists.

  36. Explain a frozenset and its typical time complexities.

    Immutable set; can be a dict key.

  37. Explain a AVL / red-black tree and its typical time complexities.

    Self-balancing BSTs guaranteeing O(log n).

  38. Explain a graph (adjacency matrix) and its typical time complexities.

    V×V matrix; fast edge checks; heavy for sparse graphs.

  39. Explain a bloom filter and its typical time complexities.

    Probabilistic set membership with false positives, no false negatives.

  40. Explain a segment tree and its typical time complexities.

    Range query / update structure in O(log n).

  41. Describe sorting: heapsort and when you would use it.

    O(n log n) using heap; in-place but not stable.

  42. Describe Bellman-Ford and when you would use it.

    Shortest paths allowing negative edges; detects negative cycles.

  43. Describe KMP / string matching and when you would use it.

    Linear-time pattern search with prefix table.

  44. Describe serialize binary tree and when you would use it.

    Preorder with null markers or level-order encoding.

  45. In-place algorithm?

    Uses O(1) extra memory beyond input (aside from recursion stack sometimes).

  46. In Python DSA interviews, explain: dict insertion order.

    CPython 3.7+ preserves insertion order as language guarantee.

  47. In Python DSA interviews, explain: list comprehension cost.

    Still O(n); builds a full list—use generators for streaming.

  48. In Python DSA interviews, explain: bisect module.

    Binary search helpers on sorted lists.

  49. In Python DSA interviews, explain: setdefault / Counter.

    Useful patterns for frequency maps and grouping.

  50. Explain the time complexity of common dictionary operations in Python.

    Python dictionaries use hash tables.

Was this page helpful?

Report a mistake