AlgoFrames

Predict evictions

Standard · Trace the algorithm · about 7 min

Follow an LRU cache with capacity 2 through these commands: put 1 1, put 2 2, get 1, put 3 3, get 2, put 4 4, get 1, get 3, get 4. Answer each question before the step plays.

Example 1

Input:

{"capacity":2,"commands":["put 1 1","put 2 2","get 1","put 3 3","get 2","put 4 4","get 1","get 3","get 4"]}

Output:

[1,-1,-1,3,4]

Constraints

  • The trace uses the commands shown.

Learn the idea first: LRU and LFU caches, a visual lesson.

More LRU and LFU caches problems