Confirmation Code Expiry
EasyState Machines · Paypal
PayPal texts users one-time confirmation codes. Process events in order; each event is [kind, user_id, code, day], where code is a string and day is an integer:
"send": a new code is sent touser_idonday. It replaces any earlier code for that user, even an unused one, and starts out unused."confirm":user_identerscodeonday.
A confirm succeeds only if all of these hold:
- The user has been sent a code, and
codeequals their latest sent code exactly. - That latest code has not already been used by a successful confirm.
- It has not expired:
confirm_day - send_day <= ttl.
A successful confirm marks the latest code as used. A failed confirm changes nothing (it does not use up or invalidate the code).
Return a list with one boolean per "confirm" event (True for success, False otherwise), in order.
Example 1
- Input:
- events = [["send", "ana", "4821", 1], ["send", "ana", "9047", 2], ["confirm", "ana", "4821", 3], ["confirm", "ana", "9047", 4], ["confirm", "ana", "9047", 4], ["send", "bo", "1111", 4], ["confirm", "bo", "1111", 10]], ttl = 5
- Output:
- [False, True, False, False]
- Explanation
- Ana's first code was replaced by
9047, so confirming4821fails.9047then succeeds and is used up, so the repeat fails. Bo confirms 6 days after the send, past the TTL of 5.
Example 2
- Input:
- events = [["send", "u", "77", 0], ["confirm", "u", "78", 1], ["confirm", "u", "77", 2]], ttl = 3
- Output:
- [False, True]
- Explanation
- The wrong code fails but does not invalidate the real one, which then succeeds 2 days after sending.
Example 3
- Input:
- events = [["send", "u", "a", 10], ["confirm", "u", "a", 15]], ttl = 5
- Output:
- [True]
- Explanation
- Exactly 5 days have passed, and the rule is
<= ttl, so it succeeds.
Constraints
- 1 ≤ events.length ≤ 105
- 0 ≤ ttl ≤ 106
- Days are non-decreasing integers in
[0, 10^9]. - Codes and user ids are strings, compared exactly.