Few days ago, I attended an Android Interview for an MNC where I encountered an interesting coding question – Hackland Election. In this post, I would like to share it with you guys so that it might be helpful for your interview preparations.
Here is the question in the similar lines that was asked during the interview:
Table of Contents
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 highes 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 last name wins.
Complete the election Winner function in your editor. It has 1 parameter:L an array of strings, votes, describing the votes in the ballot box. This function must review these votes and return a string representing the name of the winning candidate.
- 1<= n <= 10^4
Input Format An array of Strings
Output Format Name of the person who has the max votes. If there are multiple people with same number of votes(max) then print the last name of the list arranged in dictionary order.
Coding Approach for Hackland Election Problem
Here is the output using Kotlin Programming Language. Here I am using Collection Framework’s HashMap with the approach of storing key value pairs with new key addition for every new word and incrementing the value for every repeated word:
Hope it will be helpful to you guys for your interview preparation. Feel free to suggest modifications if any. Contact us here if you want to share your interview questions with fellow developers.