Study notes · 10% of the exam

Coding Challenges and Patterns

Frontend rounds often ask you to build small utilities from scratch, such as debounce, Promise.all, deepClone or an event emitter, and then probe the edge cases. Know the contract of each, the bugs reviewers look for, and the cost of each approach.

Key points

  1. 1

    Debounce waits for a pause and runs once with the latest arguments; throttle runs at most once per window. Both must forward this and arguments: return a regular function and call fn.apply(this, args).

  2. 2

    Promise.all must keep input order (out[i] = v plus a separate counter), resolve [] immediately, wrap plain values with Promise.resolve, and accept any iterable (Array.from(items)).

  3. 3

    Memoize keys matter: plain objects stringify to "[object Object]" and JSON.stringify merges undefined, null and NaN. Use a Map or WeakMap per argument, or a deliberate key function.

  4. 4

    Deep clone with cycles: register the clone in a WeakMap before recursing. Copying own keys loses Date, Map, Set and prototypes; structuredClone handles cycles and built-ins but rejects functions.

  5. 5

    An event emitter iterates over a copy of its listener list, and once removes its wrapper before calling the listener while remembering the original for off.

  6. 6

    In a Map-based LRU cache, both get and put must delete and re-insert the key; the least recently used key is map.keys().next().value.

  7. 7

    Polyfills follow the spec: bind must support new, reduce skips holes and throws on an empty array with no initial value, and instanceof returns false for primitives.

Common traps

  • return fn() inside try does not catch an async rejection; use return await fn().

  • A concurrency pool that never removes settled promises from its race array stops limiting after the first completion.

  • Curry by fn.length breaks with default or rest parameters, because they lower the reported arity.

Test yourself on Coding Challenges and Patterns

Ten questions, with the answer and explanation after each one.