Skip to the editor
JSGroundwork
JSGroundwork handwritten · web dev
←Back to Design problems

Ready to read

JSJavaScript⑂Git◎Interview prepΣDSA in JSSDSystem Design
More topics15›
</>HTML{ }CSS⚛ReactNNext.jsNeNest.jsTSTypeScriptNoNode.js🐳DockerDBSQL & Databases✓Testing🔒Web Security☁Cloud & DevOps◈GraphQL◆Redis☸Kubernetes
☰‹›
Description
All topics›JavaScript›Design problems

LRU Cache

advancedlayer A9 · Design problems5 tests

Design a cache with a fixed capacity that evicts the least recently used entry when it overflows.

  • new LRUCache(capacity) — capacity is a positive integer
  • get(key) — return the value, or -1 if absent. A successful get counts as a use and makes that key the most recently used
  • put(key, value) — insert or overwrite; this also counts as a use. If the cache is over capacity afterwards, evict the least recently used key
  • Both operations must be O(1), so no scanning for the oldest entry

Stuck?

All exercisesPlayground
1234567891011121314
loading the editor…

Nothing yet — hit Run and whatever you log shows up here.

Submit to see how you did.

⌘/Ctrl + Enter run⌘/Ctrl + S save⌘/Ctrl + / comment⌘/Ctrl + F find/replace⌘/Ctrl + D select next matchAlt + click multi-cursorTab indent · ⇧Tab outdentEsc leave fullscreen