AlgoMaster Logo
AlgoMasterDeduplicate Idempotent Requestseasy

Deduplicate Idempotent Requests

easy

An API client may retry a request after a timeout without knowing whether the first attempt completed. An idempotency key lets the server return the first attempt's stored result instead of performing the operation again.

Design an IdempotencyKeyStore class:

  • IdempotencyKeyStore() creates an empty store.
  • int execute(String key, int proposedResult) handles one request.

If key has not appeared before, store and return proposedResult. If the key already exists, ignore proposedResult and return the result stored by the first request.

Keys are case-sensitive. Stored entries remain available for the lifetime of the object.

Example 1:

Input:

Output:

Explanation: The first a request stores 1. Its retries return 1, ignoring the proposed results 3 and 5. Keys b and c have independent entries.

Example 2:

Input:

Output:

Explanation: Only the first request for x stores a result. Every retry replays 10.

Constraints

  • 1 <= key.length <= 100
  • key contains printable ASCII characters.
  • -10^9 <= proposedResult <= 10^9
  • At most 10^5 calls are made to execute on one object.
  • Keys never expire during a test case.
Hints

Loading...
CallReturns
new IdempotencyKeyStore()null
execute("a", 1)1
execute("b", 2)2
execute("a", 3)1
execute("c", 4)4
execute("a", 5)1

The first request for a stores 1. Later requests with key a return that original value and do not replace it with 3 or 5.

Run checks these cases. Submit also runs a larger hidden set.