(Hackerrank) Day 1: Data Types

Wendy Raven McNair
7 min readSep 23, 2021

Apply your detective skills to this data types challenge

Photo by cottonbro from Pexels

Day 0 of HackerRank’s 30 Days of Code was fairly simple so now, let’s try the challenge for Day 1 Data Types. First let’s look up data types to clarify what this challenge is about. A quick Google search identifies data type as a type of data. Really!? Basically a data type is a description of what kind the data is. Examples of data types include numbers, strings (think of it as text), and booleans (true or false). Devmountain gives a great introduction to data types in the following YouTube video:

https://www.youtube.com/watch?v=_r_LCMBvxmg

To ensure the computer can identify and accurately process data types, they must be written and handled in specific ways. So I imagine this HackeRank challenge will give us practice in accurately handling data types.

The Day 1 tutorial video is disabled on the HackerRank website but you can watch it here:

https://www.youtube.com/watch?v=XLCka0noTY4

Reading through the problem, the variables i, d, and s have been declared and initialized for us. Our task is to:

— declare 3 variables (and each appears to be a different kind of data type — int, double, string)

— read 3 lines of input from stdin (details in the Input Format section)

— initialize 3 variables (using previous input)

— use + operator to perform 3 operations:

- sum i and our 1st variable

- sum d and our 2nd variable (to one decimal place)

- concatenate s and our 3rd variable

At this point I’m clear on an int or integer (whole number) and a string (basically text). But what’s a double? Google it! …Okay, it’s a data type for decimal numbers. Which can also be referred to as floats. So we’re learning how to code whole numbers, decimals, and text.

The Input Format section details what each data type will be (an integer, a double, and a string) and the order they’ll be in and repeats what operation to perform on each:

— sum intergers

— sum doubles

— concatenate strings

The Output Format section succinctly summarizes the instructions already given.

The Sample Input section shows what the input data will look like:

Given that the variables i = 4, d = 4.0, and s = “HackerRank”, the above input results in Sample Output showing:

Note I got the variable values from the function in the HackerRank ide.

The Explanation section details how the results were achieved and also instructs we must read input from stdin. We cannot assign the Sample Case values to our variables like below:

intVariable = 12

doubleVariable = 4.0

stringVariable = is the best place to learn and practice coding!

Okay, now that we know the challenge details, let’s head to the HackerRank ide and try to solve this challenge.

Select your preferred coding language by clicking in the Language box dropdown menu, I selected JavaScript. Click the greater than symbol next to number 1 and it expands to reveal preset code that we shouldn’t change. In the function called main, we see the variables i, d, and s and their values as I indicated above (4, 4.0, and “HackerRank”). We need to write our code below that.

Before I attempt to solve this challenge, I go ahead and run the code. The Input (stdin) should match what was shown in the Sample Input section above. I will show no output because I haven’t coded anything yet. And the Expected Output should match the Sample Output above.

Click the Run Code button and… the result is exactly as I described above. Okay, we’re ready to start coding.

Inside the main function, just below the declared and initialized variables (i, d, s), we need to first declare our variables.

Notice the variables i, d, s were declared and intialized in a single step (ex. var i = 4) but we’re declaring first and then initializing next.

Now that we’ve declared our variables, we need to initialize them with the input. We can’t explicitly assign the input to our variables and we see nothing is being passed into the main function. So that must mean we have access to it some other kind of way that we can use. We can get clues as to what to use by looking more closely at the code we shouldn’t change. Line 17 contains commented out instructions above a function:

We can use this to access the input by calling the readLine function. We can first use console log to test what it will look like. So I put the call inside the console log, right below my declared variables and then click Run Code to see the result:

My output is only 12, only the int input. How do I get the double and string input? I’ll add another console log with the function call to see if I can get the rest. Remember to always click Run Code to see the results:

Great! Now I have the int and double input. I’ll add a final console log to get the string input.

Yes! I have access to all of my data by calling the readLine function consecutively. So now I can assign my variables consecutively and then console log my variables to see if they contain the input:

Yes!! My assignment worked so my variables have been declared and initialized. Now I need to use the + operator to perform operations on the i, d, s variables and mine and console log the results:

Uh oh! My results are not what I expected. My string result is the only correct output. It has concatenated the two variables. Come to think of it, the other two results also look like concatenation instead of a sum. Why?

Remember I stated “To ensure the computer can identify and accurately process data types, they must be written and handled in specific ways.” I suspect my data types may not be what I assumed. To find out for sure, I’ll ask my data types what they are by using typeof and console log the results. To clarify, I’ll add a string to identify what each result is:

Just as I suspected! My intVariable and doubleVariable are not numbers; they’re actually strings. Since they are strings, the computer treated them as such and concatenated their results.

I’ll use parseInt to change my intVariable into an int and parseFloat to change my doubleVariable into a double. I’ll do this where they were initialized when I assigned them and console log the result:

Alright! My intVariable and doubleVariable variables are numbers now.

So I’ll use my + operator again. And I’ll use toFixed(1) to ensure my double variables are summed to one decimal point:

Success! We finally passed the Sample Test.

Now click Submit Code to see if we passed the hidden tests.

Day 1 of 30 Days of Code conquered!

This challenge is rated Easy for difficulty level. However, for a beginner, this can be quite challenging. We see how significant console log was to help us see what was going on in the code so we could analyze the output data. Then we had to have knowledge of how to properly address issues. We also learned how to accurately handle the integer, double, and string data types so the computer could accurately process the results.

I hope this demonstrated how a programmer approaches problem solving like a detective. It’s important to see what your code outputs so you can observe what your code is doing. Then you have to analyze the cause of any issues, think of a solution, and be knowledgeable of the code necessary to achieve the solution. This requires a lot of Googling and a lot of trial and error. And above all else, a lot of patience.

--

--