Amazon Interview Question: Count the Votes. Now Let Everyone Change Their Mind.
One voter, two browser tabs, and the follow ups that turn a simple counter into a system design problem. Maya votes for A, changes her mind, and votes for B. Her connection drops, so her phone sends the second request again. If your system now counts four votes from three people, adding more servers is going to make the wrong answer arrive faster. This is why I like this question. You can…
The Amazon interview question "Count the Votes" involves designing a system for a live poll with millions of concurrent voters. People can change their choices, and results should refresh every second. The goal is to understand the product quickly while designing an architecture that accurately tracks individual choices.
Consider a scenario with three voters: Maya votes for A, Leo for A, and Nia for B. Maya then changes her mind to B, making the final results A = 1 and B = 2. The key rule is that each eligible account contributes at most one current choice per poll. To maintain this rule, store the current choice under the pair of poll ID and voter ID, along with a version number.
When a voter changes their mind, update the record by subtracting from the previous option and adding to the new one as one indivisible operation. Use a transactional datastore to enforce that the old version is used in the transaction, preventing stale states from causing issues. If multiple requests attempt to change the same voter's choice, the transaction that commits first updates the record, while the other request fails due to the version condition.
Handle dropped connections by assigning a stable request ID to each intended change, scoped to the poll and authenticated voter. Persist the outcome in the same transaction as the vote mutation. Include the original request parameters or a fingerprint of them in the record to reject reusing the same ID for different content.
For large-scale traffic, such as three million accounts submitting one change over 300 seconds, split the counters into buckets using a hash of poll ID and voter ID. Assign each voter to a stable bucket, ensuring their current choice and changes always use the same bucket during the poll. This approach helps avoid contention points and enables efficient aggregation of bucket totals.
Rewritten by urgent.news; may contain errors — check the original.
Source: dev.to