Explorer
JavaScript

Implement a `createLRUCache(capacity)` that supports `get(key)` and `put(key, value)`. When over capacity, evict the least recently used item.

Problem Statement

<p>Implement a <code>createLRUCache(capacity)</code> that supports <code>get(key)</code> and <code>put(key, value)</code>. When over capacity, evict the least recently used item.</p>

Examples

Input: const cache = createLRUCache(2); cache.put("a", 1); cache.put("b", 2); cache.put("c", 3); cache.get("a");

Output: null (a was evicted as least recently used)

Explanation: Evicts least recently accessed entry on capacity overflow.

Complexity

Time Complexity: O(N)

Space Complexity: O(1)

Hints

šŸ’” Hint 1: Use a Map which preserves insertion order in JavaScript. šŸ’” Hint 2: On get, delete the key and re-insert it to move it to the end (most recent). šŸ’” Hint 3: On put, if capacity exceeded, delete the first key (least recently used) using map.keys().next().value.

Editorial & Approach

Problem Overview & Intuition

To solve LRU Cache, we consider the execution characteristics of JavaScript engines. Implement a `createLRUCache(capacity)` that supports `get(key)` and `put(key, value)`. When over capacity, evict the least recently used item. By utilizing idiomatic language constructs and clean algorithmic principles, we can accomplish this with optimal time and memory usage.

Step-by-Step Approach

  1. Understand Problem Contract: Identify input arguments, return type expectations, and edge cases (empty inputs, nullish values).
  2. Choose Core Mechanism: Use modern JavaScript patterns (use a map which preserves insertion order in javascript).
  3. Implement Logic: Handle state and transformations efficiently (on get, delete the key and re-insert it to move it to the end (most recent)).
  4. Return Result: Ensure proper return format and preserve caller context if applicable.

Optimal Implementation (JavaScript)

function createLRUCache(capacity) {
  const cache = new Map();
  return {
    get(key) {
      if (!cache.has(key)) return -1;
      const val = cache.get(key);
      cache.delete(key);
      cache.set(key, val);
      return val;
    },
    put(key, value) {
      if (cache.has(key)) cache.delete(key);
      cache.set(key, value);
      if (cache.size > capacity) {
        cache.delete(cache.keys().next().value);
      }
    }
  };
}

Complexity Analysis

Time Complexity O(N) linear scan over input
Space Complexity O(1) constant auxiliary space (or O(N) output)

Edge Cases & Corner Traps Handled

  • Empty or boundary inputs (empty arrays, strings, zero length).
  • Type checks and unexpected values (e.g. null, undefined, negative numbers).
  • Closure preservation and memory isolation between separate invocations.

LRU Cache

Hard

Implement a createLRUCache(capacity) that supports get(key) and put(key, value). When over capacity, evict the least recently used item.

Example Scenarios
1Example 1
Input: const cache = createLRUCache(2); cache.put("a", 1); cache.put("b", 2); cache.put("c", 3); cache.get("a");
Output: null (a was evicted as least recently used)
Explanation:

Evicts least recently accessed entry on capacity overflow.

Editor
Loading Editor...
Evaluate code
Output:Click "Run" above to execute and verify your code here.
[1,-1]