FunctionRacerProblemsRace

Instant Runoff Ballot Box

MediumClassesPython, JavaScript, TypeScript, Java, C++

Implement a class BallotBox that counts ranked ballots by instant runoff. Write these methods:

The tests call the methods in sequence and compare every return value. The expected list holds null for the constructor.

Example 1

BallotBox(["ann","bob","cat"])
cast(["ann","bob"]) returns 1
cast(["bob"]) returns 2
cast(["cat","bob"]) returns 3
cast(["ann"]) returns 4
cast(["cat","ann"]) returns 5
find_winner() returns "ann"
rounds_needed() returns 3
first_choices("cat") returns 2

Round 1 gives ann 2, bob 1, cat 2, so bob is out and his own ballot has nobody left. Round 2 is a 2 to 2 tie; cat comes later in candidates, so cat is out. In round 3 ann has all 3 ballots that still count.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Classes problems