Skip to main content

Library Late Fee Calculator - Solution & Explanation

EasyPremiumFree on FleetCodeArraySimulation6 min read
Practice this problem

Problem Statement

You are given an integer array daysLate where daysLate[i] indicates how many days late the ith book was returned.

The penalty is calculated as follows:

  • If daysLate[i] == 1, penalty is 1.
  • If 2 <= daysLate[i] <= 5, penalty is 2 * daysLate[i].
  • If daysLate[i] > 5, penalty is 3 * daysLate[i].

Return the total penalty for all books.

 

Example 1:

Input: daysLate = [5,1,7]

Output: 32

Explanation:

  • daysLate[0] = 5: Penalty is 2 * daysLate[0] = 2 * 5 = 10.
  • daysLate[1] = 1: Penalty is 1.
  • daysLate[2] = 7: Penalty is 3 * daysLate[2] = 3 * 7 = 21.
  • Thus, the total penalty is 10 + 1 + 21 = 32.

Example 2:

Input: daysLate = [1,1]

Output: 2

Explanation:

  • daysLate[0] = 1: Penalty is 1.
  • daysLate[1] = 1: Penalty is 1.
  • Thus, the total penalty is 1 + 1 = 2.

 

Constraints:

  • 1 <= daysLate.length <= 100
  • 1 <= daysLate[i] <= 100

Approach Overview

Problem Overview: You receive borrowing or return information for library books and must compute the total late fee according to the library's rules. The task is mainly about correctly applying the fee policy for each record and summing the result.

Approach 1: Direct Simulation (O(n) time, O(1) space)

The most natural solution is to simulate the fee calculation exactly as described. Iterate through the array of borrowing records, compute how many days each book was returned late, and apply the fee rule for that record. Each iteration performs constant work: compare dates or day counts, compute the overdue duration, and multiply by the per‑day penalty if the book is late.

This approach works well because the problem has no complicated dependencies between entries. Each record can be processed independently, which means a single pass over the input is sufficient. The time complexity is O(n) since you process each entry once, and the space complexity is O(1) because you only maintain a running total of fees.

The implementation is essentially a straightforward simulation. You replicate the real‑world rules programmatically: determine if the return date exceeds the allowed period, compute the overdue days, and accumulate the corresponding fee. This pattern shows up frequently in easy interview problems where the focus is correct rule handling rather than algorithmic optimization.

Approach 2: Structured Fee Calculation with Precomputed Rules (O(n) time, O(1) space)

If the fee policy contains multiple tiers (for example, different rates after certain day thresholds), you can encode those rules into simple condition checks or a small lookup structure. During iteration, determine the overdue duration and map it to the correct fee calculation logic.

This still processes each record exactly once, so the time complexity remains O(n). Space complexity stays O(1) because the rule set is fixed and small. The benefit of structuring the rules explicitly is cleaner code when fee policies contain several conditions or thresholds.

This version still relies on simulation of the rules but organizes the logic to make extensions easier. In production systems, fee policies often change, so separating rule logic from the main loop improves maintainability.

Recommended for interviews: The direct simulation approach is exactly what interviewers expect. It demonstrates that you can read problem constraints carefully and implement rule-based logic correctly. Mentioning that each record is independent and therefore solvable in a single pass shows good algorithmic reasoning, even though the implementation is simple.

Solution

We define a function f(x) to calculate the late fee for each book:

$ f(x) = \begin{cases} 1 & x = 1 \ 2x & 2 leq x leq 5 \ 3x & x > 5 \end{cases}

Then, for each element x in the array daysLate, we compute f(x) and sum them up to get the total late fee.

The time complexity is O(n), where n is the length of the array daysLate. The space complexity is O(1)$.

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Direct SimulationO(n)O(1)Best for straightforward rule-based problems where each record can be processed independently
Structured Fee Rules with LookupO(n)O(1)Useful when fee policies have multiple tiers or conditions and cleaner rule organization is needed

Video Solution

Easy LeetCode for Beginners: Library Late Fee Calculator in Python • LindorCodes • No views views

Frequently Asked Questions

Is Library Late Fee Calculator easy or hard?
Library Late Fee Calculator is an Easy difficulty problem. The main challenge is correctly implementing the fee rules and handling edge cases such as books returned on time or exactly on the due date.
Library Late Fee Calculator Python/Java solution
The implementation in Python or Java follows the same structure: loop through the records, compute overdue days, apply the fee rule, and accumulate the total. Since only basic arithmetic and conditional checks are used, the logic is short and runs in O(n) time.
How to solve Library Late Fee Calculator in O(n)?
Process the records in a single pass. For each entry, compute the number of overdue days, check whether the book was returned late, and add the corresponding penalty to a running total. Because each record requires constant work, the full algorithm runs in O(n) time.
What is the best approach for Library Late Fee Calculator?
The best approach is direct simulation. Iterate through the array of borrowing or return records, calculate how many days each item is overdue, and apply the fee rule. This processes each record once, giving O(n) time complexity and O(1) space complexity.
Is Library Late Fee Calculator asked at Google/Amazon/Meta?
Problems like Library Late Fee Calculator appear in coding screens at many companies as easy rule‑implementation tasks. They are common in early interview rounds where candidates must demonstrate correct handling of conditions, loops, and array traversal.
What data structure is used in Library Late Fee Calculator?
The primary data structure is an array containing borrowing or return records. The algorithm simply iterates through the array and applies simulation logic to compute overdue durations and fees.
What is the time complexity of Library Late Fee Calculator?
The optimal solution runs in O(n) time where n is the number of borrowing records. Each entry is processed exactly once to determine the overdue duration and compute the corresponding fee. Space complexity is O(1) because only a running total is stored.

Ready to solve this problem?

Practice Library Late Fee Calculator with our built-in code editor and test cases.

Practice on FleetCode