Coding Tasks
Coding tasks in an interview are rarely about clever math — almost always they are about whether you know the right technique and its cost in time and space. Dedup with a Set in O(n) instead of indexOf in a loop at O(n²); reverse a string remembering it is immutable; stop a recursion with a base case; build a fluent chain by returning this. These are not different problems but a handful of recurring patterns.
A separate class of questions is "what does this print" — there the check is not a technique but your grasp of coercion rules, event-loop ordering, and this binding. Each layer below dissects one technique or one class of traps, from arrays to the DOM event lifecycle.
Topic map
- Array algorithms — deduplication with
Set, O(1) membership, and whyindexOfin a loop turns the job into O(n²). - String algorithms — string immutability, reversing with
[...s].reverse().join(''), and the two-pointer trick for a palindrome in O(1) space. - Recursive algorithms — base case and recursive case, descending into structure of any depth, and stack overflow without a base case.
- Method chaining —
return thisfor a fluent API, a terminal accessor, and private state via closure. - Closure utilities — factories with private state,
debounce/throttle, and currying, where a closure holds the accumulated data. - Output prediction — how to reason through "what does this print" by the rules of coercion, event-loop order, and
thisbinding. - The XOR trick — the properties of bitwise XOR and how they yield an O(1)-space solution to the single-number problem.
- DOM event lifecycle — event-driven resource loading, attaching
onload/onerrorbeforesrc, and the cached-image trap.
Common traps
| Mistake | Consequence |
|---|---|
Deduplicating with indexOf in a loop | Hidden O(n²) instead of O(n) with a Set |
| Sorting to deduplicate | Original order of first appearance is destroyed |
Calling reverse on a string directly | Error — only arrays have it, strings are immutable |
| Recursion with no base case | Endless descent and a RangeError stack overflow |
Returning a value instead of this from a chainable method | The next call in the chain throws |
Forgetting clearTimeout in debounce | Every call fires, not just the last |
Reading img.complete right after src | Loading is event-driven, not synchronous — the result is not ready |
Expecting setTimeout before Promise.then | Microtasks drain fully before any macrotask |
Interview relevance
Coding tasks check whether you separate "works" from "works at the right complexity", and whether you command recurring techniques rather than solving each problem from scratch. A candidate who names the cost out loud ("a Set here is O(n) instead of indexOf's O(n²)") immediately looks stronger than one who just wrote the first loop that came to mind.
Typical checks:
- Whether you know the base techniques —
Setfor uniqueness, two pointers, recursion,return thisfor chains. - Whether you state time and space complexity and can improve it.
- Whether you reason through "what does this print" from the language rules rather than guessing.
- Whether you understand that strings are immutable and resource loading is event-driven.
Common wrong answer: "unique values means sort and drop adjacent duplicates". Sorting costs O(n log n) and destroys the original order; a Set solves the same task in O(n) preserving the order of first appearance.