Hacker Rank Introduction (Part 1)

Hacker Rank Introduction (Part 1)

Hi This is Jeremy Kruer. Today I am going to talk to you about the free website that I have been using to help me learn F# and also to become a better F# Developer. I found out about this website from an article I read on Hacker News. A guy wanted to change careers and get into software development. He signed up for an online boot camp and the boot camp used this website to teach Java development. The student really got into challenges and contests that this website offers and soon became one of the top ranked Java Developers on the website. An investment bank saw this guys high ranking in the Java domain and offered him a job. This article got me interested and wanted to find out more about the website. The website that I am taking about is https://www.hackerrank.com/.


In order to use Hacker Rank you have to create an account, but the account is completely free. The idea behind the website is that it has different challenges and contests that you can sign up for. Each time you complete a challenge or a contest you earn points and you are ranked against other developers.

There is also a Job aspect to the website but I am not very familiar with that part of the website. Companies use the website as a recruiting pool of talented developers. You can either apply directly to companies or companies can view leaders and reach out to them for open job positions.


I can't speak to the recruiting or job application part of the website because I don't have any experience with it but I do have experience with the challenges. When you sign up they have a lot of different challenges that are broken up into different domains. Some examples of the different domains are:

  • Tutorials
  • Algorithms
  • Data Structures
  • Artificial Intelligence
  • Functional Programming

They also have some language specific domains such as:

  • Java
  • Python
  • Ruby
  • C++

Because I am interesting in improving my F# skills and F# is a functional programming language, the domain that I am most focused on is the Functional Programming domain. You can use F# in other domains such as Tutorials and Algorithms but it makes sense to me to focus on the Functional Programming domain for right now.

The challenges are always available and you can complete them at any time. When you complete a challenge you are awarded points for correct solutions and then developers are ranked based on who has the most points.


Another area that Hacker Rank has is the contests. Contests have a specific start and end date and you must sign up to be able to compete in one. Once you complete a challenge, the code is rated and the developers are ranked against each other based on how well their code was rated.

I have signed up for 2 challenges, but they have not started yet so I can't give you much more details than that. In a future post, I will go into more details about the contests once I have some experience with them.


As you complete different challenges and contests, you earn points in the different domains. Once you earn enough points in a specific domain you get ranked against all of the other developers within that specific domain. The more points you earn, the higher you get ranked.

As you can see, I am currently ranked in the 36th Percentile within the Functional Languages domain. This isn't a very good rank, but I have only been using the website for a couple of weeks. As I complete more contests and challenges, my rank should improve.

Leader Board

You can also go to the Leader Board area, select a specific domain and then see who the highest ranked developers are within that domain. You can filter the results by Country, Company, or School.

Functional Programming Challenges

I'm going to walk through the first couple Functional Programming challenges which will help teach some of the basics of the F# programming language.

Solve Me First FP

In the Solve Me First FP Challenge you are asked to accept to integers as inputs from the console and then write out their sum to the console. This is a pretty basic challenge but it is a good place to start especially if you are new to F# or new to HackerRank.

Open up Visual Studio Code and create a new script file named SolveMeFirstFP.fsx. Copy and paste the following code into your script file:

open System

let main () =  
    let a = Console.ReadLine() |> int
    let b = Console.ReadLine() |> int
    printfn "%i" (a + b)

main ()  

Line #1 open System

This opens the System namespace within the .Net Framework. The equivalent code in C# would be using System;.

Line #3 let main () =

This defines a new function called main and it accepts no parameters. The equivalent code in C# would be public void Main ().

Lines #4-6

    let a = Console.ReadLine() |> int
    let b = Console.ReadLine() |> int
    printfn "%i" (a + b)

These are the body of the main function. F# does not use curly braces {} to wrap a function, instead it relies on indentation to define the scope of a function.

Line #4 let a = Console.ReadLine() |> int

This does the following:

  1. Reads input from the Console (string)
  2. Converts it to an integer
  3. Declares a variable a and stores the integer in that variable

The equivalant C# code would be:

var a = Convert.ToInt32(Console.ReadLine());  

Line #5 let b = Console.ReadLine() |> int

This does the same thing as line #4 but stores it in a variable called b.

|> (Pipe) Operator

If you have not used F# before, you are probably not familiar with the pipe operator (|>). It has quickly become one of the F# features that I use most and something that I regularly wish existed in C#. If you hover over the pipe operator in Visual Studio Code you will get the following definition: arg:'T1 -> func('T1 -> 'U) -> 'U. What this means is that it takes an anonymous type 'T1 and passes it to a function that accepts an anonymous type 'T1 and returns an anonymous type 'U.

So how exactly is this used?

The int function takes an anonymous type and returns an integer. The Console.ReadLine() function returns a string. So the |> operator takes the string from the Console.ReadLine() function and passes it to the int function which returns an integer. Another way you could think about it in C# code would be this:

var tmpString = Console.ReadLine();  
var a = Convert.ToInt32(tmpString);  

By using the |> operator in F#, it eliminates the temporary variable while still making the code easy to read and follow along with.

Line #6 printfn "%i" (a + b)

Adds a and b together and prints the results to the console. The equivalent code in C# would be Console.WriteLine(a + b);. Because F# runs on the same .Net runtime as C#, you can use the Console.WriteLine function within F# but it is not the preferred way of doing it. The more idiomatic way to do this within F# is to use the printfn function.

printfn takes a string with place holders as the first parameter and then a variable for each place holder within the string. The placeholders within the first string define what type of variable will be output: * "%i" - integer * "%s" - string * "%A" - Array * "%f" - float * "%b" - bool

As you can see, this is similar to String.Format within C#, but the big difference is that printfn is statically type checked for both the types of the parameters as well as the numbers. This means that this code will give a compiler error printfn "%s" 1 because the "%s" is specifying a string but you are passing an integer.

Line 8 main ()

This calls/executes the main function. The () type is called a Unit within F# and is most closely related to the void type within C#. In this scenario it just means that we are calling the main function and are not passing any parameters to it.


At this point I could copy and paste the code and submit it to the HackerRank website. However, first there is something that is bothering me. Lines #4 & #5 have the same code duplicated so I want to refactor this out into a common function. I am going to create two new functions. One that reads a string from the console and one that reads a string an converts the input.

Here are my 2 new functions

let readSting () = Console.ReadLine()  
let readInput convert = readSting () |> convert  

The readString function simply calls the Console.Readline () function and is probably a little bit redundant, but I will use it a lot in future challenges so I like to have it declared explicitly.

The readInput function takes advantage of the functional properties of F# by taking a convert function as a parameter. Within functional programming languages, functions are first class citizens. They can be used just like variables can; they can be passed as function parameters and even be returned as the output of another function.

With these two new functions declared, I can refactor my main function to use the new readInput function.

open System

let readSting () = Console.ReadLine()  
let readInput convert = readSting () |> convert

let main () =  
    let a = readInput int
    let b = readInput int
    printfn "%i" (a + b)

main ()  

On lines #4 & #5 I call the readInput function and pass the int function as a parameter. In future challenges you will see how I use this readInput function over and over again to accept a lot of different types of input.

Submitting a Challenge

At this point, I am ready to submit my challenge to Hacker Rank. On the Solve Me First FP page it gives sample inputs and the expected output. In this case it says if your inputs are 2 and 3 the expected output is 5.

In Visual Studio Code, select all the text in your script file Ctrl + a and hit Alt + Enter to send the code to the F# Interactive Window. Then enter 2 and Enter and then enter 3 and Enter. The program outputs 5 which is what we expect.

Copy the content of your script file. Go to the Solve Me First FP page and scroll down to the Current Buffer section. Make sure that "F#" is selected in the Language dropdown and paste your code into the buffer window.

Now click the "Run Code" button. This will run your code against one or two sample test cases but won't count as a submission.

Assuming that all sample test cases passed, you can go ahead and click the "Submit Code" button.


You solved your first Hacker Rank challenge and you earned 3 points.

In Part 2 of this post, I will walk you through the next few challenges which will build upon what you learned from this initial challenge.

Get Notified of New Posts

* indicates required

Related Article