Parking Fee Calculator
Updated
Problem statement
A car park charges a car for one stay. You are given the time the car came in, entryTime, and the time it left, exitTime. Return the fee for the stay in whole dollars.
Both times are Unix timestamps in seconds, and exitTime is never earlier than entryTime. The stay lasts exitTime - entryTime seconds.
The fee rules are:
- A stay of 30 minutes (
1800seconds) or less is free. - A longer stay is billed from the moment the car came in, so the free 30 minutes are not taken off first. Every hour costs $3, and a part of an hour counts as a whole hour. For example, 45 minutes costs $3 and 1 hour 30 minutes costs $6.
- One day costs at most $24.
For this exercise, a day is each full 24 hours counted from entryTime, not a calendar date, so passing midnight changes nothing. Each full 24 hours costs $24. The time left after the full days is billed at $3 per hour or part of an hour, up to $24. The free 30 minutes count only once for the whole stay, so any time left after a full day is billed even when it is short.
Function
calculateParkingFee(entryTime: long, exitTime: long) → longExamples
Example 1
entryTime = 1793700000exitTime = 1793701800return = 0The car stays exactly 30 minutes, which is still inside the free half hour, so the fee is 0.
Example 2
entryTime = 1793700000exitTime = 1793702700return = 3The stay is longer than 30 minutes, so the whole 45 minutes is billed. 45 minutes is part of one hour, so the fee is 3. The free half hour is not taken off first.
Example 3
entryTime = 1793700000exitTime = 1793705400return = 61 hour 30 minutes starts a second hour, so it is billed as 2 hours: 2 * 3 = 6.
Example 4
entryTime = 1793700000exitTime = 1793782800return = 2423 hours would cost 23 * 3 = 69, but one day costs at most 24.
Example 5
entryTime = 1793700000exitTime = 1793794200return = 33The first full 24 hours cost 24. The 2 hours 10 minutes left over start 3 hours, so they cost 3 * 3 = 9. The total is 24 + 9 = 33.
Constraints
0 <= entryTime <= exitTime <= 10^10
Similar questions at other companies
- Automation Pipeline, Part 1: Single WorkerPlaid · PHONE SCREENPlaid interview questions
- Subtract Two Large Number StringsStackAdapt · PHONE SCREENStackAdapt interview questions
- Fair Coin from a Biased CoinAnthropic · PHONE SCREENAnthropic interview questions
- Three Points in a Semicircle: Monte Carlo EstimateSquarepoint Capital · PHONE SCREENSquarepoint Capital interview questions
- Apply Affine Array TransformationsByteDance · PHONE SCREENByteDance interview questions
- Exponential Backoff DelaysWonderful · PHONE SCREEN
- Add Two Integers Given as StringsJane Street · PHONE SCREENJane Street interview questions
- Fizz and Buzz Words From 1 to nDocuSign · PHONE SCREENDocuSign interview questions
- Recover Document Hierarchy from IndentationReducto · PHONE SCREEN
- Smaller Clock Hand AngleAmazon · PHONE SCREEN