Monday, May 27, 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

#### 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:

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.

 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() 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

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

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

1,358Fans
10Followers
398Subscribers