Understand memory usage and how to trade space for time (and vice versa).
Space complexity measures how much extra memory an algorithm uses as input grows. We distinguish between input space (the original data) and auxiliary space (extra memory your algorithm allocates). In interviews, optimizing space often means choosing between an O(n) hash map (fast) or an O(1) two-pointer approach (memory efficient).
N/A
This topic teaches how to measure space complexity
Learn the core patterns in this topic. Each block explains when to use the pattern, the intuition behind it, and a compact code example.
fixed variables only.
let left=0, right=n-1; (two pointer)
storing n items.
const map = new Map(); // up to n entries
recursive binary search call depth.
binarySearch(arr, lo, hi)
recursive DFS on skewed tree.
treeHeight(root.left)
memoization uses O(n) space to reduce O(2ⁿ) time to O(n)
modify input array to avoid extra space.
reverse array with two pointers
space beyond the input itself; what interviewers usually mean by "space complexity"