FrontendInterviews.dev

Loading problem…

46. Advanced Memoization

Hard•
Acceptance: 92.86%
•
🔓3/3 Pro unlocks today

This problem builds on memoization-ii. Complete that first, then load your solution to continue.

Implement an advanced memoize function with TTL (Time-To-Live), custom cache key generation, cache statistics, cache warming, and multi-level caching.

Requirements

1. Advanced Features

  • TTL: Cache entries expire after specified time
  • Custom Keys: Support custom cache key generation function
  • Cache Statistics: Track hits, misses, size, etc.
  • Cache Warming: Pre-populate cache with values
  • Multi-level Caching: Support multiple cache levels

Example Usage

// Example: TTL cache
const memoized = memoize(fn, { ttl: 1000 }); // 1 second TTL

// Example: Custom key generator
const memoized2 = memoize(fn, {
  keyGenerator: (args) => args[0].id
});

// Example: Cache statistics
const memoized3 = memoize(fn, { enableStats: true });
memoized3.getStats(); // { hits: 5, misses: 2, size: 3 }

Constraints

  • Support TTL for cache expiration
  • Support custom cache key generation
  • Track cache statistics
  • Support cache warming
  • Handle expired entries