Fix the replay of a search box race
Your task
A search box fires one request per keystroke and shows whichever response lands last, so a slow response for "ca" can replace the results for "cat". The fix is an effect cleanup: when the query changes, the effect disowns the request it started, and only the newest request may write.
An assistant wrote displayed_query(issued_queries, arrival_order, ignore_stale). It is in the editor. It has a bug. Find it and fix it.
Input and output:
issued_queries: the query of each request, in the order the requests were sent. Requestiwas sent forissued_queries[i].arrival_order: request indices, in the order their responses came back. A request whose response never came back is missing. An index can repeat or fall outside the list.ignore_stale: true with the cleanup guard, false for the shipped code with no guard.- Return the query whose results are on screen after the last arrival, or
""when nothing has been written.
Rules the tests check:
- No requests returns
"". - Ignore any index below 0 or not below the number of requests.
- Without the guard: every response writes. Return the query of the last valid arrival, or
""when there is none. - With the guard: only the newest request, the last one sent, may write. A response from any earlier request never writes, whenever it arrives. Return the newest query if its index appears anywhere in
arrival_order, otherwise"".
Examples
displayed_query(issued_queries: list[str], arrival_order: list[int], ignore_stale: bool) -> strExample 1
Input
displayed_query(["ca", "cat"], [1, 0], False)Output
"ca"Example 2
Input
displayed_query(["ca", "cat"], [1, 0], True)Output
"cat"Example 3
Input
displayed_query(["ca", "cat"], [0, 1], False)Output
"cat"