Tuesday, October 15, 2024
HomeAlgorithmsHackLand Election - Coding Interview Questions #1

HackLand Election – Coding Interview Questions #1

-

This series will be focused on Coding interview questions curated from various interviews / via online source.

” 

Hackland Election

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.

Constraints

  • 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.

Input Example:

5

Vamsi

Krishna

Vamsi

Krishna

Tallapudi

Output:

Vamsi

Related Links

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:


package main.interview.hacklandelection
import com.sun.xml.internal.fastinfoset.util.StringArray
import java.util.*
fun main() {
val list = StringArray()
var iter = readLine()!!.toInt()
while(iter– > 0) {
list.add(readLine()!!)
}
electionWinner(list)
}
fun electionWinner(list: StringArray) {
var winner = ""
var max = 0
val hashMap = HashMap<String, Int>()
for (i in 0 until list.size) {
if (hashMap.containsKey(list[i])) {
hashMap[list[i]] = hashMap.getValue(list[i])+1
} else {
hashMap[list[i]] = 0
}
}
for (entry in hashMap.entries) {
if(entry.value > max) {
max = entry.value
winner = entry.key
} else if(entry.value == max) {
winner = if(entry.key > winner) entry.key else winner
}
}
println(winner)
}

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.

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.

LATEST POSTS

Packages in Java

Hello World! Today's blog is about packages in java. Here, we will learn about what are packages and how we can use packages along with...

House Robber Problem | Maximum Sum of non-adjacent elements

In this blog post, we will discuss about how to find maximum sum of Non-Adjacent elements. Since House Robber Problem is a typical DP Problem...
00:11:41

Check Array Formation Through Concatenation | Leetcode Problem 1640

In this video, we will explain Check Array Formation Through Concatenation, Leetcode's 1640th problem in 2 different approaches along with a coding solution. Problem statement You are given...

Binary Tree Traversals in Kotlin – PreOrder, InOrder and PostOrder Traversals

Binary Tree Traversals are most common Tree Problems in an Interview as well as Competitive Programming. They can be efficient while searching, inserting and deleting...

Follow us

1,358FansLike
10FollowersFollow
397SubscribersSubscribe

Most Popular