Skip to main content

Number of Elapsed Seconds Between Two Times - Solution & Explanation

EasyMathString5 min read
Practice this problem

Problem Statement

You are given two valid times startTime and endTime, each represented as a string in the format "HH:MM:SS".

Return the number of seconds that have elapsed from startTime to endTime.

 

Example 1:

Input: startTime = "01:00:00", endTime = "01:00:25"

Output: 25

Explanation:

endTime is 25 seconds ahead of startTime.

Example 2:

Input: startTime = "12:34:56", endTime = "13:00:00"

Output: 1504

Explanation:

endTime is 25 minutes and 4 seconds ahead of startTime, which equals 1504 seconds.

 

Constraints:

  • startTime.length == 8
  • endTime.length == 8
  • startTime and endTime are valid times in the format "HH:MM:SS"
  • 00 <= HH <= 23
  • 00 <= MM <= 59
  • 00 <= SS <= 59
  • endTime is not earlier than startTime

Approach Overview

Problem Overview: You are given two times and need to compute how many seconds elapsed between them. The cleanest solution converts each timestamp into total seconds, then subtracts the values.

Approach 1: Increment Second-by-Second Simulation (O(diff) time, O(1) space)

You can simulate time progression from the start timestamp until it matches the end timestamp, incrementing a counter every second. This approach mirrors how clocks work internally and helps verify edge cases such as minute and hour rollover. The downside is scalability because the runtime depends directly on the number of elapsed seconds. It is mainly useful as a conceptual baseline or for validating small inputs.

Approach 2: Convert Time to Total Seconds (O(1) time, O(1) space)

Convert each time into the number of seconds since 00:00:00 using the formula hours * 3600 + minutes * 60 + seconds. Once both values are computed, subtract the earlier timestamp from the later one. This avoids loops entirely and reduces the problem to simple arithmetic operations. This is the expected optimal solution because the number of operations stays constant regardless of the input values.

The core idea relies on direct arithmetic instead of iterative traversal. Problems like this often appear under math and simulation categories because you model time mathematically rather than storing intermediate states. You only need primitive integer variables, so memory usage remains constant.

Approach 3: Parse and Normalize Time Components (O(1) time, O(1) space)

If the input arrives as formatted strings such as HH:MM:SS, first split the string into components and parse integers. After normalization, apply the same total-seconds conversion. This version is common in production systems where timestamps are serialized text instead of structured arrays or integers. The arithmetic remains identical after parsing.

Recommended for interviews: Interviewers expect the total-seconds conversion approach because it demonstrates that you can simplify a real-world process into constant-time arithmetic. Mentioning the simulation approach first shows you understand the brute-force interpretation, but deriving the direct conversion formula shows stronger problem-solving ability. Similar optimizations appear frequently in implementation problems.

Solution

Convert each time string into the number of seconds elapsed since 00:00:00, i.e. HH times 3600 + MM times 60 + SS, then return the difference between the two values.

The time complexity is O(1), and the space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Second-by-Second SimulationO(diff)O(1)Useful for understanding clock transitions and validating logic
Convert to Total SecondsO(1)O(1)Best general solution with minimal operations
Parse and Normalize StringsO(1)O(1)When timestamps are provided in HH:MM:SS format

Video Solution

3986. Number of Elapsed Seconds Between Two Times (Leetcode Easy) • Programming Live with Larry • 175 views views

Watch 4 more video solutions →

Frequently Asked Questions

Is Number of Elapsed Seconds Between Two Times easy or hard?
Number of Elapsed Seconds Between Two Times is classified as an Easy problem. The challenge focuses on careful arithmetic conversion rather than advanced algorithms or data structures. Most candidates solve it quickly once they recognize the total-seconds formula.
Number of Elapsed Seconds Between Two Times Python/Java solution
Python and Java solutions typically follow the same arithmetic formula. Parse or extract the hour, minute, and second components, compute total seconds for both timestamps, then subtract the values. Both implementations run in O(1) time and use O(1) extra space.
How to solve Number of Elapsed Seconds Between Two Times in O(1)?
Convert the start time and end time into total seconds from midnight. After conversion, subtract the start value from the end value to get the elapsed seconds. The computation uses constant-time arithmetic operations only.
What is the best approach for Number of Elapsed Seconds Between Two Times?
The best approach converts both timestamps into total seconds and subtracts them. Use the formula hours * 3600 + minutes * 60 + seconds for each time value. This runs in O(1) time and O(1) space because only a fixed number of arithmetic operations are required.
Is Number of Elapsed Seconds Between Two Times asked at Google/Amazon/Meta?
Time conversion and timestamp arithmetic problems appear frequently in screening rounds at companies like Amazon and Meta because they test implementation accuracy and edge-case handling. Variants involving date-time normalization or interval calculations are also common in backend-focused interviews.
What data structure is used in Number of Elapsed Seconds Between Two Times?
The problem does not require advanced data structures. Most solutions use integer variables to store hours, minutes, seconds, and the computed total seconds. Some implementations parse formatted strings into arrays or lists temporarily before conversion.
What is the time complexity of Number of Elapsed Seconds Between Two Times?
The optimal solution runs in O(1) time complexity and O(1) space complexity. The algorithm performs direct arithmetic conversion without iterating through the elapsed interval. A simulation-based solution would take O(diff) time where diff is the number of elapsed seconds.

Ready to solve this problem?

Practice Number of Elapsed Seconds Between Two Times with our built-in code editor and test cases.

Practice on FleetCode