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.
-
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. -
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. -
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. -
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. -
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. -
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. -
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. -
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. -
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. -
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. -
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. -
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. -
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. -
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. -
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. -
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. - What is amortized analysis? Give an example.
- Explain average-case vs worst-case for hash tables.
- How do you report complexity of BFS/DFS?
- When does binary search apply beyond sorted arrays?
- Linked list vs array tradeoffs?
- BFS uses which structure and why?
- Min-heap operations complexity?
- Inorder traversal of a BST yields what?
- Lowest common ancestor in a binary tree (not BST)?
- Balanced BST vs skewed BST complexity?
- Adjacency list vs matrix?
- When is BFS preferred for shortest path?
- Detect a cycle in a directed graph?
- 0/1 knapsack vs unbounded knapsack difference?
- Greedy vs DP—how do you choose?
- How should you start a coding interview problem?
- Difference between Big-O, Big-Theta, and Big-Omega?
- Does recursion use extra space?
- When is O(n log n) preferable to O(n²)?
- How do prefix sums help?
- Why is list.pop(0) slow in Python?
- What is an in-place algorithm?
- Kadane's algorithm solves what?
- Why use a dummy head node?
- Find the middle of a linked list in one pass.
- Merge two sorted linked lists—approach?
- When is a stack the right tool?
- What is a monotonic stack?
- Implement a queue using two stacks—idea?
- What does a min-stack need beyond a normal stack?
- What is tree diameter?
- k-th smallest in a BST?
- Bellman-Ford vs Dijkstra?
- How do you find connected components in an undirected graph?
- What must be true of dict keys in Python?
- Merge sort vs quicksort tradeoffs?
- Binary search bug that causes infinite loops?
- Search in a rotated sorted array—key idea?
- Classic recurrence for Fibonacci and its DP cost?
- Longest common subsequence—complexity of standard DP?
- Edit distance measures what?
- When is backtracking preferred over DP?
- House Robber intuition?
- Grid unique paths DP state?
- What edge cases should you always test?
- Why narrate tradeoffs in interviews?
- What is the lower bound for comparison-based sorting?
- Serialize and deserialize a binary tree—common approach?
- What is a bipartite graph check?
- Chaining vs open addressing?
- Why resize a hash table?
- When can you sort in O(n)?
- Is Python's sorted stable?
- Name a probabilistic alternative to exact set membership.