Most Voted Candidate
Updated
Problem statement
In an election, every vote is the name of one candidate. You are given the votes as an array votes, where votes[i] is the name written on vote i. Two votes are for the same candidate exactly when their names are equal.
Return the name of the candidate with the most votes. If several candidates are tied for the most votes, return the tied name that comes first in dictionary order.
Dictionary order compares two names letter by letter from the left. At the first position where they differ, the name with the earlier letter comes first. If one name is a prefix of the other, the shorter name comes first (so "Ann" comes before "Anna").
Function
electionWinner(votes: String[]) → StringExamples
Example 1
votes = ["Maya","Leo","Maya","Iris","Leo","Maya"]return = "Maya"Maya has 3 votes, Leo has 2 and Iris has 1. Maya has the most votes.
Example 2
votes = ["Omar","Bea","Omar","Zoe","Bea"]return = "Bea"Omar and Bea both have 2 votes, and Zoe has 1. Omar and Bea are tied, and Bea comes first in dictionary order, so the answer is Bea, even though Omar got the first vote.
Example 3
votes = ["Anna","Ann","Anna","Ann","Ken"]return = "Ann"Anna and Ann both have 2 votes. Ann is a prefix of Anna, and a shorter name that is a prefix of a longer one comes first, so the answer is Ann.
Constraints
1 <= votes.length <= 10001 <= votes[i].length <= 20- Each name is one uppercase English letter followed by zero or more lowercase English letters.
More Expedia Onsite Interview questions
- Scramble StringONSITE INTERVIEW · Seen Sep 2026
- Smallest Sufficient TeamONSITE INTERVIEW · Seen Sep 2026
- Check Anagram Pairs Index by IndexONSITE INTERVIEW · Seen Oct 2025
- Fresh Oranges Left After Rotting StopsONSITE INTERVIEW · Seen Oct 2025
- Longest Increasing SubsequenceONSITE INTERVIEW · Seen Oct 2025
- Lowest Common Ancestor with Repeated and Missing ValuesONSITE INTERVIEW · Seen Oct 2025
- Shortest Substring with Exactly K OnesONSITE INTERVIEW · Seen Oct 2025
- Middle Element of a Linked ListONSITE INTERVIEW · Seen Nov 2024
- Minimum in a Rotated Sorted ArrayONSITE INTERVIEW · Seen Sep 2024
- Mirror Image Binary TreesONSITE INTERVIEW · Seen Sep 2024
Similar questions at other companies
- Map and Accumulate Learning QueriesPalantir · ONSITE INTERVIEWPalantir interview questions
- Filter Anagrams of a TargetMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Deduplicate Logs: Keep FirstGoogle · ONSITE INTERVIEWGoogle interview questions
- Loyal Customers by Two-Day Product VarietyAmazon · ONSITE INTERVIEW
- Count Word FrequenciesBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
- List Each Name Once, AlphabeticallyDocuSign · ONSITE INTERVIEWDocuSign interview questions
- Process Shopping Records, Promotions, and Display OrderInstacart · PHONE SCREEN · ONSITE INTERVIEWInstacart interview questions
- Deep Merge with a Conflict ResolverAutodesk · ONSITE INTERVIEWAutodesk interview questions
- Group Duplicate Files by ContentPostman · ONSITE INTERVIEWPostman interview questions
- Group AnagramsGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
Expedia interview experiences
- Expedia Intern Final Round in 2021: 28 Experiences (Part 4 of 6)
- Expedia Intern Finals Before 2020: 14 Experiences (Part 6 of 6)
- Expedia Intern Final Round in 2020: 24 Experiences (Part 5 of 6)
- Expedia Intern Final Round in 2022: 15 Experiences (Part 3 of 6)
- Expedia Intern Final Round in 2023: 17 Experiences (Part 2 of 6)
- Expedia SWE Intern Final Round: 126 Experiences (Part 1 of 6)