HackerRank - Solving the problem

HackerRank Library Fine solved in Java

Today I will try to solve HackerRank “Library Fine” problem. You can find Library Fine problem at HackerRank website under Algorithms > Implementation > Library Fine.

Problem description

You can read description in Library Fine problem page. Here is short summary:

Compute a fine for late book return in a local library. Several rules apply for establishing the fine:

  • If the book is returned on or before the expected return date, no fine will be charged.
  • If the book is returned after the expected return day but still within the same calendar month and year, fine = 15 x (number of days late).
  • If the book is returned after the expected return month but still within the same calendar year, fine = 500 x (number of months late).
  • If the book is returned after the calendar year in which it was expected, there is a fixed fine of 10000.

Input at the first line contains space-separated integers denoting the date when the book was actually returned. The second line contains space-separated integers denoting the date when the book was expected to be returned (due date). Dates will be in format of valid Gregorian calendar.

Solution

Solving this problem is pretty easy. Soon as you parse all variables from HackerRank you can create just simple if else solution. This is the first attempt for solution:

As you can see it is really nothing hard. Just creating simple if else and else if conditions will solve the problem.

After a while I returned to my solution and try to improve it. Here is improved and simplified version:

if – else and else if without braces

After several iterations I compressed the code with conditions just for 5 lines. During that I learned something new about Java. Putting properly intended code for if else and else if conditions (without using braces) was outputting an error on several test cases when I was testing it at the HackerRank website.

I found out that Java intended code does not automatically format for if else and else if conditions the same way like in a Python. For else if conditions braces are necessary. Short examples:

is equal to this

And that is definitely not want you want. To fix this braces are in else if condition statement necessary:

OOP solution

I tried to solve this problem either with classes. For solving Library Fine problem is possible to use at least 3 different classes which have the same methods:

Top of the last solution is interesting. Against previous solutions, same variable names are used again and again. In this case it is important to realize one interesting fact about Java and that Java passes all primitive data types by value. This means that a copy is made which cannot be modified. (On the other hand Java objects are passed by reference. Thus passing the Java objects, you’re passing an object reference, which makes it possible to modify the object’s member variables.) It is very good taste solution to use the same variable names in different context.

Conclusion

We could discuss in which way shall we deal with this simple problem. Either we can use 5 lines of if else conditions for specific problem or variety of Java 8 classes. But I guess it would be worthless to try to solve it OOP way by instantiating at least 2 new objects in memory if we can solve the problem just by 5 lines of if else conditions.