call(back)

How Do You Approach a JavaScript Coding Question?

You are handed a JavaScript coding question — implement debounce, say, or Promise.all, or cloneDeep — with thirty-five minutes on the clock. Walk through how you run the round, from the first thing you ask to the last thing you say.

Talk for about three minutes: what you clarify before coding, how you decide what to handle and what to only mention, and the mistake you watch for in each of the common question families.

Hints

Hint 1

The questions worth asking first are the ones that change the code: which runtime, which syntax, leading or trailing edge, cycles or not, how big the input is.

Hint 2

Each classic has one bug interviewers see every week: a debounce that never clears the previous timer, a Promise.all that pushes in completion order, a clone through JSON, a var loop with setTimeout.

Solution

Show the approach

The shape of the round

  • Clarify first. Browser or Node, which syntax is allowed, whether you can run the code. Then the question's own switches: leading or trailing edge, fixed or variadic arity, cycles or not, how big the input is, whether the callback gets a this. Each answer changes the code, so ask before writing.
  • Say the spec, build the core. Name the edge cases you know about — sparse arrays, thisArg, mutation during iteration, thenables, cycles — and say which you will handle. Mentioning them earns most of the credit; skipping them silently earns none.
  • Propose, then code aloud. These questions rarely need a clever algorithm, so go straight to the right data structure and keep talking while you type.
  • Read it back, then test. One pass for typos and variables used before they exist, then three cases: the example, the empty input, the nasty one.
  • Close with trade-offs: what you left out, what you would change with more time, and the complexity if it matters.

The pitfall in each family

Debounce and throttle. Forgetting to clear the previous timer turns a debounce into a throttle. Calling fn() instead of fn.apply(this, args) loses both the receiver and the arguments. Ask about edges, and about cancel. → Implement debounce(), Implement Throttle

Promise combinators. Write results by index, never push on completion. Resolve [] for an empty input instead of hanging. Promise.resolve(item) normalises plain values. all fails fast, allSettled never rejects, any rejects only once everything has, with an AggregateError. → Implement Promise.all, Implement Promise.allSettled, Implement Promise.any

Deep clone. JSON.parse(JSON.stringify(x)) drops undefined, turns a Date into a string and NaN into null, and throws on cycles. Carry a WeakMap of visited objects, keep prototypes, and recurse into Map and Set values too. → Implement cloneDeep

Closures and this. var in a loop with setTimeout prints the final value every time; let or a closure fixes it. this inside a callback is not the outer object. Classes are hoisted, but sit in the temporal dead zone until their line runs. → Predict the Output: var and let in a Loop With Timers, Implement Function.prototype.bind

Polyfills. map passes value, index and array, honours thisArg, keeps holes, and fixes its range before the first callback. → Implement Array.prototype.map

The DOM. Cache a querySelectorAll result instead of querying inside a loop. Replacing innerHTML drops every listener attached beneath it. getElementsByTagName returns a live collection; querySelectorAll a static one. → Implement getElementsByTagName

Worth saying out loud

  • Ask whether you may assume a queue or a heap exists; interviewers usually let you.
  • Prefer pure functions, never mutate the input, and say a word about callbacks that mutate the array you are iterating.
  • When recursion is the natural shape, ask about depth and cycles before writing it.

Assistant

Sign in to use the assistant — ask what the problem means, get a hint when you're stuck, or have your current work reviewed. Conversations save to your account.

Similar Problems6