Analyze time and auxiliary space complexity
Quick answer
Complexity describes how resource requirements grow with input size. Big O gives an asymptotic upper bound, not the exact running time; a tight bound is often written using Theta.
Working examples
Run as an independent script in Node.js v22.16.0. ByteMind editor support has not been verified.
const n = 4;let visits = 0;for (let i = 0; i < n; i++) { for (let j = 0; j < n; j++) visits++;}console.log(visits);Expected console output:
16There are four inner visits for each of four outer iterations.
Run as an independent script in Node.js v22.16.0. ByteMind editor support has not been verified.
let n = 16, steps = 0;while (n > 1) { n = Math.floor(n / 2); steps++;}console.log(steps, n);Expected console output:
4 1Four halvings reach one.
How it works and cost model
Choose a cost model first, such as comparisons of bounded integers. A single pass over n elements makes n visits (Theta(n)); all ordered pairs make n² visits. Halving a positive interval each iteration takes logarithmically many steps. Auxiliary space counts extra storage beyond input, with an explicit convention about returned output. An algorithm can use O(1) auxiliary space while taking O(n) time. JavaScript engine implementation, allocation, input shape and hardware affect measured performance; the language does not promise a specific Big O for all built-ins.
Common mistakes
Do not infer a tight bound from one stopwatch run, or label every pair of nested loops quadratic. Count actual loop bounds, including dependent inner loops.
Practice
For n = 1, 4 and 8, count visits in a single loop, an all-pairs loop, and a loop dividing n by two. State each growth bound and auxiliary-space assumption.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Analyze time and auxiliary space complexity — Knowledge Check
Reason about the algorithm or data structure, its invariants, trade-offs and deterministic outputs.
What does an asymptotic upper bound describe?
For an algorithm scanning n items and storing a single counter, what auxiliary space is used under a fixed-width number model?
Which observation alone cannot establish asymptotic complexity?
What is the exact console output in the Asymptotic complexity (Big O) example 1?
const n = 4;let visits = 0;for (let i = 0; i < n; i++) { for (let j = 0; j < n; j++) visits++;}console.log(visits);What is the exact console output in the Asymptotic complexity (Big O) example 2?
let n = 16, steps = 0;while (n > 1) { n = Math.floor(n / 2); steps++;}console.log(steps, n);