08 / 10

How do closures enable memoization

Closures can be used for memoization, a technique that stores the results of expensive function calls and returns the cached result when the same inputs occur again. This can significantly improve performance in recursive or repetitive calculations.
Difficulty: 5/10
Topics: closures, memoization, performance

Scenario Questions

0-2 years experience
  1. 1

    Write a tiny function that returns the nth Fibonacci number using a closure to memoize previous results. How does the closure keep the cache alive?

  2. 2

    If you call that memoized Fibonacci function twice with the same argument, what will be logged if you added a console.log inside the original recursive calculation?

  3. 3

    What would happen if you accidentally declared the cache variable outside the closure instead of inside it?

2-5 years experience
  1. 1

    In a React component you added a memoized helper that computes an expensive layout value. The component re‑renders more often than expected. How would you use a closure to keep the memoization across renders, and what pitfalls could break it?

  2. 2

    A teammate’s closure‑based memoizer is leaking memory because the cache never expires. How would you modify the closure to limit cache size, and what trade‑offs does that introduce?

  3. 3

    You notice the memoized API wrapper sometimes returns stale data after the underlying data changes. Explain why the closure might be the cause and how you’d fix it.

5-8 years experience
  1. 1

    Design a Node.js module that provides memoized database query results across multiple request handlers. Discuss how you’d structure the closure, handle cache invalidation, and ensure safety in a clustered environment.

  2. 2

    Under high load your closure‑based memoization layer shows increased latency due to cache contention. What architectural changes could you make to improve scalability?

  3. 3

    How would you instrument a closure memoizer to track hit/miss ratios and decide when to evict entries in production?

8+ years experience
  1. 1

    Our legacy monolith contains many ad‑hoc closure memoizers. We want to replace them with a centralized caching service. Outline a migration plan, including abstraction of the closure pattern, backward compatibility, and avoiding performance regressions.

  2. 2

    At the organization level, we need to choose between closure‑based memoization in JavaScript services and an external distributed cache like Redis. What factors drive that decision, and how would you evaluate latency, consistency, and developer ergonomics?

  3. 3

    Propose a policy for when engineers should use closure memoization versus other caching strategies across teams, considering maintainability, testability, and cross‑team impact.

Follow-up Questions

  • How would you verify that the cache is being hit versus recomputed?
  • What problems can arise if the closure’s cache grows without bound?
  • When might you choose a different caching strategy over a closure?