Code Challenges
Solve problems, earn XP, and level up your skills.
Deep Clone Object
Write a function `deepClone(obj)` that returns a deep copy of an object (nested objects should also be cloned, not referenced). Example: const a = { x: 1, y: { z: 2 } }; const b = deepClone(a); b.y.z = 99; // a.y.z should still be 2
Group By Property
Write a function `groupBy(arr, key)` that groups an array of objects by a given key. Example: groupBy([{type:'a',val:1},{type:'b',val:2},{type:'a',val:3}], 'type') → { a: [{type:'a',val:1},{type:'a',val:3}], b: [{type:'b',val:2}] }
Debounce Function
Write a function `debounce(fn, delay)` that returns a debounced version of `fn` — the function only executes after `delay` milliseconds have passed without it being called again. This is a classic interview question. Describe how debounce works and implement it. Hint: Use `setTimeout` and `clearTimeout`.
Memoize Function
Write a function `memoize(fn)` that returns a memoized version of `fn`. A memoized function caches its results — if called again with the same arguments, it returns the cached result instead of recomputing. Example: const slow = (n) => { /* expensive */ return n * 2; }; const fast = memoize(slow); fast(5); // computes → 10 fast(5); // returns cached → 10 (no recompute)
Implement Promise.all
Implement a function `myPromiseAll(promises)` that mimics the behavior of `Promise.all`: - Resolves with an array of results when all promises resolve - Rejects immediately if any promise rejects Do NOT use `Promise.all` in your implementation.
Binary Search
Write a function `binarySearch(arr, target)` that searches a sorted array for `target` and returns its index, or -1 if not found. You must use the binary search algorithm (O(log n)), not linear search. Example: binarySearch([1,3,5,7,9], 5) → 2 binarySearch([1,3,5,7,9], 4) → -1 binarySearch([2,4,6,8,10], 10) → 4