You only have free questions left (including this one).

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.

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:

  • a constructor that takes the cache's capacity: the most photos it can hold at once
  • get, which takes a photo_id and returns that photo if it's in the cache, or None if it isn't
  • put, which takes a photo_id and a photo and stores the photo in the cache (replacing the old photo if that ID is already cached)

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:

cache = PhotoCache(2) # Room for 2 photos cache.put('red-velvet', red_velvet_photo) cache.put('tiramisu', tiramisu_photo) cache.get('red-velvet') # Returns red_velvet_photo # The cache is full, so this evicts tiramisu, # the least recently used photo cache.put('lemon-drizzle', lemon_drizzle_photo) cache.get('tiramisu') # Returns None cache.get('red-velvet') # Returns red_velvet_photo

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.

Start your free trial!

Log in or sign up with one click to get immediate access to free mock interview questions

Where do I enter my password?

Actually, we don't support password-based login. Never have. Just the OAuth methods above. Why?

  1. It's easy and quick. No "reset password" flow. No password to forget.
  2. It lets us avoid storing passwords that hackers could access and use to try to log into our users' email or bank accounts.
  3. It makes it harder for one person to share a paid Interview Cake account with multiple people.

Start your free trial!

Log in or sign up with one click to get immediate access to free mock interview questions

Where do I enter my password?

Actually, we don't support password-based login. Never have. Just the OAuth methods above. Why?

  1. It's easy and quick. No "reset password" flow. No password to forget.
  2. It lets us avoid storing passwords that hackers could access and use to try to log into our users' email or bank accounts.
  3. It makes it harder for one person to share a paid Interview Cake account with multiple people.

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?

Start your free trial!

Log in or sign up with one click to get immediate access to free mock interview questions

Where do I enter my password?

Actually, we don't support password-based login. Never have. Just the OAuth methods above. Why?

  1. It's easy and quick. No "reset password" flow. No password to forget.
  2. It lets us avoid storing passwords that hackers could access and use to try to log into our users' email or bank accounts.
  3. It makes it harder for one person to share a paid Interview Cake account with multiple people.

Reset editor

. . .