Calculate Delayed Arrival Time — Easy Problem & Solution

A train was scheduled to arrive at hour arrivalTime on a 24-hour clock but is delayedTime hours late.

  • Difficulty: Easy
  • Topics: Math
  • Asked at: Amazon, Google, TCS
  • Time limit: 2 s
  • Memory limit: 256 MB
  • Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby

Problem statement

A train was scheduled to arrive at hour arrivalTime on a 24-hour clock but is delayedTime hours late.

Return the hour it actually arrives, again on the 24-hour clock.

Example 1

Input: arrivalTime = 15, delayedTime = 5
Output: 20

Example 2

Input: arrivalTime = 13, delayedTime = 11
Output: 0
Explanation: 24 wraps around to 0, not 24.

Example 3

Input: arrivalTime = 0, delayedTime = 0
Output: 0

Constraints

  • 1 <= arrivaltime < 24
  • 1 <= delayedTime <= 24

How to solve Calculate Delayed Arrival Time

Add the delay to the scheduled hour and reduce modulo 24.

Approach

  1. Compute arrivalTime + delayedTime.
  2. Return that modulo 24.

Why it works

The only real content is that the 24-hour clock is the integers modulo 24 — hour 24 is hour 0, not a 25th hour. With both inputs under 25 the sum never exceeds 47, so a single modulo is exact and no loop or conditional subtraction is needed.

Complexity

  • Time — O(1)
  • Space — O(1)

Pitfalls

  • Returning 24 instead of 0 for an exact wrap.
  • Subtracting 24 once is enough here, but the modulo says so unconditionally.
  • The clock has no minutes — everything is whole hours.

Reference solution

Python

def findDelayedArrivalTime(arrivalTime: int, delayedTime: int) -> int:
    return (arrivalTime + delayedTime) % 24

JavaScript

var findDelayedArrivalTime = function(arrivalTime, delayedTime) {
    return (arrivalTime + delayedTime) % 24;
};

Also on the editorial tab: C, C#, C++, Go, Java, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.

All 213 math problems · the whole catalogue