Fix the replay of racing search responses
Your task
A React search hook fetches results on every keystroke and shows whichever response comes back. A slow response for "ca" can land after the one for "cat" and replace it. The fix drops a response once a newer one is on screen, and the team wants a replay of which responses reach the screen.
An assistant wrote applied_search_results(query_lines, ignore_stale). It is in the editor. It has a bug. Find it and fix it.
Input and output:
query_lines: one string per keystroke, in order,"query|latency". The latency is how long that request takes to come back.ignore_stale: true when the hook has the guard, false for the hook without it.- Return the queries whose results were rendered, in the order they were rendered.
Rules the tests check:
- Line i, counting every line from 0, is sent at time i.
- Skip a line unless it splits on
|into exactly 2 parts and the latency is one or more digits from 0 to 9. The query can be empty. A skipped line still takes its time slot, so the next line is sent at its own index. - A response lands at its send time plus its latency.
- Handle responses in the order they land. When two land at the same time, the one sent first goes first.
- Without the guard, every response is rendered.
- With the guard, a response is rendered only when no request sent after it has been rendered already.
Examples
applied_search_results(query_lines: list[str], ignore_stale: bool) -> list[str]Example 1
Input
applied_search_results(["c|1", "ca|9", "cat|1"], False)Output
["c", "cat", "ca"]Example 2
Input
applied_search_results(["c|1", "ca|9", "cat|1"], True)Output
["c", "cat"]