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 photoId and returns that photo if it's in the cache, or null if it isn't
  • put, which takes a photoId 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:

PhotoCache cache = new PhotoCache(2); // room for 2 photos cache.put("red-velvet", redVelvetPhoto); cache.put("tiramisu", tiramisuPhoto); cache.get("red-velvet"); // returns redVelvetPhoto // the cache is full, so this evicts tiramisu, // the least recently used photo cache.put("lemon-drizzle", lemonDrizzlePhoto); cache.get("tiramisu"); // returns null cache.get("red-velvet"); // returns redVelvetPhoto

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 photoId 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 hash map operations and pointer updates, no matter how many photos are in the cache. (As with any hash map, those lookups take O(1) time on average.)

The cache holds at most n photos, and the linked list and the hash map 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.)

Java's LinkedHashMap is a hash map with a doubly linked list running through its entries. If you construct it with accessOrder set to true, it keeps its entries in order of use, and if you override its removeEldestEntry() method, it can evict the least recently used entry for you. 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

. . .