Back to the 100
Problem 100Design
Hard

LFU Cache

100

Simulate an LFU cache. Evict the least frequently used key, breaking ties by least recent use. Return outputs for get operations.

EXAMPLES

Example 1
Input
{
  "capacity": 2,
  "operations": [
    "put",
    "put",
    "get",
    "put",
    "get",
    "get",
    "put",
    "get",
    "get",
    "get"
  ],
  "values": [
    [
      1,
      1
    ],
    [
      2,
      2
    ],
    [
      1
    ],
    [
      3,
      3
    ],
    [
      2
    ],
    [
      3
    ],
    [
      4,
      4
    ],
    [
      1
    ],
    [
      3
    ],
    [
      4
    ]
  ]
}

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

FUNCTION SHAPE

capacity: intoperations: stringArrayvalues: intMatrixintArray
00:00
1 local tests readyRun with ⌘/Ctrl + Enter. Your code stays in this browser.

Runs solve(...) locally in a browser worker. SWE Playbook does not submit your code. Only run code you trust; Python code may access the network.