Question presented to candidate: "What is memoization, when does it actually help, and can you show it genuinely speeding up a repeated call?"
What a strong answer should cover:
- Memoization caches a function's return value, keyed by its input arguments, so a repeated call with the SAME arguments returns instantly instead of recomputing.
- It only produces correct results for pure functions — ones whose output depends only on their arguments, with no reliance on external mutable state and no side effects to skip.
- It is a classic time-for-memory tradeoff: faster repeated calls, at the cost of holding cached results in memory for as long as the cache lives.
- The cache key for multi-argument functions typically needs a resolver (e.g. JSON.stringify-ing the argument list, or a custom key function) — a plain single argument can often be used as the key directly.
- Real verification: proving with a real timing measurement that a cached call is genuinely faster, not just assuming a Map lookup is fast.
Clarifying questions expected:
- "Should the cache ever be cleared or bounded in size, or is an unbounded cache acceptable for this exercise?" (a strong candidate flags unbounded-cache memory growth as a real production concern)
Code / implementation expected: Yes — a short, genuinely runnable memoize implementation with a real, measured before/after timing comparison.