Fix the replay of a guarded pay button
Your task
Your checkout button guarded against double-submit with a React state flag, and fast double-clicks still charged the card twice. The fix is a ref used as a lock, plus an idempotency key the server checks. An idempotency key is an id the server uses to spot a repeated request. The team wants a replay that shows which clicks become real charges.
An assistant wrote settled_charge_indices(click_times, keys, charge_ms). It is in the editor. It has a bug. Find it and fix it.
Input and output:
click_times: when each click arrived, in milliseconds, in order. Times are 0 or more and never go down.keys: the idempotency key sent with each click.keys[i]belongs to clicki.charge_ms: how long one request to the server takes, in milliseconds.- Return the indices (from 0) of the clicks that charge the card, in increasing order.
Rules the tests check:
- Use only the first n clicks, where n is the length of the shorter list. Ignore the extra entries of the longer one.
- A negative
charge_mscounts as 0. - The first click always passes the lock. A later click passes when its time is at or after the release time: the time of the last click that passed, plus
charge_ms. A click before that is refused. It sends nothing and changes nothing. - Every click that passes sends a request and takes the lock, whether or not its key is new. The release time becomes its time plus
charge_ms. - If an earlier charge used the same key, the server replays the stored result and nothing is charged. Otherwise the click charges the card and its key is recorded.
- Keys compare exactly. Case matters, and the empty string is a key like any other.
- No clicks returns an empty list.
Examples
settled_charge_indices(click_times: list[int], keys: list[str], charge_ms: int) -> list[int]Example 1
Input
settled_charge_indices([0, 30], ["k1", "k1"], 500)Output
[0]Example 2
Input
settled_charge_indices([0, 600], ["k1", "k2"], 500)Output
[0, 1]Example 3
Input
settled_charge_indices([0, 600], ["k1", "k1"], 500)Output
[0]