Practice analyzing time and space complexity of code snippets and algorithms.
Reading code and quickly determining its complexity is a key interview skill. This section walks through worked examples — nested loops, recursion, and combined patterns — so you can confidently analyze any code you write or are asked about.
Practice topic — no single answer
Practice topic — no single answer
Learn the core patterns in this topic. Each block explains when to use the pattern, the intuition behind it, and a compact code example.
O(n).
for (let i=0; i<n; i++) → O(n)
O(n²).
for i: for j: → O(n²)
O(n)+O(n) = O(n).
loop1 then loop2 → still O(n)
O(log n).
while (n>1) { n=Math.floor(n/2); } → O(log n)
O(2ⁿ).
fib(n-1)+fib(n-2) → O(2ⁿ) without memo
O(log n) or O(n log n).
mergeSort splits then merges → O(n log n)
keep dominant term.
O(n²)+O(n) = O(n²)