You are participating in an online chess tournament. There is a chess round that starts every 15 minutes. The first round of the day starts at 00:00, and after every 15 minutes, a new round starts.
00:15, the fourth round starts at 00:45, and the seventh round starts at 01:30.You are given two strings loginTime and logoutTime where:
loginTime is the time you will login to the game, andlogoutTime is the time you will logout from the game.If logoutTime is earlier than loginTime, this means you have played from loginTime to midnight and from midnight to logoutTime.
Return the number of full chess rounds you have played in the tournament.
Note: All the given times follow the 24-hour clock. That means the first round of the day starts at 00:00 and the last round of the day starts at 23:45.
Example 1:
Input: loginTime = "09:31", logoutTime = "10:14" Output: 1 Explanation: You played one full round from 09:45 to 10:00. You did not play the full round from 09:30 to 09:45 because you logged in at 09:31 after it began. You did not play the full round from 10:00 to 10:15 because you logged out at 10:14 before it ended.
Example 2:
Input: loginTime = "21:30", logoutTime = "03:00" Output: 22 Explanation: You played 10 full rounds from 21:30 to 00:00 and 12 full rounds from 00:00 to 03:00. 10 + 12 = 22.
Constraints:
loginTime and logoutTime are in the format hh:mm.00 <= hh <= 2300 <= mm <= 59loginTime and logoutTime are not equal.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
We want to find out how many complete time periods fit between a start time and an end time. The brute force method involves checking every possible complete time period one by one to see if it fits.
Here's how the algorithm would work step-by-step:
def the_number_of_full_rounds_you_have_played(start_time, end_time, length_of_round):
number_of_rounds_played = 0
current_time = start_time
# Keep going as long as we can fit another round
while True:
next_time = current_time + length_of_round
# If adding another round puts us at or after the end, then exit
if next_time > end_time:
break
# Count the fact that we fit in another round
number_of_rounds_played += 1
# Set the start of the next round
current_time = next_time
return number_of_rounds_playedTo find the number of full game rounds completed within a given time interval, we focus on aligning start and end times to the nearest round boundaries. We compute how many rounds can start after the starting time, and how many rounds completed before the ending time, and subtract appropriately.
Here's how the algorithm would work step-by-step:
def numberOfRounds(startTime, finishTime):
startHour, startMinute = map(int, startTime.split(':'))
finishHour, finishMinute = map(int, finishTime.split(':'))
startTotalMinutes = startHour * 60 + startMinute
finishTotalMinutes = finishHour * 60 + finishMinute
# Handle the case where the game wraps around midnight
if finishTotalMinutes < startTotalMinutes:
finishTotalMinutes += 24 * 60
# Find the start of the first complete round after the start time.
alignedStartTime = (startTotalMinutes + 14) // 15 * 15
# Find the end of the last complete round before the finish time.
alignedFinishTime = finishTotalMinutes // 15 * 15
# Ensure aligned times are valid
if alignedStartTime > alignedFinishTime:
return 0
# Calculate and return the number of full rounds
return (alignedFinishTime - alignedStartTime) // 15| Case | How to Handle |
|---|---|
| startTime and finishTime are equal, but not at the start of a 15-minute interval (e.g., '10:07') | The number of rounds is zero if the start and end times are the same and not on a 15-minute boundary. |
| startTime and finishTime are equal and at the start of a 15-minute interval (e.g., '10:00') | The number of rounds is still zero if the start and end times are the same and on a 15-minute boundary. |
| startTime is later than finishTime (e.g., '23:50' and '00:10') | Handle wraparound by adding a day (24 hours or 1440 minutes) to finishTime when startTime is later. |
| startTime or finishTime is invalid (e.g., '25:00', '10:60') | Validate the input strings and throw an exception or return an error code if the format is invalid. |
| startTime and finishTime are very close, less than 15 minutes apart (e.g., '10:01' and '10:14') | The number of rounds is zero if the difference between the rounded start and end times is less than 15 minutes. |
| startTime is at the very end of a 15-minute interval and finishTime is at the start of an interval (e.g. 10:14 and 10:15) | No full 15-minute intervals are possible, so the result should be zero. |
| startTime is '00:00' and finishTime is '23:59' | Calculate full rounds within the 24 hour period, which is (24 * 60) / 15 - 1 because the last interval is not complete. |
| The input strings are null or empty. | Check for null or empty inputs and return 0 or throw an IllegalArgumentException accordingly. |