Algorithm Analysis for Coding Interviews
Understanding algorithm analysis is crucial for coding interviews, as it involves evaluating time and space complexity using Big O notation. Time complexity assesses how an algorithm's running time increases with input size, while space complexity measures additional memory usage. Common complexities include O(n) and O(n log n) for time, and O(1) or O(n) for space. Candidates should articulate trade-offs between time and space, demonstrating awareness of performance implications. Amortized complexity is also important, particularly for operations like appending to Python lists. Clear communication of complexity analysis is essential during interviews to showcase problem-solving skills.
Every data structure chapter in this course references time and space complexity. It is the language of coding interviews. Every time we choose a hash table over an array, or a heap over a sorted list, 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 array 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 | Array index access, hash table 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 array 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. 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 |
Hash table or array 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 array on every step costs
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 Python's dynamic array, which is what a Python list is under the hood.
Appending to a Python list is
Most common Python 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 resize 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 a set for O(1) membership checks. |
| O(1) | Hash-based lookup. Always prefer the list for membership. |
| O(1) | Average case. Worst case O(n) due to hash collisions. |
| O(n log n) | Timsort. Always O(n log n) regardless of input order. |
| O(1) | The correct way to implement a queue in Python. |
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 hash table 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