Idempotency Key Handler
Networks retry. A customer’s webhook fires twice; a payment request is re-sent after a timeout. Without idempotency you double-charge or double-write. The fix: dedupe by a client-supplied key and always return the first result. Solve it in Python or TypeScript, with hidden tests.
The problem
Implement process(requests). Each request is [key, value]. The first time you see a key, record its value as the result. Any later request with the same key must return that first recorded result — the new value is ignored (the operation already happened). Return the list of results, one per request, in order.
requests = [["k1","a"],["k1","b"],["k2","c"],["k1","d"]]["a", "a", "c", "a"]- The result for a key is fixed by its first occurrence; later values for that key are discarded.
- Return one result per request, preserving order.
- This is exactly how an idempotency-key store makes a retried POST safe.
Your turn — write it
Edit the stub, hit Run (or ⌘/Ctrl + Enter), and watch the hidden tests. Stuck? the hints are right above and Reveal solution is one click away.
Implement process(requests): keep a map of key → first result. For each request, if the key is new, store its value; then append the stored result for that key.
- Use a dict/Map from key to the first value seen.
- On each request: if the key isn’t stored yet, store the value.
- Always append the stored value for the key (which is the first one) to the output.
Explore the topic
See this challenge alongside everything else on the same subject — handbooks, system designs, algorithms and tools, in one place.