Find which of an agent's file reads went stale

Agent buildMedium25 min

Your task

A coding agent keeps the file excerpts it has read in its context while it edits. After an edit, some of those excerpts no longer match the file, and an edit planned from one of them can land on the wrong lines. Before each new edit, the agent checks which of its reads are out of date.

Write stale_reads(events) that returns the reads whose lines have changed since the agent read them.

Input and output:

  • events: up to 2,000 events in the order they happened. ["read", file, first, last] reads lines first to last of file. ["write", file, first, last, count] replaces lines first to last of file with count new lines, and a count of 0 deletes them.
  • file is a non-empty string. Line numbers count from 1 and go up to 1,000,000,000, first is at most last, and count is at most 10,000.
  • Every event refers to lines that exist in the file at the moment it happens.
  • Return the indexes in events (counting from 0) of the stale reads, in increasing order.

Rules:

  • A read is stale when a later write to the same file replaced or deleted at least one of the lines it read.
  • A write changes only the lines it replaces. Every other line keeps its content.

Examples

stale_reads(events: list[list]) -> list[int]
Example 1
Inputstale_reads([["read", "app.py", 10, 20], ["write", "app.py", 15, 15, 3]])
Output[0]
Example 2
Inputstale_reads([["read", "app.py", 50, 60], ["write", "app.py", 1, 10, 0], ["write", "app.py", 55, 55, 1]])
Output[]
Example 3
Inputstale_reads([["read", "app.py", 30, 40], ["write", "app.py", 5, 5, 11], ["write", "app.py", 45, 45, 2]])
Output[0]