Calculate Delayed Arrival Time - Problem

You're managing a railway station's arrival board and need to calculate when delayed trains will actually arrive. Given a train's original arrival time in 24-hour format (0-23 hours) and the delay duration in hours, determine the actual arrival time.

The challenge is handling time wraparound - if a train scheduled to arrive at 22:00 is delayed by 5 hours, it will arrive at 03:00 the next day. Your task is to implement the clock arithmetic correctly.

Input: Two positive integers - arrivalTime (0-23) and delayedTime (any positive integer)

Output: An integer representing the final arrival time in 24-hour format (0-23)

Example: If a train arrives at hour 15 with a 8-hour delay, it arrives at hour (15 + 8) % 24 = 23

Input & Output

example_1.py โ€” Basic Case
$ Input: arrivalTime = 15, delayedTime = 5
โ€บ Output: 20
๐Ÿ’ก Note: Train originally arriving at 15:00 (3 PM) with 5-hour delay arrives at 20:00 (8 PM). Simple addition: 15 + 5 = 20.
example_2.py โ€” Wraparound Case
$ Input: arrivalTime = 13, delayedTime = 11
โ€บ Output: 0
๐Ÿ’ก Note: Train arriving at 13:00 (1 PM) with 11-hour delay: (13 + 11) % 24 = 24 % 24 = 0. Arrives at midnight (00:00).
example_3.py โ€” Multiple Day Delay
$ Input: arrivalTime = 18, delayedTime = 64
โ€บ Output: 10
๐Ÿ’ก Note: Large delay spans multiple days: (18 + 64) % 24 = 82 % 24 = 10. After 64 hours (2 days + 16 hours), arrives at 10:00 AM.

Constraints

  • 1 โ‰ค arrivalTime โ‰ค 24
  • 1 โ‰ค delayedTime โ‰ค 104
  • Note: arrivalTime is in 24-hour format where 1 represents 01:00, 24 represents 00:00 (midnight)

Visualization

Tap to expand
06121815+8 hours06121823โ†’(15+8) % 24 = 23Modular arithmetic handles clock wraparound automatically
Understanding the Visualization
1
Original Time
Start with the train's scheduled arrival time on a 24-hour clock
2
Add Delay
Add the delay hours to the original time - this might exceed 24
3
Apply Modulo
Use modulo 24 to wrap around the clock face and get final time
4
Result
The remainder gives us the actual arrival time in 24-hour format
Key Takeaway
๐ŸŽฏ Key Insight: Modular arithmetic with 24 automatically handles the cyclical nature of time, making complex time calculations simple and efficient.
Asked in
Google 25 Amazon 18 Microsoft 15 Meta 12
23.4K Views
Medium Frequency
~8 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen