Daily iOS
CS fundamentals and algorithms

O(1) LRU cache

You're designing an LRU cache that keeps only recently used items. To make get and put average O(1), what does the HashMap do and what does the doubly linked list do?

How would an interviewer hear this answer? You'll see whether they'd move on, dig in, or say no, and which sentences would trip you up. Your score doesn't change.
See the short answer

The [Key: Node] Dictionary is only the index that jumps from a key to its list node in O(1); the doubly linked list keeps the recency order, head most recent and tail oldest, so a node can be unlinked and moved to the front or removed from the end in O(1).

Share a question