Interview Q&A · questions with model answers
Algorithms 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 the sliding window pattern.
Maintain a contiguous window [L,R] expanding/shrinking to satisfy a constraint in amortized O(n).
-
Most asked Detect a cycle in a linked list—how?
Floyd: slow/fast pointers; if they meet, a cycle exists.
-
Most asked Reverse a singly linked list iteratively.
Three pointers: prev=None, cur=head; while cur: nxt=cur.next; cur.next=prev; prev=cur; cur=nxt; return prev.
-
Most asked How does Dijkstra work at a high level?
Priority queue of tentative distances; always expand the closest unsettled node; requires non-negative weights.
-
Most asked Topological sort—requirements and uses?
Only for DAGs; orders nodes so every edge u→v has u before v.
-
Most asked Name five high-frequency coding patterns.
Two pointers, sliding window, BFS/DFS, binary search on answer, DP/knapsack-style; also heap Top-K and Union-Find.
-
Most asked LRU cache—data structures?
Hash map key→node plus doubly linked list for O(1) move-to-front and eviction.
-
BFS uses which structure and why?
A FIFO queue so nodes are processed level by level / in discovery order.
-
Min-heap operations complexity?
Peek min O(1); insert and extract-min O(log n) for binary heap.
-
Inorder traversal of a BST yields what?
Keys in sorted ascending order (given standard left < node < right ordering).
-
Lowest common ancestor in a binary tree (not BST)?
Recurse left/right; if both sides find a target, node is LCA; else return the non-null side.
-
Detect a cycle in a directed graph?
DFS colors (visiting recursion stack) or Kahn's algorithm failing to order all nodes.
-
Does recursion use extra space?
Yes—call stack depth.
-
How do prefix sums help?
Precompute cumulative sums so any range sum is O(1) via prefix[r]-prefix[l-1].
-
Kadane's algorithm solves what?
Maximum subarray sum in O(n) by tracking best ending here / best overall.
-
Find the middle of a linked list in one pass.
Slow moves 1, fast moves 2; when fast ends, slow is at middle.
-
Merge two sorted linked lists—approach?
Iterate both with a dummy tail, always attach the smaller node; append remainder.
-
k-th smallest in a BST?
Inorder traversal counting visits; stop at k.
-
Binary search bug that causes infinite loops?
Mishandling mid and bounds so the search interval never shrinks (e.g., always setting lo=mid on integers).
-
Search in a rotated sorted array—key idea?
One half is always sorted; decide which half contains the target using mid vs ends.
-
Classic recurrence for Fibonacci and its DP cost?
F(n)=F(n-1)+F(n-2); naive exponential, memo/tabulation O(n) time O(n) or O(1) space rolling.
-
Longest common subsequence—complexity of standard DP?
O(m·n) time and space for strings of lengths m,n (space can be optimized to O(min(m,n))).
-
Edit distance measures what?
Minimum insert/delete/replace ops to transform one string into another; classic DP on prefixes.
-
House Robber intuition?
At each house: max(skip + rob previous best, take + rob best two back); linear DP.
-
Grid unique paths DP state?
dp[i][j] = paths to cell (i,j) = from above + from left, with first row/col base cases.
-
Serialize and deserialize a binary tree—common approach?
Preorder with null markers, or level-order BFS encoding; parse with the same convention.
-
Is Python's sorted stable?
Yes—Timsort is a stable mergesort hybrid.
-
Name a probabilistic alternative to exact set membership.
Bloom filter—fast, space-efficient, allows false positives but not false negatives (typically).