Watch 5 video solutions for The Number of Full Rounds You Have Played, a medium level problem involving Math, String. This walkthrough by Competitive Somya has 742 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.Problem Overview: You receive two timestamps in HH:MM format representing when a player logged in and logged out. A game round lasts exactly 15 minutes. The task is to count how many complete 15‑minute rounds were fully played between those two times. If the logout time is earlier than the login time, the session crossed midnight and continues into the next day.
Approach 1: Convert Times to Minutes and Calculate (Time: O(1), Space: O(1))
The straightforward strategy converts both timestamps into total minutes from midnight. Parse the string, compute hours * 60 + minutes, and handle overnight sessions by adding 24 * 60 minutes when the logout time is smaller than the login time. Once both values are normalized, round the start time up to the next 15‑minute boundary and round the end time down to the previous boundary. The number of full rounds equals (endRounded - startRounded) / 15 if the result is positive.
This approach works because a valid round must start and end exactly on multiples of 15 minutes. By shifting the login time upward and the logout time downward, only fully completed rounds remain in the interval. The implementation mainly uses simple arithmetic and string parsing, which makes it easy to implement in any language. Problems like this rely heavily on math operations combined with basic string parsing.
Approach 2: Utilize Modulo for Round Calculation (Time: O(1), Space: O(1))
This version performs the same logic but uses modulo arithmetic to align timestamps to valid round boundaries. After converting both times to minutes, adjust the login time using (15 - start % 15) % 15 to move it to the next multiple of 15. Adjust the logout time using end - (end % 15) to snap it to the previous boundary. Handle the overnight case by adding 1440 minutes when the logout time occurs the next day.
Once aligned, compute the difference and divide by 15 to get the total rounds. The modulo trick removes conditional rounding logic and keeps the calculation concise. This method is often preferred in interviews because it shows comfort with arithmetic transformations and edge cases such as midnight wrap‑around.
Recommended for interviews: Interviewers typically expect the minute‑conversion strategy combined with rounding to 15‑minute boundaries. It demonstrates clean reasoning about time intervals and edge cases like crossing midnight. The modulo variant is equally optimal and often considered a cleaner implementation once you recognize that every valid round aligns to multiples of 15.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Convert Times to Minutes and Calculate | O(1) | O(1) | Best general solution. Easy to reason about and handles midnight edge cases clearly. |
| Modulo-Based Round Alignment | O(1) | O(1) | When you want concise arithmetic logic using modulo to align times to 15‑minute intervals. |