Hackerrank Tuple Thought Process

Today I decided to tackle the Tuples problem set on Hackerrank. It is categorised as Easy but it was quite a challenge for me. The following is how I arrived at a solution and what my thought process looked like.

Tutorial Section

There is a section called Tutorial at the top of the problem set. You can use this to find out more about what a tuple is and what it can and can't do. After you have submitted your solution or attempted a couple of times, the Discussion section is often worth a look.

Rephrase the Question

Questions on Hackerrank have a tendency to be wordy and difficult to comprehend. I have a feeling this ambiguity is intentional and it is probably good preparation for interviews on the job market. Before I even begin to tackle the problem, I now spend time simplifying the question so that I can more easily assimilate what is being asked.


Create a tuple of integers and return the hash of these numbers.

Task Breakdown

  1. Accept input from user in the form of a string.
  2. Convert string to list of integers.
  3. Convert list to tuple
  4. Return the hash value of tuple.

Tuple Properties

As I haven't been dealing with Tuples on a regular basis, the characteristics of a tuple are not freely available to me from memory, so a little digging was required. Here are 3 points worth noting about tuples.

  1. The key difference between a list and a tuple is that a tuple is immutable, which means we cannot modify a tuple, a list by contrast is mutable.
  2. The parentheses are optional, the below values are the same:

    my_tuple = (1, 2, 3)

    my_tuple = 1, 2, 3

  3. A comma is needed after a single element, otherwise Python will think it is an integer or a string:

          a_tuple = (1,)

          an_integer = (1)

          a_string = ("1")

When to choose a Tuple over a List

Lists are generally used over tuples due in large part to the versatility of lists. However, a tuple would be useful if you want to iterate over a very large list quickly and you do not wish the items of the list to change. A tuple also takes up less space in memory. This can be observed by importing the sys module and using the getsizeof function, the size is measured in bytes:

Here is a useful video by Socractica on tuples.

Problem Solving Process


In the problem itself we are given the following code block:

if __name__ == '__main__': n = int(input()) integer_list = map(int, input().split())

You can ignore if __name__ == '__main__':  because it doesn't directly impact the code.

The int(input()) function takes input from the user in integer form, again it doesn't directly impact the solution and the reason is given in the discussion section.

I needed clarification on what the split method does, which I found here.

I then needed to read about what the map function does.

From the problem itself you can see that a list has been assigned. The next steps are to create a tuple and then return the hash. As stated in the question the hash is a built in function in Python so it is not necessary to import a new module.

Once you have attempted the problem or even solved it, you can find a comprehensive breakdown of the problem under the discussion section I have referred to.


Something I have found useful is running the solution in my text editor and terminal window while solving the problem. This allows me to get real time feedback and it allows me to print output that I would like to see, e.g. the contents of the list or the tuple before the hash is returned.


Did you find this post useful? Do you use Hackerrank? If so I would love to hear from you in the comments section below or by dropping me an e-mail.