Daily iOS
CS fundamentals and algorithms

LRU cache

You need to write an in-memory image cache with LRU eviction yourself. Which data structures give you O(1) get and put?

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

A Dictionary (key to node) plus a doubly linked list (recency order) gives O(1) get and put. NSCache's eviction policy is opaque and its cost limit is approximate, so when you need exact LRU and capacity control, you build your own.

Share a question