Implement plurality and majority voting

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.

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.

|Home/Coding & Algorithms/Coursera
Coursera logo
Coursera
Dec 15, 2025, 12:00 AM
mediumSoftware EngineerTechnical ScreenCoding & Algorithms
5
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)

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...