/Interview

AI interview rounds · Lesson 3 of 8 · 15 min

The code comprehension round

Study with AI

Part 1Basics

Real work mostly means changing code someone else wrote. A round built around that makes sense, and it suits the AI era too, since reading code now matters more than typing it. Google's reported pilot is this kind of round.

The trap is to start changing things straight away. First get a picture: what's this meant to do, what are the main pieces, and what does it promise? A bug is simply a gap between a promise and what happens.

Words you need

LRU
Least recently used. When a cache is full, it removes the item unused for the longest.
TTL
Time to live. How long an item stays valid.
Invariant
Something that must always be true, such as: size never exceeds max.

A worked example

Meera's practice file is a cache of about 30 lines that claims to be 'LRU with expiry'. No tests fail. She writes down the two promises: recently used items survive, and expired items disappear. Then she writes one small test per promise, and two of them fail right away. Nothing crashes, so nothing would have told her otherwise.

Part 2The question

How do you find what's wrong in code you've never seen, in under an hour?

By the end of this lesson, you should be able to:

  • Build a picture of unknown code quickly
  • Turn a claim into a test
  • Use AI to explain code, and check its explanation

Part 3In detail

Step by step

  1. Read the names and the shape first. Types, public methods and comments tell you the promises.
  2. Write the promises down. Each promise is something you can test.
  3. Trace one normal call by hand. Follow the data through it.
  4. Test each promise. Start with a small script. Each failure is a lead.
  5. Fix the cause. Then rerun everything.
  6. Ask AI to explain, then check. It's good at summaries, but it can describe what code should do rather than what it does.

Good questions for the assistant

Ask it to summarise a function, to list what a file assumes about its inputs, or to explain an unfamiliar API. Then verify it against the code. If it says a cache is LRU because the file's comment says so, you've learned nothing.

Prompts to copy

List the promises

Tell it to read the code, and not to trust the comments.

Read this code. Don't assume the comments are correct.
[code]
List: 1) what it promises to callers, 2) what it assumes about its inputs, 3) any state it changes. Then, for each promise, write one short test that would fail if the promise were broken. Don't fix anything.

The code, explained

This is real code, and our tests run it, including the buggy versions, to prove the bugs are real.

The file you're handed

Meant to be an LRU cache with expiry. Find three bugs before reading the notes. Nothing crashes, which is what makes them hard.

TtlCache.tsTypeScript
1/** Meant to be an LRU cache with expiry. It has three bugs, and no test here fails yet. */2export class TtlCacheInherited<V> {3  private items = new Map<string, Item<V>>();4  private max: number;5  private ttlMs: number;6  private now: Clock;7  constructor(max: number, ttlMs: number, now: Clock) {8    this.max = max;9    this.ttlMs = ttlMs;10    this.now = now;11  }12  get(key: string): V | undefined {13    const item = this.items.get(key);14    if (!item || item.expires <= this.now()) return undefined;15    return item.value;16  }17  set(key: string, value: V): void {18    if (this.items.size >= this.max) {19      const oldest = this.items.keys().next().value as string;20      this.items.delete(oldest);21    }22    this.items.set(key, { value, expires: this.now() + this.ttlMs });23  }24  get size(): number {25    return this.items.size;26  }27}
27 linesUTF-8TypeScript
item.expires <= this.now()
Returns undefined, but leaves the dead item in the map, where it keeps taking space.
return item.value
Doesn't mark the key as recently used, so this is really first-in, first-out.
this.items.size >= this.max
Also runs when overwriting an existing key, evicting an innocent key though the size wouldn't grow.

Tested by npm run test:modules.

The fixed version

All three fixes come from one fact: a Map remembers insertion order.

TtlCache.tsTypeScript
1/** Map keeps insertion order, so delete-then-set moves a key to the "most recent" end. */2export class TtlCache<V> {3  private items = new Map<string, Item<V>>();4  private max: number;5  private ttlMs: number;6  private now: Clock;7  constructor(max: number, ttlMs: number, now: Clock) {8    this.max = max;9    this.ttlMs = ttlMs;10    this.now = now;11  }12  get(key: string): V | undefined {13    const item = this.items.get(key);14    if (!item) return undefined;15    this.items.delete(key);16    if (item.expires <= this.now()) return undefined;17    this.items.set(key, item);18    return item.value;19  }20  set(key: string, value: V): void {21    this.items.delete(key);22    if (this.items.size >= this.max) {23      const oldest = this.items.keys().next().value as string;24      this.items.delete(oldest);25    }26    this.items.set(key, { value, expires: this.now() + this.ttlMs });27  }28  get size(): number {29    return this.items.size;30  }31}
31 linesUTF-8TypeScript
this.items.delete(key)
Delete then set moves a key to the newest position. It also handles expired items and overwrites.
this.items.keys().next().value
The first key in the Map is the least recently used one.

Tested by npm run test:modules.

What can go wrong

Rewriting it
You were asked to understand and improve it. A rewrite throws away what you could have shown, and burns the time.
Trusting the comments
The file says LRU. It isn't. Comments are claims, not facts.
Fixing the symptom
Three bugs here share one fix once you know how a Map orders its keys.

Part 4Questions and answers

Answer out loud first, then open each one.

What are the three bugs in the inherited cache?
Reading a key doesn't mark it as used, overwriting a key can evict a different one, and expired items stay and take up space.
Why are these hard to find?
None of them crash. The cache just returns misses at the wrong times, so you only find them by testing what it promises.
Why does delete-then-set make it LRU?
A Map iterates in insertion order. Reinserting moves a key to the end, so the first key is always the least recently used.

Part 5Going further

Drill: Find the three bugs (20 min)

  1. Copy the inherited cache without reading the notes.
  2. Write the promises.
  3. Write a test for each, and find the bugs.
  4. Fix them with as small a change as you can.

Keep the fundamentals fresh with the critical basics.

Who says so

Checked in November 2026. Vendors and individuals often report what candidates told them, so treat their details as reports.

Next: the fundamentals screen. The maths and vocabulary that every AI interview assumes.

Next: LLM screen →

← The AI-enabled coding round

AI Engineer · AI interview rounds