Katkat

Compute an Overnight Alarm Interval

MetaSoftware Engineer · HR Screen

Last updated

This problem tests the ability to calculate time intervals across date rollovers by converting 12-hour clock times into a continuous 24-hour linear timeline.

Problem Statement

You are building a scheduling feature that calculates the duration of an alarm set for the next morning. Given a start time (e.g., 8:00 PM) and an end time (e.g., 9:00 AM) that occurs on the following day, calculate the total elapsed time in minutes.

Implement the function calculate_overnight_duration(start_time, end_time).

Input Format

  • start_time: A string representing a 12-hour clock time (e.g., "8:00 PM").
  • end_time: A string representing a 12-hour clock time (e.g., "9:00 AM").

Output Format

  • Return an integer representing the total minutes between the two times.

Example 1

Input: start_time = "8:00 PM", end_time = "9:00 AM" Output: 780 Explanation: 8:00 PM to 12:00 AM is 4 hours (240 minutes). 12:00 AM to 9:00 AM is 9 hours (540 minutes). 240 + 540 = 780.

Example 2

Input: start_time = "11:30 PM", end_time = "1:15 AM" Output: 105

Constraints

  • Times are given in "HH:MM AM/PM" format.
  • Assume the end time always occurs on the next day.
  • You may assume valid input formats.

Hints

Hint 1

Convert both time strings into minutes elapsed since the start of their respective 12-hour cycles (e.g., 12:00 AM/PM = 0 minutes).

Hint 2

Account for the 'PM' to 'AM' rollover by treating the start time as having elapsed from a reference point (like midnight) and adding the full 12-hour or 24-hour cycle logic.

Hint 3

Think of the timeline as a continuous number line where the 'gap' is always bridged by passing through the midnight transition point.

Follow-up questions

  • How would you modify your solution to accept 24-hour clock input (e.g., "20:00" to "09:00")?
  • What if the start and end times were provided on the same day? How would the logic change?
  • How would you handle potential input errors, such as invalid times or impossible durations?