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 What does Big-O describe in interviews?
Asymptotic upper bound on growth of time/space as input size grows—not exact runtime on one machine.
-
Most asked What is Union-Find used for?
Dynamic connectivity / components with near O(1) amortized union and find (with optimizations).
-
Most asked What two properties justify DP?
Optimal substructure and overlapping subproblems.
-
What is amortized analysis? Give an example.
Average cost per op over a sequence; dynamic array append is amortized O(1) despite occasional O(n) resizes.
-
What is an in-place algorithm?
Uses only a small constant (or logarithmic) amount of extra memory beyond the input.
-
What is a monotonic stack?
Stack kept strictly increasing/decreasing to answer next greater/smaller in amortized O(n).
-
What does a min-stack need beyond a normal stack?
Track current minimum—often via an auxiliary stack of mins so getMin is O(1).
-
What is tree diameter?
Longest path between any two nodes; often computed via heights or two BFS/DFS passes.
-
What must be true of dict keys in Python?
They must be hashable (immutable with stable hash) and consistent with equality.
-
What edge cases should you always test?
Empty input, single element, duplicates, negatives, overflow/large n, disconnected graphs, skewed trees.
-
What is a bipartite graph check?
Try 2-coloring via BFS/DFS; if an edge joins same color, not bipartite.