Calculate Delayed Arrival Time

Easy
1072.

Calculate Delayed Arrival Time

Calculate Delayed Arrival Time is an easy coding problem on math, asked in Amazon, Google and TCS interviews.

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
Write your solution here. Running it against the tests needs a free account — what you type comes with you.Sign in to run
/**
 * @param {number} arrivalTime
 * @param {number} delayedTime
 * @return {number}
 */
var findDelayedArrivalTime = function(arrivalTime, delayedTime) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

Calculate Delayed Arrival Time — Problem & Solution

Calculate Delayed Arrival Time solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem on math.

Calculate Delayed Arrival Time is an easy coding problem on math, asked in Amazon, Google and TCS interviews.

  • 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

Hints for Calculate Delayed Arrival Time

  1. Hint 1

    Add the two hours together.

  2. Hint 2

    Take the result modulo 24.

  3. Hint 3

    Hour 24 is written as 0.

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

Java

public static int findDelayedArrivalTime(int arrivalTime, int delayedTime) {
    return (arrivalTime + delayedTime) % 24;
}

C++

int findDelayedArrivalTime(int arrivalTime, int delayedTime) {
    return (arrivalTime + delayedTime) % 24;
}

JavaScript

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

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

All 337 math problems · the whole catalogue