The Code Works. Does the Candidate Understand It? Five Follow-Up Questions for a Coding Interview
By CheatProof.ai Team · Published · Updated
You ask a candidate how their cache works. “I used a dictionary,” they say, “so looking up a key is fast.”
You were hoping to hear how they handle expiration. Maybe the solution came together quickly enough that you’re wondering if they had help. Either way, the next few minutes are easier if you give them something specific to reason through.
Here’s the cache they’ve written. Entries last five minutes by default. The caller supplies the current time in seconds, so you can test what happens six minutes later without waiting around.
class Cache:
def __init__(self, ttl=300):
self.ttl = ttl
self.entries = {}
def put(self, key, value, now):
self.entries[key] = (value, now + self.ttl)
def get(self, key, now):
entry = self.entries.get(key)
if entry is None:
return None
value, expires_at = entry
if now >= expires_at:
del self.entries[key]
return None
return value1. “I put a value in at 10:00 and read it at 10:04. What do I get if I read it again at 10:06?”
Let them make a prediction. They might say, “I’d still get the value. I read it two minutes ago.” Give them a moment with the code before jumping in. They may spot it themselves: “Oh, wait. Where do I update the expiry? I don’t. I just return the value.”
The write at 10:00 set the deadline to 10:05. That deadline was still in the dictionary after the 10:04 read, so the 10:06 read deletes the entry and returns None.
2. “Same key, but this time I write 'Paris' at 10:00 and overwrite it with 'Berlin' at 10:03. What comes back at 10:05?”
'Berlin'. The second put replaces both parts of the entry. The dictionary now holds the new city and a deadline of 10:08.
If the candidate mentions 10:08, see whether they include that instant in the lifetime of the entry. You can settle it together by looking at now >= expires_at. A read at exactly 10:08 returns None.
3. “Could you change it so the five minutes start over every time I read a value that’s still there?”
Let the candidate make the edit. One line just before the final return value is enough.
self.entries[key] = (value, now + self.ttl)The phrase “still there” is worth talking through. An expired entry can still be sitting in the dictionary. The code needs to check its old deadline before granting another five minutes. Otherwise, someone looking up 'Paris' an hour late could bring it back just by asking for it.
4. “How would you write a test that passes now but would have failed before your change?”
Give them room to choose the times. One possible test looks like this:
cache = Cache(ttl=300)
cache.put("city", "Berlin", now=0)
assert cache.get("city", now=240) == "Berlin"
assert cache.get("city", now=360) == "Berlin"At 240, both versions return 'Berlin'. At 360, the original version has passed its deadline of 300. In the updated version, the earlier read moved the deadline to 540, so 'Berlin' is still available.
There’s a good follow-up if the candidate picks 299 for that last read. Ask them to run the test against the original code. It still passes. Let them decide which number to change and why.
5. “We write a million different keys overnight. Nobody reads them, and by morning they’ve all expired. How many entries are sitting in the dictionary?”
Still a million, assuming the process stayed up. The only line that deletes an entry is inside get. Those keys could sit there for weeks.
At this point I’d ask whether they’d change anything before shipping it. A cache for ten fixed settings might be fine as written. A service that gets a fresh key with every request would keep consuming memory. A candidate who asks which kind of service you’re building has given you a good reason to spend the last few minutes on that trade-off.
After a conversation like this, you can write a concrete interview note: “Expected the first read to extend the deadline, then spotted the mistake. Added sliding expiration and picked a test case that caught the difference.” You’ve recorded what the candidate could do and where they needed a prompt.