Watch 5 video solutions for Number of Elapsed Seconds Between Two Times, a easy level problem. This walkthrough by Programming Live with Larry has 175 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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 == 8endTime.length == 8startTime and endTime are valid times in the format "HH:MM:SS"00 <= HH <= 2300 <= MM <= 5900 <= SS <= 59endTime is not earlier than startTimeProblem 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.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Second-by-Second Simulation | O(diff) | O(1) | Useful for understanding clock transitions and validating logic |
| Convert to Total Seconds | O(1) | O(1) | Best general solution with minimal operations |
| Parse and Normalize Strings | O(1) | O(1) | When timestamps are provided in HH:MM:SS format |