Loading section...
@lru_cache: Python's Magic Memoization Decorator
Concepts: pyLruCache, pyMemoization, pyClimbStairs
Python's @lru_cache (and its simpler sibling @functools.cache from Python 3.9+) is the single most powerful tool in your interview toolkit for DP problems. It wraps a recursive function and automatically caches its results. The first time you call fib(10), it computes and caches the result. Every subsequent call to fib(10) returns the cached value instantly. You write natural recursive code and get memoized DP for free. Fibonacci: From Exponential to Linear The transformation from fib_naive to fib is exactly two lines: the import and the decorator. The recursive logic is identical. This is why @lru_cache is so powerful in interviews. You write the intuitive recursive solution, slap on the decorator, and the exponential time complexity collapses to linear. The interviewer sees clean code wi