PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches
|Home/Coding & Algorithms/Coursera

Implement plurality and majority voting

Last updated: Mar 29, 2026

Quick Overview

This question evaluates skills in frequency-based selection and majority detection, including handling tie-breaking rules and applying linear-time, constant-space algorithms such as the Boyer–Moore majority vote.

  • medium
  • Coursera
  • Coding & Algorithms
  • Software Engineer

Implement plurality and majority voting

Company: Coursera

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You are given an array `votes` of length `n`, where each element is a candidate identifier (string or integer). Implement two voting systems: ## 1) Plurality vote Return the candidate with the highest number of votes. - If multiple candidates are tied for the highest count, return the lexicographically smallest candidate (if identifiers are strings) or the smallest numeric id (if integers). ## 2) Majority vote (Boyer–Moore) Return the candidate who has **strictly more than** `n/2` votes. - If no such candidate exists, return `null` (or `None`). - This must be done in **O(n)** time and **O(1)** extra space (aside from the input), i.e., using the Boyer–Moore majority vote technique. ### Input/Output - **Input:** `votes: List[Candidate]` - **Output:** - `pluralityWinner(votes) -> Candidate` - `majorityWinner(votes) -> Candidate | null` ### Constraints - `1 <= n <= 10^6` - Candidate identifiers are comparable for tie-breaking. ### Examples 1. `votes = ["A", "B", "A", "C", "B", "A"]` - plurality winner: `"A"` (3 votes) - majority winner: `null` (3 is not > 6/2) 2. `votes = [2, 2, 1, 2]` - plurality winner: `2` - majority winner: `2` (3 > 4/2)

Quick Answer: This question evaluates skills in frequency-based selection and majority detection, including handling tie-breaking rules and applying linear-time, constant-space algorithms such as the Boyer–Moore majority vote.

Related Interview Questions

  • Implement Plurality and Runoff Voting - Coursera (hard)
  • Implement Popular and Ranked Voting - Coursera (hard)
Coursera logo
Coursera
Dec 15, 2025, 12:00 AM
Software Engineer
Technical Screen
Coding & Algorithms
1
0

You are given an array votes of length n, where each element is a candidate identifier (string or integer).

Implement two voting systems:

1) Plurality vote

Return the candidate with the highest number of votes.

  • If multiple candidates are tied for the highest count, return the lexicographically smallest candidate (if identifiers are strings) or the smallest numeric id (if integers).

2) Majority vote (Boyer–Moore)

Return the candidate who has strictly more than n/2 votes.

  • If no such candidate exists, return null (or None ).
  • This must be done in O(n) time and O(1) extra space (aside from the input), i.e., using the Boyer–Moore majority vote technique.

Input/Output

  • Input: votes: List[Candidate]
  • Output:
    • pluralityWinner(votes) -> Candidate
    • majorityWinner(votes) -> Candidate | null

Constraints

  • 1 <= n <= 10^6
  • Candidate identifiers are comparable for tie-breaking.

Examples

  1. votes = ["A", "B", "A", "C", "B", "A"]
    • plurality winner: "A" (3 votes)
    • majority winner: null (3 is not > 6/2)
  2. votes = [2, 2, 1, 2]
    • plurality winner: 2
    • majority winner: 2 (3 > 4/2)

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Coursera•More Software Engineer•Coursera Software Engineer•Coursera Coding & Algorithms•Software 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.