Coding Interview Questions-HackLand Election

Zaid Alissa Almaliki
1 min readJul 21, 2019

There are n citizens voting in this year’s Hackland election. Each voter writes the name of their chosen candidate on a ballot and places it in a ballot box. The candidate with the highest number of votes win the election; if two or more candidates have the same number of votes, then the tied candidates’ names are ordered alphabetically and the first name wins.

First Example

Input = [“alex”, “zaid”, “harry”, “dave”, “zaid”, “victor”, “harry”, “alex”, “mary”, “mary”]

Output: alex

Explanation first example votes = {“Alex”, “Zaid”, “Harry”, “Dave”, “Zaid”, “Victor”, “Harry”, “Alex”, “Mary”, “Mary”} Alex, Harry, Zaid, and Mary are all tied for the highest number of votes. Because Alex is alphabetically first, we return his name as the winner.

Second Example

Input = { “victor”, “sarah”, “ryan”, “dave”, “maria”, “anna”, “anna”, “ryan”, “sarah” };

Output: anna

Explanation second example votes = {“Victor”, “Sarah”, “Ryan”, “Dave”, “Maria”, “Maria”, “Anna”, “Anna”, “Ryan”, “Sarah”} Sarah, Ryan, Maria, and Anna are all tied for the highest number of votes. Because Anna is alphabetically first, we return her name as the winner.

Cheers,
Zaid Alissa Almaliki

Check our free course on Udemy

Thanks for reading. If you loved this article, feel free to hit that follow button so we can stay in touch.

--

--

Zaid Alissa Almaliki

Founder, Principal Data Engineer and Cloud Architect Consultant in DataAkkadian.