FastPrepInstant Runoff Election

Instant Runoff Election

Bloomberg LP logoBloomberg LP● MediumNEW GRADONSITE INTERVIEW
Learn

Problem statement

Every row in ballots ranks all candidates from most to least preferred. In each round, count each ballot for its highest-ranked remaining candidate. A candidate with strictly more than half the ballots wins.

Otherwise eliminate the candidate with the fewest votes, breaking such ties by lexicographically largest name, and repeat. If all remaining candidates tie, return the lexicographically smallest candidate.

Function

instantRunoffWinner(ballots: String[][]) → String

Examples

Example 1

ballots = [["A","B","C"],["A","C","B"],["B","C","A"],["C","B","A"]]return = "A"

C is eliminated from the initial 2-1-1 round, and its ballot transfers to B; the 2-2 tie eliminates B, leaving A.

Constraints

  • Every ballot contains the same distinct candidate names.
  • At least one ballot and candidate exist.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public String instantRunoffWinner(String[][] ballots) {
  // Write your code here.
}
ballots[["A","B","C"],["A","C","B"],["B","C","A"],["C","B","A"]]
expected"A"
Checking account…