JavaScript foundations for coding rounds · core
Big-O in JavaScript
Big-O says how time or memory grows when the input size n grows, keeping only the biggest term. In JavaScript, many built-ins hide a loop (includes, indexOf, shift, splice, slice, spread), so count them when you state the cost.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- Drop constants and small terms: O(3n + 20) is O(n); O(n² + n) is O(n²). Two different inputs stay separate: O(n + m) or O(n·m).
- JavaScript costs to remember: push/pop O(1) amortised; shift/unshift/splice O(n); includes/indexOf O(n); Map/Set get, set, has, delete O(1) on average; sort O(n log n); slice and spread copy O(k).
- A hidden loop counts: arr.includes() inside a for loop is O(n·m), not O(n). Replacing it with a Set makes it O(n + m).
- Space counts too: a new array or Map of size n is O(n) extra; recursion uses O(depth) call-stack memory.
- Rough limits for a 1–2 second answer: n ≤ 10^3 allows O(n²); n ≤ 10^5–10^6 needs O(n log n) or O(n).
- Say the complexity before coding ("brute force is O(n²); with a Map it becomes O(n)") and again after, including space. Interviewers expect both.
The red pen
Common traps: mistake → fix
Mistake: array.shift() is O(1)
Fix: it moves every element, so it is O(n); a BFS that shifts a big array becomes O(n²). Use a head index.
Mistake: A loop is O(n) because there is one for statement
Fix: check the body: includes, indexOf, slice and spread inside it are loops too.
Mistake: Recursion is free in memory
Fix: each call is a stack frame, so depth n means O(n) space and a possible RangeError.
Mistake: O(log n) means 'a bit faster'
Fix: it means the problem size is divided (usually halved) each step: 1,000,000 items need only about 20 steps.
Easy to hard
Questions interviewers ask, easy to hard
- What is the time and space complexity of your solution?
- Why is checking arr.includes(x) inside a loop slow? How do you fix it?
- What does amortised O(1) mean for array push?
- Your input grows from 1,000 to 1,000,000 items. Which of your two solutions still works and why?
Practise
Practise with these questions
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.