JS Interview Questions
JS Interview Questions
3. LRU Cache
Build an LRU (Least Recently Used) cache class in JS.
class LRUCache {
constructor(capacity) {}
get(key) {}
put(key, value) {}
}
Constraints:
O(1) time for get and put.
Use a Map and Doubly Linked List.
4. Throttle / Debounce
Implement these two functions:
function debounce(fn, delay) {}
function throttle(fn, delay) {}
Bonus: Show usage in a real-world scroll or input event listener.
5. Chained Sum Function
sum(1)(2)(3)...(n) == total
Usage:
sum(1)(2)(3)(4)(); // 10
Hint: Use valueOf() or toString() overriding.
7. Memoization
Create a memoize(fn) that caches the output of pure functions.
const memoizedAdd = memoize((a, b) => a + b);
Bonus: Handle arguments of any type (objects, arrays, etc.).
9. Implement JSON.stringify
Custom polyfill for JSON.stringify(obj).
Handle:
Primitives
Arrays
Nested objects
undefined, functions, and symbols should be omitted like the real
JSON.stringify
🧠 Tips
Focus on readability and efficiency.
Always talk about time/space complexity.
Use ES6+ features confidently, but know how they work under the hood.
Would you like solutions, unit tests, or want to pick any one to do now together?