Hackerrank-type coding question: balanced-string

and why doing these questions are worth your time

Back to my series of coding challenge questions. I’ll be posting a few of these individually, as they are basically required practice if you are an entry-level data analyst or data scientist looking for your first position.

Think from the hiring manager’s perspective. They see you might have some projects and coding experience, but that doesn’t completely cut it for them. As one respectable senior data scientist once told me, who will the hiring person choose: the one who can code the right answers after scouring through stack overflow and google or the one who can code the answer just from the knowledge in his head? I think you know the answer to that.

These questions might seem mindless and unreasonably challenging at times, but your answers to them demonstrate the skills in your head. And once you start to get the right answers, you will feel good. How you approach these problems is also key to successfully completing them.

Here is a question on a code challenge site that is a good example of something you might see:

We consider a string to be balanced if it consists of exactly two different characters and both of those characters appear exactly the same number of times. For example: “aabbab” is balanced (both ‘a’ and ‘b’ occur three times) but “aabba” is not balanced (‘a’ occurs three times, ‘b’ occurs two times). String “aabbcc” is also not balanced (it contains three different letters). Write a function that determines if the string is balanced.

Once again, let’s break this down into manageable steps.

Let’s first create an input box so we can test our own values:

S = str(input())#'abbba'

How do we start approaching this? The idea is to iterate through each character and create a count of each. We can accomplish this with a dictionary, and using list comprehension to build it.

The set() method we will return a dictionary of unique/disctinct characters of our string which are iterable:

set(S)#{'a', 'b'}

Now we want to count the occurrences for each unique letter, which would be each letter in this set. We can do this using the count() method with that list comprehension I mentioned a few lines ago.

#character(char): count the character in S for each character in the unique set of Sdict = {char: S.count(char) for char in set(S)}
#{'b': 2, 'a': 2}

We now have a count of each value. What we want to do from here is coerce this dictionary’s *values* into a set. The values in our dictionary are 2, 2. So if they are in a set, there should only be one ‘2’ if there value counts are equal.

If there is more than 1 unique value, then the value counts are not equal, and therefore not balanced.

#coerce those dict values into a list (otherwise we cannot get the unique values with the set() method. Then to get the len of that set, we have to coerce back to a list. 
len(list(set(list(dict.values())))) == 1#False

Since I tested an unbalanced string ‘abbba’, we returned False, which is good! If you try this with ‘abba’, a balanced string, we return True.

If you noticed in the instructions, the string is not considered balanced if it has more than 2 different characters. So we will have to account for this by adding something simple to our code:

#if the length of our dict is not 2, then our string has more (or less) characters and is unbalanced.len(dict) == 2#False

Now we want to combine this code into a function.

def solution(S):
dict = {char: S.count(char) for char in set(S)}
if len(list(set(list(dict.values())))) == 1 and len(dict) == 2:
return 'Balanced'
return 'Not balanced'


There we go!

See how walking through the logic step-by-step was making this much easier? This is my general approach for all of these types of questions. The more you do this, the more fluid your capacity to answer questions using Python becomes!

I’m a recent Data Science graduate with a B.S. in Environmental Science. Currently seeking job opportunities. Constantly learning!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store