Daily iOS
Persistence

Key-value store with expiry

You're given 15 minutes to design a key-value store whose values carry an expiry time. set(key, value, ttl) and get(key) must never hand back an expired value, and starting with UserDefaults is fine. How would you design it?

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

You store the value together with its expiry time, and get compares it against the current time, returns nil when it's expired, and deletes it on the spot.

Share a question