learn
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

javascriptTab moves to the next control
function debounce(fn, wait) {
return function () {
  return fn.apply(this, arguments);
};
}

2. throttle

javascriptTab moves to the next control
function throttle(fn, limit) {
return function () {
  return fn.apply(this, arguments);
};
}

3. once

javascriptTab moves to the next control
function once(fn) {
return function () {
  return fn.apply(this, arguments);
};
}

4. memoize

javascriptTab moves to the next control
function memoize(fn) {
return function (key) {
  return fn(key);
};
}

5. deep clone

javascriptTab moves to the next control
function deepClone(value) {
return { ...value };
}

6. flatten

javascriptTab moves to the next control
function flatten(input) {
return input;
}

7. curry

javascriptTab moves to the next control
function curry(fn) {
return fn;
}

8. Promise.all polyfill

javascriptTab moves to the next control
async function myAll(items) {
return [];
}

9. bind polyfill

javascriptTab moves to the next control
function myBind(fn, self) {
return fn;
}

10. retry with backoff

javascriptTab moves to the next control
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.

References

On this page