Hackerrank Challenge Walkthrough

Another question of the challenge series answered step-by-step

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 completing them.

Here is a question on a HackerRank that is a decent example of something you might see in a technical interview or coding challenge:


An OrderedDict is a dictionary that remembers the order of the keys that were inserted first. If a new entry overwrites an existing entry, the original insertion position is left unchanged.

You are the manager of a supermarket.
You have a list of items together with their prices that consumers bought on a particular day.
Your task is to print each item_name and net_price in order of its first occurrence.

Input Format:

The first line contains the number of items, .
The next lines contains the item’s name and price, separated by a space.

Ok, let’s break this down step-by-step, with the help of the HackerRank discussions section ;).

This challenge revolves around the OrderedDict sub-class so we want to instantiate this class first:

from collections import OrderedDict
ordered_dict = OrderedDict()

By now, we should know how to input a simple integer for this first input number. However, if we pay attention to the input format, we can see that we will end up iterating through this number because it dictates the amount of ‘item_name’s and prices we will input afterward. So in this case, our N (for the number of items) will be embedded in a for loop, where we are iterating through the range of that first input number:

for i in range(int(input()):##input

Next, we want to input the item name and its price in one line. We can use the “.split()” method to divide what we input by a space and just assign that to item and price. Originally, I did this:

item, price = input().split()

An input box will appear. Let’s say I type:

#cookies 4
#cookies 4
#chicken 6
#noodles 3

I can now call the object “item” I created and it will output “noodles” because it was last. Likewise for ‘price’.


However, I ran into an issue when the item I wanted to input had two separate words, (i.e. “chicken wings”). This just returned a value error:

ValueError: too many values to unpack (expected 2)

The workaround is to use the “.rsplit()” method. The ‘r’ before split designates a right split. So we can start the split from the right side, which is our number. This will leave the rest of the input to be assigned to “item”:

#rsplit will split the string into a list, starting from the right
item, price = input().rsplit(' ', 1)

Now if we input “chicken wings 6”, then the 1st value “6” is split off by the space (‘ ‘) and “chicken wings” is assigned to “item”.

#chicken wings 6
#chicken wings

Good, now we want to add the prices of the same items so that we can output the net price of those items if there are multiple of those items.

We can assign the items and their prices to our ordered_dict by using the “.get()” method and placing “item, 0” inside those brackets. Then tack on the price after that. This will assign the cumulative price to each item in the dict, so get the item, or zero if the item is not there, plus the price.

ordered_dict[item] = ordered_dict.get(item, 0) + int(price)

Now if we call the object “ordered_dict”, we return our dictionary of items and their prices!

Now the task wants us to output a list of each unique item and its net price. So after our for loop, we want to print out “item” and “ordered[item]” for each item in that ordered_dict we made. To avoid returning a generator object (ordered_dict) we have to coerce our print function as a list:

[print(item, ordered_dict[item]) for item in ordered_dict]##output
#cookies 8
#chicken 6
#noodles 3

Great success!!!

Now all combined, our working solution looks like this:

from collections import OrderedDict
ordered_dict = OrderedDict()
for i in range(int(input())):

item, price = input().rsplit(' ', 1)

ordered_dict[item] = ordered_dict.get(item, 0) + int(price)

[print(item, ordered_dict[item]) for item in ordered_dict]

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!



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