Expert · Interview preparation and career · topic 59 of 61 · 30 min
Classic coding questions
Explanation
A method for live coding
- Clarify: inputs, outputs, edge cases. "Should flatten handle any depth? Can the input contain null?"
- Write 2–3 examples as tests before coding.
- Say your plan in one or two sentences.
- Write the simplest correct version first.
- Walk through your examples line by line; fix bugs.
- 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)
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;
}Flatten to any depth
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
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 6Your own Promise.all
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
| Question | Core 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 |
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
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.
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 499Practice (0/4 done)
Write flatten(arr) that flattens nested arrays to any depth WITHOUT using arr.flat.
Write deepClone(value) for plain objects, arrays and primitives (no structuredClone, no JSON). The copy must share no objects with the original.
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.
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
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Data-structure questions in JavaScriptInterview preparation and career · same module
- Core concept questionsInterview preparation and career · same module
- Running the coding interviewData Structures and Algorithms (DSA) for interviews · Interview strategy and mock problems
- Copy and CloneRust Programming · Ownership: Rust's big idea
- Interview preparationFrontend Web Development · Build, deploy and get hired
- Full-stack interviews in IndiaFull-Stack Development · Capstone, portfolio and getting work
Questions people ask about this
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.
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.
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.
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
- 2 more topics in “Interview preparation and career” before its exam: Data-structure questions in JavaScript, Projects, portfolio and jobs.
- Module 12 exam: Interview preparation · 12 questions · 25 min, without help. 5 topics of this module still to finish first.
- 61 topics left in the course before the final exam.