← Back to problems

1. LRU Cache

EASY
CACHEDESIGNHASH MAPLLD

Implement a fixed-capacity Least-Recently-Used cache driven by a command stream.

Implement an LRU (Least Recently Used) cache with a fixed capacity, set once at startup. Process commands from stdin, one per line. Commands: - INIT <capacity> : always the first line; sets capacity (>= 1). - PUT <key> <value> : insert or update. Keys and values are integers. Updating an existing key refreshes its recency. - GET <key> : print the stored value, or -1 if the key is absent. A successful GET refreshes recency. - SIZE : print the current number of entries. When capacity is exceeded on a PUT of a NEW key, evict the least-recently-used entry. Both a PUT of a new key and a successful GET count as "uses" for recency. Updating an existing key via PUT counts as a use but never triggers eviction. A GET on an absent key does not count as a use and does not change any state.
Log in to submit a solution

Comments

Log into join the discussion.