REPOSITORY CODING · HARD
Remove withdrawn preferences before ballot eligibility
Skip every withdrawn preference before assigning ballot weight. Ballots left with no eligible candidate are abstentions and must not inflate the quota.
Exact election countingTransferable vote laboratory
Work through a real codebase.
Choose an environment, investigate the existing implementation and use the tests to check your change. Sign in to open a workspace; execution and Ono usage follow your plan’s allowances.
Task requirements and environments
Ruby on Rails · 80 minute session
- Count a bounded multi-seat transferable-vote election with 1..8 distinct ASCII candidate IDs, 1..5 seats and at most 30 uniquely identified ballots. Ballot weights are integers 1..100 and preferences are duplicate-free known candidate IDs. Withdrawn candidates are removed from every preference list before determining valid/abstained ballot weight.
- Use exact rational arithmetic and a fixed Droop quota floor(validWeight/(seats+1))+1. Each round allocates live ballot weight to its first active preference; unallocated live weight is exhausted. Initial empty/withdrawn-only ballots are abstentions. Retained winner weight plus live tallies plus exhaustion must conserve initial valid weight.
- Elect the largest tally meeting quota, breaking equal election tallies by ascending candidate ID. Transfer each allocated ballot at the inclusive Gregory surplus factor (tally-quota)/tally and retain quota weight. Otherwise eliminate the lowest tally, breaking elimination ties by descending candidate ID, without changing ballot weights.
- If remaining candidates fit all remaining seats, elect them in descending tally/ascending ID order without further surplus transfers. Return winners in election order and every immutable round snapshot with sorted rational tallies, exhaustion and transfer factor, all fractions in reduced numerator/denominator form. This is the stated bounded STV policy, not a claim of jurisdiction-specific election compliance.
- Native Rails provides tenant-scoped persisted rehearsal editions as well as the pure count endpoint, preview and visible grading command. Validate malformed contests before constructing a mutable transfer ledger.