Draft saved on this device
▤ Description
Core C++Medium
Implement an LRU Cache in C++
Build a least-recently-used cache with O(1) average operations.
Implement a cache with capacity C. PUT key value inserts or updates a key and marks it most recently used. If full, evict the least recently used key. GET key prints its value, or -1 if absent; a successful GET marks the key most recently used. Design for average O(1) per operation.
Input format
First line: C Q. Next Q lines: PUT key value or GET key.
Output format
For each GET print the result on its own line.
Examples
Example 1
Input
2 6 PUT 1 10 PUT 2 20 GET 1 PUT 3 30 GET 2 GET 3
Expected output
10 -1 30
Constraints
- 1 ≤ C ≤ 100000
- 1 ≤ Q ≤ 100000
- Keys and values are signed 32-bit integers.
Related concepts
This early-access exercise uses standard input/output. More advanced concurrency challenges will use specialized class interfaces, instrumentation, and stress tests once the judging infrastructure is ready.
⌘ Code
Loading Monaco C++ editor…
Checking judge...
Checking whether the external C++ judge is available.
⌁ Test cases & resultsPublic samples: 1
Input
2 6 PUT 1 10 PUT 2 20 GET 1 PUT 3 30 GET 2 GET 3
Expected output
10 -1 30