PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches
|Home/Coding & Algorithms/Coursera

Implement Popular and Ranked Voting

Last updated: Apr 6, 2026

Quick Overview

This question evaluates algorithmic implementation skills and understanding of voting systems, including aggregation of first-place preferences and the conceptual mechanics of elimination-based ranked-choice counting.

  • hard
  • Coursera
  • Coding & Algorithms
  • Backend Engineer

Implement Popular and Ranked Voting

Company: Coursera

Role: Backend Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Technical Screen

You are given ranked ballots in aggregated form. Each key is an ordered tuple of candidate names representing one voter's preference from highest to lowest, and each value is the number of voters who submitted that exact ranking. Implement two methods: 1. `popular_vote(ballots) -> candidate` - Return the candidate with the largest number of first-place votes. 2. `ranked_choice_winner(ballots) -> candidate` - In each round, count only the highest-ranked non-eliminated candidate on each ballot. - If any candidate has more than 50% of the active first-place votes, return that candidate. - Otherwise, eliminate the candidate with the fewest first-place votes and recount. - Continue until a candidate has a majority or only one candidate remains. Assumptions: - Every ballot ranks the same set of candidates. - There is no tie when choosing which candidate to eliminate. Example input: `ballots = {` ` ("A", "B", "C"): 4,` ` ("B", "C", "A"): 3,` ` ("C", "B", "A"): 2` `}` For this example: - The popular-vote winner is `A`, because `A` has the most first-place votes initially. - The ranked-choice winner is `B`: first-round counts are `A=4, B=3, C=2`; eliminate `C`, then `C`'s ballots transfer to `B`, so `B` becomes `5` out of `9`.

Quick Answer: This question evaluates algorithmic implementation skills and understanding of voting systems, including aggregation of first-place preferences and the conceptual mechanics of elimination-based ranked-choice counting.

Related Interview Questions

  • Implement Plurality and Runoff Voting - Coursera (hard)
  • Implement plurality and majority voting - Coursera (medium)
Coursera logo
Coursera
Jan 30, 2026, 12:00 AM
Backend Engineer
Technical Screen
Coding & Algorithms
0
0

You are given ranked ballots in aggregated form. Each key is an ordered tuple of candidate names representing one voter's preference from highest to lowest, and each value is the number of voters who submitted that exact ranking.

Implement two methods:

  1. popular_vote(ballots) -> candidate
    • Return the candidate with the largest number of first-place votes.
  2. ranked_choice_winner(ballots) -> candidate
    • In each round, count only the highest-ranked non-eliminated candidate on each ballot.
    • If any candidate has more than 50% of the active first-place votes, return that candidate.
    • Otherwise, eliminate the candidate with the fewest first-place votes and recount.
    • Continue until a candidate has a majority or only one candidate remains.

Assumptions:

  • Every ballot ranks the same set of candidates.
  • There is no tie when choosing which candidate to eliminate.

Example input:

ballots = { ("A", "B", "C"): 4, ("B", "C", "A"): 3, ("C", "B", "A"): 2 }

For this example:

  • The popular-vote winner is A , because A has the most first-place votes initially.
  • The ranked-choice winner is B : first-round counts are A=4, B=3, C=2 ; eliminate C , then C 's ballots transfer to B , so B becomes 5 out of 9 .

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Coursera•More Backend Engineer•Coursera Backend Engineer•Coursera Coding & Algorithms•Backend Engineer Coding & Algorithms
PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.