Skip to main content

Calculate Delayed Arrival Time - Solution & Explanation

EasyMath10 min readAsked at: Google
Practice this problem

Problem Statement

You are given a positive integer arrivalTime denoting the arrival time of a train in hours, and another positive integer delayedTime denoting the amount of delay in hours.

Return the time when the train will arrive at the station.

Note that the time in this problem is in 24-hours format.

 

Example 1:

Input: arrivalTime = 15, delayedTime = 5 
Output: 20 
Explanation: Arrival time of the train was 15:00 hours. It is delayed by 5 hours. Now it will reach at 15+5 = 20 (20:00 hours).

Example 2:

Input: arrivalTime = 13, delayedTime = 11
Output: 0
Explanation: Arrival time of the train was 13:00 hours. It is delayed by 11 hours. Now it will reach at 13+11=24 (Which is denoted by 00:00 in 24 hours format so return 0).

 

Constraints:

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

Approach Overview

Problem Overview: You are given an arrivalTime (0–23) and a delayedTime. The task is to compute the new arrival hour after the delay while keeping the result within a 24‑hour clock. If the sum exceeds 23, the clock wraps around to the beginning.

Approach 1: Simple Addition and Modulus (O(1) time, O(1) space)

The most direct solution adds the delay to the original arrival hour and applies modulo 24. The modulo operation automatically wraps values back into the valid range of a 24‑hour clock. The calculation is (arrivalTime + delayedTime) % 24. This works because modular arithmetic models cyclic systems like clocks. The approach uses constant time and space since it performs only a single arithmetic operation and returns the result.

This technique relies on basic math and modular arithmetic. Whenever a problem involves circular ranges (hours, days of week, rotating indices), modulo arithmetic provides a clean and reliable solution.

Approach 2: Alternative Calculation using Conditional Logic (O(1) time, O(1) space)

You can also compute the result using conditional checks instead of modulo. First calculate sum = arrivalTime + delayedTime. If the sum is less than 24, it is already a valid hour. If it is 24 or greater, subtract 24 to wrap the value back into the correct range. This mirrors how a physical clock resets after midnight.

This method uses straightforward arithmetic and conditional branching. It may feel more intuitive if you are thinking about the real-world behavior of clocks rather than mathematical cycles. However, the modulo version is shorter and scales better when working with other cyclic problems.

Recommended for interviews: Interviewers generally expect the modulo solution because it demonstrates comfort with cyclic calculations and produces the cleanest code. The conditional version shows you understand the underlying behavior, but the expression (arrivalTime + delayedTime) % 24 communicates the intent immediately and avoids extra branching.

Approach 1: Simple Addition and Modulus

This approach involves using simple arithmetic to add the delayed time to the arrival time and then using the modulus operator to handle the wrap-around effect that occurs after 23:00 on a 24-hour clock. By taking modulus of the sum with 24, you ensure that the resulting time stays within the 0 to 23 range, effectively resetting to 0 after 23.

In the C solution, we define a function delayedArrivalTime which takes two parameters: arrivalTime and delayedTime. The function calculates the new arrival time by adding these two values and taking the modulus with 24.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(1) since the calculation involves basic arithmetic operations.
Space Complexity: O(1) as no extra space is used beyond the inputs provided.

Try this approach in the editor →

Approach 2: Alternative Calculation using Conditional Logic

Instead of using the modulus operator directly, this approach utilizes conditional logic to determine if the calculated time exceeds 23:00. If it does, the result is adjusted by subtracting 24, effectively wrapping it back to start from 0.

In this C solution, an alternative function delayedArrivalTimeAlt is defined where conditional logic checks if the summed time exceeds 24 and adjusts accordingly. The conditional operator achieves this succinctly.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(1) as it sticks to constant time operations.
Space Complexity: O(1) given no additional structures are used.

Try this approach in the editor →

Approach 3: Default Approach

Code

Python

Java

C++

Go

TypeScript

Rust

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Simple Addition and Modulus

Time Complexity: O(1) since the calculation involves basic arithmetic operations.
Space Complexity: O(1) as no extra space is used beyond the inputs provided.

Alternative Calculation using Conditional Logic

Time Complexity: O(1) as it sticks to constant time operations.
Space Complexity: O(1) given no additional structures are used.

Default Approach

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Simple Addition and ModulusO(1)O(1)Best general solution for clock-style wraparound problems
Conditional Logic with WraparoundO(1)O(1)Useful when modulo is avoided or when explaining the clock behavior step by step

Video Solution

Leetcode | 2651. Calculate Delayed Arrival Time | Easy | Java SolutionDeveloper Docs672 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Calculate Delayed Arrival Time easy or hard?
Calculate Delayed Arrival Time is classified as an Easy problem. It tests understanding of basic arithmetic and modulo operations rather than complex algorithms or data structures.
Calculate Delayed Arrival Time Python/Java solution
In Python or Java, compute the result using (arrivalTime + delayedTime) % 24. The same logic works across C++, JavaScript, C#, and other languages because modulo arithmetic behaves consistently for this case.
How to solve Calculate Delayed Arrival Time in O(1)?
Add the delay to the arrival time and apply modulo 24 to keep the result within the 24‑hour clock range. The formula (arrivalTime + delayedTime) % 24 immediately returns the correct hour after wraparound.
What is the best approach for Calculate Delayed Arrival Time?
The best approach uses simple addition followed by modulo 24: (arrivalTime + delayedTime) % 24. This directly models the wraparound behavior of a 24‑hour clock and runs in O(1) time with O(1) space.
Is Calculate Delayed Arrival Time asked at Google/Amazon/Meta?
Problems like this appear in coding practice sets and entry‑level interview preparation. While the exact question is unlikely at major companies, the concept of modular arithmetic frequently appears in scheduling, circular arrays, and time calculations.
What data structure is used in Calculate Delayed Arrival Time?
No special data structure is required. The solution relies purely on arithmetic operations and modular math to simulate a circular 24‑hour clock.
What is the time complexity of Calculate Delayed Arrival Time?
The problem has constant time complexity O(1) because it requires only a single arithmetic calculation. Space complexity is also O(1) since no additional data structures are used.

Ready to solve this problem?

Practice Calculate Delayed Arrival Time with our built-in code editor and test cases.

Practice on FleetCode