But it doesn't have to end here! Sign up for the 7-day coding interview crash course and you'll get a free Interview Cake problem every week.
You're in!
Your bakery's app has a gallery of cake photos, and customers say it feels sluggish.
Every time someone taps a cake, the app downloads its full-size photo from your server. That's slow, and people tend to flip back and forth between the same few cakes ("the chocolate one... no wait, the lemon one... okay, back to the chocolate one"). So you want to keep recently viewed photos in memory. But phones don't have much memory to spare, so the cache can only hold a fixed number of photos.
Write a class PhotoCache with:
When the cache is full and a new photo comes in, evict the photo that was used least recently to make room. Both get and put count as using a photo.
get and put should both run in O(1) time.
For example:
Photo IDs are strings like 'red-velvet'. Treat each photo as an opaque value: you never need to look inside it (in our tests, photos are just strings). The capacity will always be at least 1.
This problem is usually called LRU Cache. LRU stands for "least recently used."
Don't forget that get counts as using a photo. If get just looks a photo up without updating its place in line, you'll evict the wrong photo later.
What if put gets a photo_id that's already in the cache? Store the new photo, but don't add a second copy. And since the number of photos isn't changing, nothing should get evicted.
Make sure your code works when the capacity is 1. Every new photo should push out the one before it.
We can get O(1) time for both get and put, evictions included.
Log in or sign up with one click to get immediate access to free mock interview questions
We'll never post on your wall or message your friends.
Actually, we don't support password-based login. Never have. Just the OAuth methods above. Why?
Log in or sign up with one click to get immediate access to free mock interview questions
We'll never post on your wall or message your friends.
Actually, we don't support password-based login. Never have. Just the OAuth methods above. Why?
O(1) time for both get and put, and O(n) space, where n is the cache's capacity.
Each call does a fixed number of dictionary operations and pointer updates, no matter how many photos are in the cache. (As with any dictionary, those lookups take O(1) time on average.)
The cache holds at most n photos, and the linked list and the dictionary each have one entry per photo, so the space is O(n).
Suppose photos come in different sizes, and instead of a maximum number of photos, the cache gets a budget of bytes. How would you change your solution? (Careful: one big new photo might push out several small ones.)
Python's collections.OrderedDict remembers the order its keys were added in. Popping a key and adding it back moves it to the end, and popitem(last=False) removes the oldest key, so it can do most of an LRU cache's job. Try rewriting your solution with it. Why might an interviewer still ask you to build the linked list yourself?
Your bakery sometimes reshoots its cakes, so a cached photo can go stale. How would you add an expiration time, so get never returns a photo that was cached more than 10 minutes ago?
Log in or sign up with one click to get immediate access to free mock interview questions
We'll never post on your wall or message your friends.
Actually, we don't support password-based login. Never have. Just the OAuth methods above. Why?
Reset editor