Theory
Understanding complexity is essential when comparing algorithms. O(1) is constant time, O(log n) grows slowly, O(n) grows linearly, O(n log n) commonly appears in efficient sorting, and O(n²) grows quadratically.
Example
// O(1)
console.log("Hello");
// O(n)
for (let i = 0; i < 5; i++) {
console.log(i);
}
Expected Output
The first operation is constant time and the loop grows with input size.
Practice
Identify the time complexity of five different code snippets.
Interview Questions
- What is O(1)?
- What is O(n)?
- What is O(log n)?
- Why is complexity analysis important?