Algorithm Analysis for Coding Interviews
Understand how to evaluate algorithm efficiency by analyzing time and space complexity using Big O notation. Learn to explain trade-offs clearly and frame your decisions strategically to succeed in coding interviews.
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 Go slices (dynamic arrays) and how their capacity grows.
Appending to a slice with append is
Most common Go 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) when the slice grows, but amortized O(1). |
| O(n) | If you actually delete index 0, elements shift left. Use |
| O(n) | Every element after index i shifts right. |
| O(n) | Linear scan. Use a hash set for O(1) membership checks (often a map[T]struct{}). |
| O(1) | Hash-based lookup. Prefer the set over a slice for membership checks. |
| O(1) | Average case. Worst case O(n) due to hash collisions. |
| O(n log n) | Comparison sort. O(n log n) in the typical/worst-case bounds used in interviews. |
| O(1) | A typical way to implement a queue in Go is a ring buffer (or two-index slice pattern) for O(1) enqueue/dequeue. |
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