Simple LRU Cache Simulation
PYTHON coding challenge · Difficulty: easy · Topic: LRU Cache · +50 XP
Implement a Least Recently Used (LRU) cache with a fixed capacity and replay a list of operations.
Each query is a tuple (op, key, value):
("put", key, value)storesvalueunderkey. If the key already exists its value is replaced. If storing a NEW key makes the cache hold more thancapacitykeys, evict the least recently used key.("get", key, 0)returns the value stored underkey, or-1if the key is not in the cache. The third field is ignored.
Both a successful get and a put (insert or update) make that key the most recently used. Return a list with the result of every get, in order.
Example Input
`
capacity = 2
queries = [("put", 1, 10), ("put", 2, 20), ("get", 1, 0), ("put", 3, 30),
("get", 2, 0), ("get", 3, 0), ("put", 1, 15), ("get", 1, 0)]
`
Expected Output
`
[10, -1, 30, 15]
`
get 1 makes key 1 recently used, so put 3 evicts key 2. put 1 then updates key 1 to 15.
What this PYTHON challenge teaches you
“Simple LRU Cache Simulation” is a easy-level PYTHON challenge focused on LRU Cache. Working through it gives you hands-on practice with OrderedDict, hash map, caching, simulation — the kind of transformation you are asked to write in real data engineering work and in technical interviews. You can solve it directly in the browser: the dataset is pre-loaded, so you write the query or DataFrame code, run it, and compare your output against the expected result immediately.
Concepts covered
- OrderedDict
- hash map
- caching
- simulation
How to approach it
If you get stuck, work through these steps in order before looking at a full solution — each one narrows the problem down:
- You need fast lookup AND to know which key was used least recently.
- collections.OrderedDict keeps keys in order: move_to_end(key) marks a key as most recently used.
- On every successful get and every put, move the key to the end.
- After inserting a new key, if len(cache) > capacity, evict the oldest key with popitem(last=False).
- A get of a missing key appends -1 and changes nothing.
How to practise it on PySpark.in
Open the challenge, write your Python code in the editor and press Run to execute it against the sample dataset. Submitting checks your output against every test case, including hidden ones, so you find out straight away whether your logic holds up. You can retry as often as you like, and each solved challenge adds to your XP.
Related PYTHON challenges
- Word Frequency Counter
- Validate Unique User IDs
- Extract Error Codes from Log Strings
- Sum of Values by Key
- Detect Anomalies in Transaction Batches
- Flatten Nested JSON Structure
Helpful resources
Frequently asked questions
Do I need to install Spark or a database to solve this?
No. The PYTHON environment runs in your browser with the sample data already loaded, so there is nothing to install or configure.
Is this challenge free?
Yes - the problem, the sample dataset, the hints and unlimited test runs are free.
What level is it?
It is rated easy and covers LRU Cache.