Find Winner of an Election

Quick Overview

Given a list of votes where each entry is a candidate's name, return the candidate with the most votes, breaking a tie by the lexicographically smaller name. The problem tests counting with a hash map and applying a deterministic tie-break only among the leading candidates.

Find Winner of an Election

Company: Worldpay

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given an array of strings `votes` where each element is the name of the candidate a voter voted for, return the name of the candidate who received the most votes. If there is a tie in the maximum number of votes, return the lexicographically smaller name among the tied candidates. ### Function Signature ```python def find_winner(votes: list[str]) -> str: ``` ### Rules - Only the candidates tied at the maximum vote count are compared by name. A candidate with fewer votes never wins, however early it sorts. - Lexicographic order is ordinary string comparison: compare letter by letter, and a name that is a prefix of another name is smaller (`"al"` comes before `"alice"`). ### Constraints - `1 <= len(votes) <= 10^4` - `1 <= len(votes[i]) <= 10` - `votes[i]` consists of lowercase English letters. ### Examples ```text Input: votes = ["alice","bob","alice","carol","bob","alice"] Output: "alice" ``` alice has 3 votes, bob has 2 and carol has 1, so alice wins outright. ```text Input: votes = ["dave","eve","dave","eve"] Output: "dave" ``` dave and eve are tied with 2 votes each, and `"dave"` is lexicographically smaller than `"eve"`. ```text Input: votes = ["bob","carl","carl","bob","anna"] Output: "bob" ``` bob and carl both have 2 votes and anna has 1. The tie is only between bob and carl, and `"bob"` comes before `"carl"`. anna sorts first but has fewer votes.

Overview: Given a list of votes where each entry is a candidate's name, return the candidate with the most votes, breaking a tie by the lexicographically smaller name. The problem tests counting with a hash map and applying a deterministic tie-break only among the leading candidates.

Worldpay logo
Worldpay
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given an array of strings votes where each element is the name of the candidate a voter voted for, return the name of the candidate who received the most votes.

If there is a tie in the maximum number of votes, return the lexicographically smaller name among the tied candidates.

Function Signature

def find_winner(votes: list[str]) -> str:

Rules

  • Only the candidates tied at the maximum vote count are compared by name. A candidate with fewer votes never wins, however early it sorts.
  • Lexicographic order is ordinary string comparison: compare letter by letter, and a name that is a prefix of another name is smaller ( "al" comes before "alice" ).

Constraints

  • 1 <= len(votes) <= 10^4
  • 1 <= len(votes[i]) <= 10
  • votes[i] consists of lowercase English letters.

Examples

Input: votes = ["alice","bob","alice","carol","bob","alice"]
Output: "alice"

alice has 3 votes, bob has 2 and carol has 1, so alice wins outright.

Input: votes = ["dave","eve","dave","eve"]
Output: "dave"

dave and eve are tied with 2 votes each, and "dave" is lexicographically smaller than "eve".

Input: votes = ["bob","carl","carl","bob","anna"]
Output: "bob"

bob and carl both have 2 votes and anna has 1. The tie is only between bob and carl, and "bob" comes before "carl". anna sorts first but has fewer votes.