#460

LFU Cache

HardDesignHas vizHash TableLinked ListDesign

Problem

LFU Cache

You must solve the interview problem titled "LFU Cache".

Difficulty: Hard. Topics: Hash Table, Linked List, Design. Primary pattern: Design.

Read the title as the specification: implement the exact behavior it names, return the correct output type, and aim for an interview-optimal Design solution. Use the examples to lock input/output format, then confirm edge cases (empty, single element, duplicates, extremes).

Example 1

Input: sample input for LFU Cache

Output: sample output for LFU Cache

Apply a Design approach and match the required return type.

Example 2

Input: edge-case input for LFU Cache

Output: edge-case output for LFU Cache

Verify empty/single/boundary behavior.

Constraints

  • Interview-scale limits for a Hard Design problem.
  • Handle empty and single-element inputs when they are valid.
  • Prefer the optimal asymptotic complexity expected in interviews.
Loading visualization…

Explanation

LFU Cache is a Hard interview problem tagged [Hash Table, Linked List, Design]. The winning approach is typically **Design**.

Intuition

Each structure covers a weakness of the other (lookup vs order).

Approach

Combine primitives (hash map + doubly linked list, two heaps, etc.) to hit the required complexities.

  1. List operations + target Big-O.
  2. Choose fields.
  3. Implement smallest correct version.
  4. Verify edge cases (empty, capacity 1).

Solution

Pattern-level skeleton only — not this problem’s exact answer. Adapt it to the statement.

Pattern templateDesign

Edge cases

  • •Empty or single-element input
  • •Duplicates and ties
  • •Maximum constraints / overflow
  • •Disconnected or unreachable states (for graphs)

Complexity

Time: O(n log n) – O(n²)

Space: O(n)

Before coding, restate the problem, name the pattern (Design), and write the invariant or DP state in one sentence.

Learn the topic

Got it?

Mark #460 LFU Cache as understood when the pattern clicks.