HackerRank Library Fine Solution

Hello Programmers, In this post, you will learn how to solve HackerRank Library Fine Solution. This problem is a part of the HackerRank Algorithms Series.

One more thing to add, don’t straight away look for the solutions, first try to solve the problems by yourself. If you find any difficulty after trying several times, then look for the solutions. We are going to solve the HackerRank Algorithms problems using C, CPP, JAVA, PYTHON, JavaScript & SCALA Programming Languages.

You can practice and submit all HackerRank problem solutions in one place. Find a solution for other domains and Sub-domain. I.e. Hacker Rank solution for HackerRank C ProgrammingHackerRank C++ ProgrammingHackerRank Java Programming, HackerRank Python ProgrammingHackerRank Linux ShellHackerRank SQL Programming, and HackerRank 10 days of Javascript.

HackerRank Library Fine Solution
HackerRank Library Fine Solution

As you already know that this site does not contain only the Hacker Rank solutions here, you can also find the solution for other problems. I.e. Web Technology, Data StructuresRDBMS ProgramsJava Programs Solutions,  Fiverr Skills Test answersGoogle Course AnswersLinkedin Assessment, and Coursera Quiz Answers.

HackerRank Library Fine


Your local library needs your help! Given the expected and actual return dates for a library book, create a program that calculates the fine (if any). The fee structure is as follows:

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

Charges are based only on the least precise measure of lateness. For example, whether a book is due January 1, 2017 or December 31, 2017, if it is returned January 1, 2018, that is a year late and the fine would be 10,000 Hackos.

d1m1y1 = 14, 7, 2018
d2, m2y2 = 5, 7, 2018

The first values are the return date and the second are the due date. The years are the same and the months are the same. The book is 14 – 5 = 9 days late. Return 9 * 15 = 135.

Function Description

Complete the libraryFine function in the editor below.

libraryFine has the following parameter(s):

  • d1, m1, y1: returned date day, month and year, each an integer
  • d2, m2, y2: due date day, month and year, each an integer


  • int: the amount of the fine or 0 if there is none

Input Format

The first line contains 3 space-separated integers, d1, m1, y1, denoting the respective daymonth, and year on which the book was returned.
The second line contains 3 spaceseparated integers, d2, m2, y2, denoting the respective daymonth, and year on which the book was due to be returned.


  • 1 <= d1, d2 <= 31
  • 1 <= m1, m2 <= 12
  • 1 <= y1, y2 <= 3000
  • It is guaranteed that the dates will be valid Gregorian calendar dates.

Sample Input

9 6 2015
6 6 2015

Sample Output



Given the following dates:
Returned: d1 = 9, m1 = 6, y1 = 2015
Due: d2 = 6, m2 = 6, y2 = 2015

Because y2 = y1, we know it is less than a year late.
Because m2 = m1, we know it’s less than a month late.
Because d2 < d1, we know that it was returned late (but still within the same month and year).

Per the library’s fee structure, we know that our fine will be 15 Hackos x (# days late). We then print the result of 15 x (d1 – d2) = 15 x (9 – 6) = 45 as our output.

HackerRank Library Fine Solution

Library Fine Solution in C

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {

    /* Enter your code here. Read input from STDIN. Print output to STDOUT */    
    int edd, emm, eyyyy, add, amm, ayyyy;
    int fine =0;
    scanf("%d%d%d%d%d%d", &add, &amm, &ayyyy, &edd, &emm, &eyyyy);
    if(ayyyy < eyyyy){
        fine = 0;    
    else if(ayyyy > eyyyy){
        fine = 10000;
    else if(ayyyy == eyyyy){
        if(amm < emm){
            fine = 0;
        else if(amm > emm){
            fine = 500 * (amm - emm);
        else if(amm == emm){
            if(add <= edd){
                fine = 0;
            else if(add > edd){
                fine = 15 * (add - edd);
    printf("%d", fine);
    return 0;

Library Fine Solution in Cpp

#include <bits/stdc++.h>
using namespace std;
int a[1000][1000];
int main()
    int n,i,j,k,d1,d2,m1,m2,y1,y2;

    if((y2-y1>0)|| (y2==y1 && (m2-m1)>0) || (y2==y1 && m2==m1 && d2>=d1))

    else if(y1!=y2)
    else if(m1!=m2)
    else if(d1!=d2)
    return 0;

Library Fine Solution in Java

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int actualDay = sc.nextInt();
        int actualMonth = sc.nextInt();
        int actualYear = sc.nextInt();
        int expectedDay = sc.nextInt();
        int expectedMonth = sc.nextInt();
        int expectedYear = sc.nextInt();        
        System.out.println(calcFine(actualDay, actualMonth, actualYear, expectedDay, expectedMonth, expectedYear));
    private static int calcFine(int actualDay, int actualMonth, int actualYear, int expectedDay, int expectedMonth, int expectedYear){
        if(actualYear < expectedYear){
            return 0;
        if(actualYear > expectedYear){
            return 10000;

        if(actualMonth < expectedMonth){
            return 0;
        if(actualMonth > expectedMonth){
            return 500 * (actualMonth - expectedMonth);
        if(actualDay < expectedDay){
            return 0;
        if(actualDay > expectedDay){
            return 15 * (actualDay - expectedDay);
        return 0;

Library Fine Solution in Python

import sys

return_date = map(int, sys.stdin.readline().split(' '))
expected_date = map(int, sys.stdin.readline().split(' '))

if return_date[2] > expected_date[2]:
    print '10000'
elif return_date[1] > expected_date[1] and return_date[2] >= expected_date[2]:
    print str((return_date[1] - expected_date[1]) * 500)
elif return_date[0] > expected_date[0] and return_date[2] >= expected_date[2] and return_date[1] >= expected_date[1]:
    print str((return_date[0] - expected_date[0]) * 15)
    print '0'

Library Fine Solution using JavaScript

function processData(input) {
    //Enter your code here
    var input = input.split('\n');
    var actual = input[0].split(' ');
    var expected = input[1].split(' ');
    var fine=0;
    if(parseInt(actual[1]) > parseInt(expected[1])  && parseInt(actual[2]) == parseInt(expected[2])){
       fine = 500 * parseInt(actual[1] - expected[1], 10);
     if(parseInt(actual[2]) > parseInt(expected[2])){
       fine = 10000;
    if(parseInt(actual[1]) == parseInt(expected[1]) && parseInt(actual[0]) > parseInt(expected[0]) ){
        fine = 15 * parseInt(actual[0] - expected[0], 10);

_input = "";
process.stdin.on("data", function (input) {
    _input += input;

process.stdin.on("end", function () {

Library Fine Solution in Scala

object Solution {

    def main(args: Array[String]) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution
        val actual = readLine.split(" ").map(_.toInt)
        val expected = readLine.split(" ").map(_.toInt)
        val difference = (actual(0) - expected(0), actual(1) - expected(1), actual(2) - expected(2))
        if (difference._3 < 0) {
        } else if (difference._3 == 0) {
            if (difference._2 < 0) {
            } else if (difference._2 == 0) {
                if (difference._1 <= 0) {
                } else {
                    println(15 * difference._1)
            } else {
                println(500 * difference._2)
        } else {

Library Fine Solution in Pascal

program library_fine;
    if aY > eY then
    else if (aM > eM) and (aY=eY) then
    else if (aD > eD) and (aY=eY) and (aM=eM) then

Disclaimer: This problem (Library Fine) is generated by HackerRank but the solution is provided by Chase2learn. This tutorial is only for Educational and Learning purposes.


1. How do you solve the first question in HackerRank?

If you want to solve the first question of Hackerrank then you have to decide which programing language you want to practice i.e C programming, Cpp Programing, or Java programming then you have to start with the first program HELLO WORLD.

2. How do I find my HackerRank ID?

You will receive an email from HackerRank to confirm your access to the ID. Once you have confirmed your email, the entry will show up as verified on the settings page. You will also have an option to “Make primary”. Click on that option. Read more

3. Does HackerRank detect cheating?

yes, HackerRank uses a powerful tool to detect plagiarism in the candidates’ submitted code. The Test report of a candidate highlights any plagiarized portions in the submitted code and helps evaluators to verify the integrity of answers provided in the Test.

4. Does HackerRank use camera?

No for coding practice Hackerrank does not use camera but for companies’ interviews code submission time Hackerrank uses the camera.

5. Should I put HackerRank certificate on resume?

These certificates are useless, and you should not put them on your resume. The experience you gained from getting them is not useless. Use it to build a portfolio, and link to it on your resume. 

6. Can I retake HackerRank test?

The company which sent you the HackerRank Test invite owns your Test submissions and results. It’s their discretion to permit a reattempt for a particular Test. If you wish to retake the test, we recommend that you contact the concerned recruiter who invited you to the Test and request a re-invite. 

7. What is HackerRank?

HackerRank is a tech company that focuses on competitive programming challenges for both consumers and businesses. Developers compete by writing programs according to provided specifications. Wikipedi

Finally, we are now, in the end, I just want to conclude some important message for you

Note:- I compile all programs, if there is any case program is not working and showing an error please let me know in the comment section. If you are using adblocker, please disable adblocker because some functions of the site may not work correctly.

Please share our posts on social media platforms and also suggest to your friends to Join Our Groups. Don’t forget to subscribe. 

Sharing Is Caring

Leave a Comment