Not sure what to study next?

Get a personalised quant interview roadmap built around your upcoming interviews, strengths and weaknesses.

Takes a few minutes. Start with your target role and firms.

← Programming questions
Hard / PYTHON

LRU Cache with Capacity Changes

hash_maps · linked_lists · simulation 45 minute study target

Not completed

Challenge

Simulate a least-recently-used cache and return one value for each get operation. Operations are ["put", key, value], ["get", key], and ["resize", new_capacity]. A successful get and every put make that key most recent. A missing get returns null (None) and changes no recency. Put on an existing key updates its value. When over capacity, evict least-recent keys. Resize can evict several keys and otherwise preserves recency. At capacity zero, nothing is retained.

Input and output contract

0 ≤ initial and resized capacity ≤ 5,000; at most 15,000 operations, including at most 1,500 gets; integer keys and values with absolute value ≤ 1,000,000. All operation shapes are valid. Put and resize emit no result. c is maximum cache capacity; m is operation count.

All tests follow this contract. You do not need to validate malformed input. Return the requested result from lru_cache; printed output is not your answer. Standard-library tools are allowed. Implement your solution in Python 3.12.

Target complexity: O(m) expected total time; O(c) cache space plus returned get results

Return values are checked by type. Use the exact function name. Print output is not your answer. Tests have time and memory limits.

Public examples

Example 1

Arguments

[2, [['put', 1, 10], ['put', 2, 20], ['get', 1], ['put', 3, 30], ['get', 2], ['resize', 1], ['get', 1], ['get', 3]]]

Expected return

[10, None, None, 30]
Example 2

Arguments

[0, [['put', 1, 5], ['get', 1], ['resize', 1], ['put', 1, 9], ['get', 1]]]

Expected return

[None, 9]
Example 3

Arguments

[1, [['put', 1, 4], ['put', 1, 8], ['get', 1]]]

Expected return

[8]
Hint

A dictionary locates entries. A doubly linked list makes removing and moving a known entry constant time; head is least recent.

The worked solution unlocks after completion, or with Premium access.

lru_cache.pyPython 3.12

Drafts are saved to your account, not to shared browser storage.

Sign in to submit code and save progress.

Results

Your results will appear here.

Hidden tests reveal only their pass/fail status, never their inputs or expected outputs. Runtime is observed wall time—not an inferred complexity class.

Join our Newsletter and Receive the Latest Updates!

Main Links

Trade Courses Assessments Interview Prep Contact Us

Social Media

Facebook Instagram YouTube

Legal

Terms and Conditions Privacy Policy Privacy settings

Talent Acquisition

Looking to hire exceptional quant talent?
See how we can help.

© 2026 EverythingQuant. All Rights Reserved.