Interview Q&A · 70 questions · 16 answered publicly

Algorithms Interview Questions

Ordered by how often they're asked. Sign in free to see every answer, rate your confidence and run timed mock interviews.

  1. Most asked What does Big-O describe in interviews?

    Summary

    Asymptotic upper bound on growth of time/space as input size grows—not exact runtime on one machine.

    Longer Answer

    Asymptotic upper bound on growth of time/space as input size grows—not exact runtime on one machine. Complexity literacy.

    Key Points

    • Asymptotic upper bound on growth of time/space as input size grows—not exact runtime on one machine
    • Complexity literacy
    • Relevant areas: Dsa, Complexity
    • Use the answer to connect the concept back to a realistic production or interview scenario for 'What does Big-O describe in interviews?'

    Comparison / Tradeoffs

    Aspect Notes
    Core idea Asymptotic upper bound on growth of time/space as input size grows—not exact runtime on one machine.
    Practical tradeoff Complexity literacy.

    Permalink

  2. Most asked Explain the sliding window pattern.

    Summary

    Maintain a contiguous window [L,R] expanding/shrinking to satisfy a constraint in amortized O(n).

    Longer Answer

    Maintain a contiguous window [L,R] expanding/shrinking to satisfy a constraint in amortized O(n). Window technique.

    Key Points

    • Maintain a contiguous window [L,R] expanding/shrinking to satisfy a constraint in amortized O(n)
    • Window technique
    • Relevant areas: Dsa, Arrays, Strings
    • Use the answer to connect the concept back to a realistic production or interview scenario for 'Explain the sliding window pattern.'

    Comparison / Tradeoffs

    Aspect Notes
    Core idea Maintain a contiguous window [L,R] expanding/shrinking to satisfy a constraint in amortized O(n).
    Practical tradeoff Window technique.

    Permalink

  3. Most asked Two Sum in O(n) average time—how?

    Summary

    Scan once; store seen values in a hash map; look up complement target-x.

    Longer Answer

    Scan once; store seen values in a hash map; look up complement target-x. Hash lookup pattern.

    Key Points

    • Scan once; store seen values in a hash map; look up complement target-x
    • Hash lookup pattern
    • Relevant areas: Dsa, Arrays, Hashing
    • Use the answer to connect the concept back to a realistic production or interview scenario for 'Two Sum in O(n) average time—how?'

    Comparison / Tradeoffs

    Aspect Notes
    Core idea Scan once; store seen values in a hash map; look up complement target-x.
    Practical tradeoff Hash lookup pattern.

    Permalink

  4. Most asked How do you merge overlapping intervals?

    Summary

    Sort by start; scan and merge when current.start <= last.end, else append a new interval.

    Longer Answer

    Sort by start; scan and merge when current.start <= last.end, else append a new interval. Interval merge.

    Key Points

    • Sort by start; scan and merge when current.start <= last.end, else append a new interval
    • Interval merge
    • Relevant areas: Dsa, Arrays, Sorting
    • Use the answer to connect the concept back to a realistic production or interview scenario for 'How do you merge overlapping intervals?'

    Comparison / Tradeoffs

    Aspect Notes
    Core idea Sort by start; scan and merge when current.start <= last.end, else append a new interval.
    Practical tradeoff Interval merge.

    Permalink

  5. Most asked Detect a cycle in a linked list—how?

    Summary

    Floyd: slow/fast pointers; if they meet, a cycle exists.

    Longer Answer

    Floyd: slow/fast pointers; if they meet, a cycle exists. Reset one to head to find entry.

    Key Points

    • Floyd: slow/fast pointers; if they meet, a cycle exists
    • Reset one to head to find entry
    • Cycle detection
    • Use the answer to connect the concept back to a realistic production or interview scenario for 'Detect a cycle in a linked list—how?'

    Comparison / Tradeoffs

    Aspect Notes
    Core idea Floyd: slow/fast pointers; if they meet, a cycle exists.
    Practical tradeoff Cycle detection.

    Permalink

  6. Most asked Reverse a singly linked list iteratively.

    Summary

    Three pointers: prev=None, cur=head; while cur: nxt=cur.next; cur.next=prev; prev=cur; cur=nxt; return prev.

    Longer Answer

    Three pointers: prev=None, cur=head; while cur: nxt=cur.next; cur.next=prev; prev=cur; cur=nxt; return prev. List reverse.

    Key Points

    • Three pointers: prev=None, cur=head; while cur: nxt=cur.next; cur.next=prev; prev=cur; cur=nxt; return prev
    • List reverse
    • Relevant areas: Dsa, Linked List
    • Use the answer to connect the concept back to a realistic production or interview scenario for 'Reverse a singly linked list iteratively.'

    Comparison / Tradeoffs

    Aspect Notes
    Core idea Three pointers: prev=None, cur=head; while cur: nxt=cur.next; cur.next=prev; prev=cur; cur=nxt; return prev.
    Practical tradeoff List reverse.

    Permalink

  7. Most asked How do you get Top K elements efficiently?

    Summary

    Maintain a size-K heap (min-heap of K for largest K) in O(n log K), or quickselect average O(n).

    Longer Answer

    Maintain a size-K heap (min-heap of K for largest K) in O(n log K), or quickselect average O(n). Top K.

    Key Points

    • Maintain a size-K heap (min-heap of K for largest K) in O(n log K), or quickselect average O(n)
    • Top K
    • Relevant areas: Dsa, Heap
    • Use the answer to connect the concept back to a realistic production or interview scenario for 'How do you get Top K elements efficiently?'

    Comparison / Tradeoffs

    Aspect Notes
    Core idea Maintain a size-K heap (min-heap of K for largest K) in O(n log K), or quickselect average O(n).
    Practical tradeoff Top K.

    Permalink

  8. Most asked How do you validate a binary search tree?

    Summary

    DFS with allowable (low, high) bounds per node, or check inorder is strictly increasing.

    Longer Answer

    DFS with allowable (low, high) bounds per node, or check inorder is strictly increasing. Validate BST.

    Key Points

    • DFS with allowable (low, high) bounds per node, or check inorder is strictly increasing
    • Validate BST
    • Relevant areas: Dsa, Trees, Bst
    • Use the answer to connect the concept back to a realistic production or interview scenario for 'How do you validate a binary search tree?'

    Comparison / Tradeoffs

    Aspect Notes
    Core idea DFS with allowable (low, high) bounds per node, or check inorder is strictly increasing.
    Practical tradeoff Validate BST.

    Permalink

  9. Most asked How does Dijkstra work at a high level?

    Summary

    Priority queue of tentative distances; always expand the closest unsettled node; requires non-negative weights.

    Longer Answer

    Priority queue of tentative distances; always expand the closest unsettled node; requires non-negative weights. Dijkstra.

    Key Points

    • Priority queue of tentative distances; always expand the closest unsettled node; requires non-negative weights
    • Dijkstra
    • Relevant areas: Dsa, Graphs
    • Use the answer to connect the concept back to a realistic production or interview scenario for 'How does Dijkstra work at a high level?'

    Comparison / Tradeoffs

    Aspect Notes
    Core idea Priority queue of tentative distances; always expand the closest unsettled node; requires non-negative weights.
    Practical tradeoff Dijkstra.

    Permalink

  10. Most asked Topological sort—requirements and uses?

    Summary

    Only for DAGs; orders nodes so every edge u→v has u before v.

    Longer Answer

    Only for DAGs; orders nodes so every edge u→v has u before v. Used for course schedules / build deps.

    Key Points

    • Only for DAGs; orders nodes so every edge u→v has u before v
    • Used for course schedules / build deps
    • Topo sort
    • Use the answer to connect the concept back to a realistic production or interview scenario for 'Topological sort—requirements and uses?'

    Comparison / Tradeoffs

    Aspect Notes
    Core idea Only for DAGs; orders nodes so every edge u→v has u before v.
    Practical tradeoff Topo sort.

    Permalink

  11. Most asked What is Union-Find used for?

    Summary

    Dynamic connectivity / components with near O(1) amortized union and find (with optimizations).

    Longer Answer

    Dynamic connectivity / components with near O(1) amortized union and find (with optimizations). DSU.

    Key Points

    • Dynamic connectivity / components with near O(1) amortized union and find (with optimizations)
    • DSU
    • Relevant areas: Dsa, Graphs, Union Find
    • Use the answer to connect the concept back to a realistic production or interview scenario for 'What is Union-Find used for?'

    Comparison / Tradeoffs

    Aspect Notes
    Core idea Dynamic connectivity / components with near O(1) amortized union and find (with optimizations).
    Practical tradeoff DSU.

    Permalink

  12. Most asked What two properties justify DP?

    Summary

    Optimal substructure and overlapping subproblems.

    Longer Answer

    Optimal substructure and overlapping subproblems. DP definition.

    Key Points

    • Optimal substructure and overlapping subproblems
    • DP definition
    • Relevant areas: Dsa, Dp
    • Use the answer to connect the concept back to a realistic production or interview scenario for 'What two properties justify DP?'

    Comparison / Tradeoffs

    Aspect Notes
    Core idea Optimal substructure and overlapping subproblems.
    Practical tradeoff DP definition.

    Permalink

  13. Most asked Memoization vs tabulation?

    Summary

    Memo: top-down recursion + cache.

    Longer Answer

    Memo: top-down recursion + cache. Tabulation: bottom-up iterative filling of a table.

    Key Points

    • Memo: top-down recursion + cache
    • Tabulation: bottom-up iterative filling of a table
    • DP styles
    • Use the answer to connect the concept back to a realistic production or interview scenario for 'Memoization vs tabulation?'

    Comparison / Tradeoffs

    Aspect Notes
    Core idea Memo: top-down recursion + cache.
    Practical tradeoff DP styles.

    Permalink

  14. Most asked Name five high-frequency coding patterns.

    Summary

    Two pointers, sliding window, BFS/DFS, binary search on answer, DP/knapsack-style; also heap Top-K and Union-Find.

    Longer Answer

    Two pointers, sliding window, BFS/DFS, binary search on answer, DP/knapsack-style; also heap Top-K and Union-Find. Pattern literacy.

    Key Points

    • Two pointers, sliding window, BFS/DFS, binary search on answer, DP/knapsack-style; also heap Top-K and Union-Find
    • Pattern literacy
    • Relevant areas: Dsa, Patterns
    • Use the answer to connect the concept back to a realistic production or interview scenario for 'Name five high-frequency coding patterns.'

    Comparison / Tradeoffs

    Aspect Notes
    Core idea Two pointers, sliding window, BFS/DFS, binary search on answer, DP/knapsack-style; also heap Top-K and Union-Find.
    Practical tradeoff Pattern literacy.

    Permalink

  15. Most asked LRU cache—data structures?

    Summary

    Hash map key→node plus doubly linked list for O(1) move-to-front and eviction.

    Longer Answer

    Hash map key→node plus doubly linked list for O(1) move-to-front and eviction. Design + DSA.

    Key Points

    • Hash map key→node plus doubly linked list for O(1) move-to-front and eviction
    • Design + DSA
    • Relevant areas: Dsa, Design, Hashing
    • Use the answer to connect the concept back to a realistic production or interview scenario for 'LRU cache—data structures?'

    Comparison / Tradeoffs

    Aspect Notes
    Core idea Hash map key→node plus doubly linked list for O(1) move-to-front and eviction.
    Practical tradeoff Design + DSA.

    Permalink

  16. Most asked BFS vs DFS on trees—when each?

    Summary

    BFS for level order / shortest depth; DFS for path problems, subtree properties, recursion-friendly shapes.

    Longer Answer

    BFS for level order / shortest depth; DFS for path problems, subtree properties, recursion-friendly shapes. Tree traversal choice.

    Key Points

    • BFS for level order / shortest depth; DFS for path problems, subtree properties, recursion-friendly shapes
    • Tree traversal choice
    • Relevant areas: Dsa, Trees
    • Use the answer to connect the concept back to a realistic production or interview scenario for 'BFS vs DFS on trees—when each?'

    Comparison / Tradeoffs

    Aspect Notes
    Core idea BFS for level order / shortest depth; DFS for path problems, subtree properties, recursion-friendly shapes.
    Practical tradeoff Tree traversal choice.

    Permalink

  17. What is amortized analysis? Give an example.
  18. Explain average-case vs worst-case for hash tables.
  19. How do you report complexity of BFS/DFS?
  20. When does binary search apply beyond sorted arrays?
  21. Linked list vs array tradeoffs?
  22. BFS uses which structure and why?
  23. Min-heap operations complexity?
  24. Inorder traversal of a BST yields what?
  25. Lowest common ancestor in a binary tree (not BST)?
  26. Balanced BST vs skewed BST complexity?
  27. Adjacency list vs matrix?
  28. When is BFS preferred for shortest path?
  29. Detect a cycle in a directed graph?
  30. 0/1 knapsack vs unbounded knapsack difference?
  31. Greedy vs DP—how do you choose?
  32. How should you start a coding interview problem?
  33. Difference between Big-O, Big-Theta, and Big-Omega?
  34. Does recursion use extra space?
  35. When is O(n log n) preferable to O(n²)?
  36. How do prefix sums help?
  37. Why is list.pop(0) slow in Python?
  38. What is an in-place algorithm?
  39. Kadane's algorithm solves what?
  40. Why use a dummy head node?
  41. Find the middle of a linked list in one pass.
  42. Merge two sorted linked lists—approach?
  43. When is a stack the right tool?
  44. What is a monotonic stack?
  45. Implement a queue using two stacks—idea?
  46. What does a min-stack need beyond a normal stack?
  47. What is tree diameter?
  48. k-th smallest in a BST?
  49. Bellman-Ford vs Dijkstra?
  50. How do you find connected components in an undirected graph?
  51. What must be true of dict keys in Python?
  52. Merge sort vs quicksort tradeoffs?
  53. Binary search bug that causes infinite loops?
  54. Search in a rotated sorted array—key idea?
  55. Classic recurrence for Fibonacci and its DP cost?
  56. Longest common subsequence—complexity of standard DP?
  57. Edit distance measures what?
  58. When is backtracking preferred over DP?
  59. House Robber intuition?
  60. Grid unique paths DP state?
  61. What edge cases should you always test?
  62. Why narrate tradeoffs in interviews?
  63. What is the lower bound for comparison-based sorting?
  64. Serialize and deserialize a binary tree—common approach?
  65. What is a bipartite graph check?
  66. Chaining vs open addressing?
  67. Why resize a hash table?
  68. When can you sort in O(n)?
  69. Is Python's sorted stable?
  70. Name a probabilistic alternative to exact set membership.