JavaScript
Coding challenges
Ten classic implementations with tests and hidden solutions.
Ten functions interviewers ask you to write. Each starts broken — make the tests green, then peek at one possible solution. All tests run in the page; nothing leaves your browser.
1. debounce
function debounce(fn, wait) {
return function () {
return fn.apply(this, arguments);
};
}2. throttle
function throttle(fn, limit) {
return function () {
return fn.apply(this, arguments);
};
}3. once
function once(fn) {
return function () {
return fn.apply(this, arguments);
};
}4. memoize
function memoize(fn) {
return function (key) {
return fn(key);
};
}5. deep clone
function deepClone(value) {
return { ...value };
}6. flatten
function flatten(input) {
return input;
}7. curry
function curry(fn) {
return fn;
}8. Promise.all polyfill
async function myAll(items) {
return [];
}9. bind polyfill
function myBind(fn, self) {
return fn;
}10. retry with backoff
async function retry(fn, tries) {
return fn();
}Key takeaways
- Closures carry the state: timers, caches, flags, counts.
- Failing tests first proves the exercise actually tests something.
- Compare your solution with the reveal — same behavior, different shape is fine.