Skip to content

Expert · Interview preparation and career · topic 59 of 61 · 30 min

Classic coding questions

Explanation

Front-end and Node interviews often ask you to implement small utilities from scratch: debounce, throttle, deep clone, flatten, curry, once, groupBy, and your own Promise.all. Follow a method: clarify the requirements, write examples, code a simple version, test edge cases aloud, then improve.

A method for live coding

  1. Clarify: inputs, outputs, edge cases. "Should flatten handle any depth? Can the input contain null?"
  2. Write 2–3 examples as tests before coding.
  3. Say your plan in one or two sentences.
  4. Write the simplest correct version first.
  5. Walk through your examples line by line; fix bugs.
  6. Mention complexity and possible improvements.

Interviewers care as much about how you think and communicate as about the final code. Silence for ten minutes followed by perfect code is a weaker signal than steady, clear thinking aloud.

Deep clone (without structuredClone)

javascript
function deepClone(value) {
  if (value === null || typeof value !== 'object') return value;  // primitives
  if (Array.isArray(value)) return value.map(deepClone);
  const out = {};
  for (const [k, v] of Object.entries(value)) out[k] = deepClone(v);
  return out;
}
Follow-ups: Dates, Maps, circular references (use a WeakMap of already-copied objects).

Flatten to any depth

javascript
function flatten(arr) {
  const out = [];
  for (const x of arr) {
    if (Array.isArray(x)) out.push(...flatten(x));
    else out.push(x);
  }
  return out;
}
// built-in: arr.flat(Infinity)

Curry

javascript
function curry(fn) {
  return function curried(...args) {
    if (args.length >= fn.length) return fn(...args);
    return (...more) => curried(...args, ...more);
  };
}
const add3 = curry((a, b, c) => a + b + c);
console.log(add3(1)(2)(3), add3(1, 2)(3), add3(1, 2, 3)); // 6 6 6
fn.length is the number of declared parameters.

Your own Promise.all

javascript
function promiseAll(items) {
  return new Promise((resolve, reject) => {
    const results = [];
    let done = 0;
    if (items.length === 0) return resolve(results);
    items.forEach((item, i) => {
      Promise.resolve(item).then((value) => {
        results[i] = value;          // keep input order
        if (++done === items.length) resolve(results);
      }, reject);                    // first rejection rejects all
    });
  });
}

Key points the interviewer listens for: results in INPUT order, not finishing order; non-promise values are allowed (Promise.resolve wraps them); an empty array resolves immediately; the first rejection rejects.

Other favourites

QuestionCore idea
debounce(fn, ms)clearTimeout then setTimeout on every call (module 10)
throttle(fn, ms)Remember the last run time; skip calls inside the window
once(fn)A closure flag; cache the first result
memoize(fn)A Map from arguments to results
groupBy(list, keyFn)reduce into an object of arrays
EventEmitter (on, off, emit)A Map from event name to a list of handlers
Careful: Do not jump straight to clever one-liners. A readable loop that is obviously correct beats a reduce chain you cannot explain. And always handle the empty input: empty array, empty string, zero; it is the first edge case interviewers test.
Think of it like this: Live coding is like cooking in front of a judge on a TV show. They taste the dish, but they also watch whether you plan, keep your station clean and taste as you go. A calm cook who explains each step scores higher than a silent one, even with a similar dish.
Tip: Practise each utility three times: once looking at a solution, once from memory a day later, and once explaining it aloud to a friend or a rubber duck.

Need help?

Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.

Try:

Watch the lesson

Classic coding questions · 1/6

Classic coding questions

In this lesson you will write the utility functions interviewers ask for most.

Prefer a video by a teacher? Find free videos on “javascript interview implement debounce curry deep clone promise.all” on YouTube ↗

In real life: A machine-coding round

Some Indian product companies (especially in e-commerce and fintech) run a 60–90 minute "machine coding" round: build a small working feature such as an autocomplete with debounce or a mini event emitter, with clean code and a few tests. Utilities like the ones in this topic are exactly the building blocks.

javascript
class Emitter {
  #handlers = new Map();
  on(evt, fn) { (this.#handlers.get(evt) ?? this.#handlers.set(evt, []).get(evt)).push(fn); }
  emit(evt, ...args) { for (const fn of this.#handlers.get(evt) ?? []) fn(...args); }
}
const e = new Emitter();
e.on('paid', (amt) => console.log('Receipt for', amt));
e.emit('paid', 499); // Receipt for 499

Practice (0/4 done)

Task 1 · Code · Javascript

Write flatten(arr) that flattens nested arrays to any depth WITHOUT using arr.flat.

Task 2 · Code · Javascript

Write deepClone(value) for plain objects, arrays and primitives (no structuredClone, no JSON). The copy must share no objects with the original.

Task 3 · Code · Javascript

Write curry(fn) so that a curried function can be called with its arguments one at a time or several at once: curry((a, b, c) => a + b + c)(1)(2)(3) and (1, 2)(3) both give 6.

Task 4 · Code · Javascript

Write promiseAll(items) that behaves like Promise.all: results in input order, plain values allowed, [] resolves to [], and the first rejection rejects. Do not use Promise.all.

Quick quiz

Question 1

1.In a curry helper, what does fn.length give?

Question 2

2.Why must promiseAll store results by index instead of push?

Question 3

3.What is usually the FIRST edge case to test?

Question 4

4.What do interviewers value most during live coding?

Read more (free)

Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.

In a curry helper, what does fn.length give?

Answer: The number of declared parameters

Function.length is the count of declared parameters (before any default or rest).

Why must promiseAll store results by index instead of push?

Answer: Promises finish in any order, but results must match input order

Push would record finishing order.

What is usually the FIRST edge case to test?

Answer: Empty input

Empty arrays and strings break many first attempts.

What do interviewers value most during live coding?

Answer: Clear thinking aloud and testing your own code

Communication and correctness matter more than clever tricks.

Which language should you use in a coding interview?Related

Answer: The one you write most fluently and know the standard library of

Fluency saves time and avoids silly errors; most companies accept common languages such as Python, Java, C++ and JavaScript.

From Running the coding interview

What does .clone() on a String do?Related

Answer: Allocates new heap memory and copies all the bytes

clone makes a deep, independent copy.

From Copy and Clone

In a 90-minute machine-coding round, what should you do first?Related

Answer: Clarify the requirements and plan briefly

A few minutes of questions and planning avoid building the wrong thing. Then make it work, then make it nice.

From Interview preparation

In debounce, what does clearTimeout(timer) do on each call?Related

Answer: Cancels the previously scheduled call

Each new call cancels the waiting one and restarts the wait.

From Memoization, debounce and throttle

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

Next steps

  1. 2 more topics in “Interview preparation and career” before its exam: Data-structure questions in JavaScript, Projects, portfolio and jobs.
  2. Module 12 exam: Interview preparation · 12 questions · 25 min, without help. 5 topics of this module still to finish first.
  3. 61 topics left in the course before the final exam.

← Previous: Predict-the-output questions