FrontendInterviews.dev

Loading problem…

1. Memoization

Easy•
Acceptance: 35.15%
•
🔓3/3 Pro unlocks today

Implement a memoize function that caches the results of function calls to improve performance.

Requirements

1. Basic Functionality

  • Accept a function fn
  • Return a memoized version of the function
  • Cache function results based on arguments
  • Return cached result if arguments match
  • Call original function if not cached

Example Usage

// Example: Expensive computation
const expensiveFn = (n) => {
  console.log('Computing...');
  return n * 2;
};

const memoized = memoize(expensiveFn);

memoized(5); // Logs: Computing..., returns 10
memoized(5); // Returns 10 (from cache, no log)
memoized(6); // Logs: Computing..., returns 12

Constraints

  • Cache function results based on arguments
  • Return cached result if arguments match
  • Call original function if not cached
  • Handle multiple arguments
  • Preserve 'this' context