Library Fine | HackerRank


This is an easy hackerrank challenge which will help you to become good at competitive programming. There are various competitive programming websites like CodeChef, HackerEarth, Codeforces where you can practice coding.

Problem :

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:

• If the book is returned on or before the expected return date, no fine will be charged (i.e.: fine = 0).
• 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 * (the number of days late).
• 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 * (the 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 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.

Read full problem : Library Fine

Library Fine HackerRank

Solution :

Let integer variables return_day, return_month, return_year represents returned date and due_day, due_month and due_year represents expected return date of a book.

If return_year < due_year, then no fine will be charged.

If return_year == due_year, then we have to check for return_month and return_day since book is returned within the same calendar year.

If book is returned within the same calendar year but after expected return month (due_month) i. e. (return_year == due_year && return_month > due_month) then fine is 500 Hackos * (return_month – due_month).

If the book is returned within the same calendar year and within the same calendar month but after the expected return day i. e. (return_year == due_year && return_month == due_month && return_day > due_day) then fine is 15 Hackos * (return_day – due_day).

If the book is returned after the expected return year i. e. (return_year > due_year) then fine is 10000 Hackos.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int library_fine(int return_day, int return_month, int return_year, int due_day, int due_month, int due_year) 
{
    int fine = 0;
    if (return_year == due_year)
    {
        if (return_month == due_month)
        {
            if (return_day > due_day)
            {
                fine = 15 * (return_day - due_day);
            }
        }
        else if (return_month > due_month)
        {
            fine = 500 * (return_month - due_month);
        }
    }
    else if (return_year > due_year)
    {
        fine = 10000;
    }
    return fine;
}


C++ Implementation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>

int library_fine(int return_day, int return_month, int return_year, int due_day, int due_month, int due_year) 
{
    int fine = 0;
    if (return_year == due_year)
    {
        if (return_month == due_month)
        {
            if (return_day > due_day)
            {
                fine = 15 * (return_day - due_day);
            }
        }
        else if (return_month > due_month)
        {
            fine = 500 * (return_month - due_month);
        }
    }
    else if (return_year > due_year)
    {
        fine = 10000;
    }
    return fine;
}

int main()
{
    int return_day, return_month, return_year;
    int due_day, due_month, due_year;

    std::cin >> return_day >> return_month >> return_year;
    std::cin >> due_day >> due_month >> due_year;

    std::cout << library_fine(return_day, return_month, return_year, due_day, due_month, due_year) << "\n";
}    

View this code on Github.

If you have another solution then please share it in the comments below.

Other Competitive Programming Problems and Solutions

Sherlock and Squares
Circular Array Rotation