Algorithm Analysis for Coding Interviews
Understand how to analyze algorithm time and space complexity using Big O notation in coding interviews. Learn to communicate trade-offs clearly, optimize solutions, and justify performance decisions effectively.
Every data structure chapter in this course references time and space complexity. It is the language of coding interviews. Every time we choose an unordered_map over a vector, or a heap over a sorted vector, we are making a claim about performance. Big
This lesson is a focused recap of what complexity analysis means in an interview context and how to use it to make and justify decisions under pressure.
What Big O measures
Big O notation describes how the running time or memory usage of an algorithm grows as the input size grows. It is a way of comparing algorithms independent of hardware, language, and implementation details.
In an interview, Big O serves one purpose: it tells us whether our solution will be fast enough for large inputs, and it gives us a shared vocabulary to discuss trade-offs with the interviewer.
Time complexity and space complexity
Time complexity measures how the running time of an algorithm changes as the input size grows. An algorithm with
Space complexity measures how much additional memory an algorithm uses beyond the input itself. We exclude the input because it is given to us and only measure the memory our algorithm allocates on top of it. An algorithm that allocates a new vector of size
Common time complexity classes
These are the complexity classes that appear most frequently in coding interviews, ordered from most to least efficient.
Complexity | Name | Example | Interview context |
O(1) | Constant | vector index access, unordered_map lookup | The best possible. Always mention when achieved. |
O(log n) | Logarithmic | Binary search, BST operations | Excellent. Halving the search space on each step. |
O(n) | Linear | Single vector scan, DFS, BFS | Standard target for most interview problems. |
O(n log n) | Linearithmic | Sorting, heap operations on n elements | Acceptable. Often, the best achievable solution for sorting problems. |
O(n2) | Quadratic | Nested loops, bubble sort | Usually the brute force approach. Mention it and optimize from here. |
O(2n) | Exponential | Recursive subsets, naive recursion | Almost always too slow. A signal that memoization is needed. |
For most coding interview problems, the target is
Common space complexity patterns
Space complexity follows the same Big O notation as time complexity, but measures memory rather than running time. The patterns below cover the space costs that appear most frequently in interview solutions.
Pattern | Space | Example |
Fixed number of variables | O(1) | Two pointers, sliding window |
unordered_map or vector of size $n$ | O(n) | Frequency count, prefix sums |
Recursive call stack of depth $h$ | O(h) | DFS on a tree of height $h$ |
Storing all subsets or combinations | O(2n) | Backtracking problems |
Time vs. space trade-offs
Most interview optimizations involve trading space for time. A brute force solution that scans the vector on every step costs unordered_set or unordered_map to store previously seen values reduces the time to
The reverse trade-off, sacrificing time to save space, is less common but does appear. When an interviewer asks for an
How to frame trade-offs: When discussing trade-offs with an interviewer, be explicit. Say: "I can solve this in
Amortized complexity
Amortized complexity describes the average cost of an operation over a sequence of operations, rather than the worst-case cost of a single operation. It comes up most often with C++'s dynamic array, std::vector.
Appending to a vector with push_back is vector::push_back is
Most common C++ operations
These are the operations that come up most frequently in interviews and where the complexity surprises candidates.
Operation | Time Complexity | Description |
| O(1) amortized | Occasionally O(n) on reallocation but amortized O(1). |
| O(n) | Every element shifts left. Use |
| O(n) | Every element after index i shifts right. |
| O(n) | Linear scan. Use an unordered_set for O(1) average membership checks. |
| O(1) average |
|
| O(1) average | Average case for unordered_map. Worst case O(n) due to hash collisions. |
| O(n log n) |
|
| O(1) | Use |
How to analyze your own solution
In an interview, we need to analyze our solution quickly and accurately. A reliable approach is to count the loops and recursive calls, then reason about what each one does to the complexity.
A single loop over
Space complexity follows the same logic. Count the data structures we allocate. A single unordered_map or unordered_set storing
Common mistake (ignoring space complexity): Many candidates state only time complexity and forget space. Interviewers notice this. Always state both. If the space complexity is
Complexity analysis is a tool for communication, not just calculation. The goal in an interview is not to produce a mathematically rigorous proof. It is to demonstrate that we understand how our solution scales and can discuss the trade-offs clearly. Every chapter that follows builds on this foundation.
Interview lens: In an interview, always state the time and space complexity of your solution before the interviewer asks. Say: "This runs in