CodingNeed.

Bridge · From syntax to engineering · 40 MIN

Project · Apply each ledger event once

A repeated identifier must keep the same meaning.

Clients may repeat a request after losing the response. Store the meaning of the first event under its ID. A repeat with the same amount has no additional effect; a repeat with a different amount is a conflict. A Map implements this for a single in-memory batch. Real services need durable uniqueness, transaction boundaries, an expiry policy, and owner-scoped keys. The downloadable Expense API project builds on this model so you can inspect the HTTP behavior and test duplicate requests.

Write a small contract first, then test how the implementation behaves at its boundaries.

Read the example

const seen = new Map([["evt-1", 10]]);
console.log(seen.get("evt-1"));
Check the expected output
10

Your challenge

Input is a list of {id:string,amount:integer}. Apply the first event for each id; ignore exact repeats and list ids of conflicting repeats in encounter order. Return {total, appliedIds, conflicts}. Do not mutate input.

Solution cost: Expected O(n). time · O(u + f), unique ids plus conflicts. space

Common trap

Checking if seen.get(id) is truthy loses the valid amount zero.

Further reading: HTTP method idempotency

Essential cookies keep your account signed in. Optional analytics is not configured on this site. Your choice does not affect access to lessons.

Read the Privacy Policy. You can change this choice in the footer.