Magicsheet logo

Minimum Time Difference

Medium
65.2%
Updated 6/1/2025

Minimum Time Difference

What is this problem about?

The Minimum Time Difference problem gives you a list of time points in "HH:MM" format. You need to find the minimum difference (in minutes) between any two time points in the list. Since time is circular (the day wraps around after 23:59), you must also consider the wrap-around difference. This Minimum Time Difference coding problem tests string parsing, sorting, and circular interval reasoning.

Why is this asked in interviews?

Companies like Microsoft, Meta, Amazon, Google, Bloomberg, and Capital One ask this because it combines three practical skills: parsing time strings, sorting numeric values, and handling circular/modular arithmetic. Real-world applications — scheduling, clocks, cron jobs — make this directly relevant. The array, math, sorting, and string interview pattern is well-represented here.

Algorithmic pattern used

Sort + circular scan. Convert each time string to total minutes since midnight (HH*60 + MM). Sort the resulting list. Compute differences between adjacent pairs. For the circular wrap-around, add 1440 - last + first (since a day has 1440 minutes). Return the minimum of all computed differences.

Example explanation

Time points: ["23:50", "00:10", "12:00"]. Convert to minutes: [1430, 10, 720]. Sort: [10, 720, 1430]. Differences:

  • 720 - 10 = 710.
  • 1430 - 720 = 710.
  • Circular: 1440 - 1430 + 10 = 20. Minimum difference = 20 minutes.

Common mistakes candidates make

  • Forgetting the circular wrap-around difference between the last and first sorted values.
  • Parsing hours and minutes incorrectly (e.g., using split or substring incorrectly).
  • Not sorting before computing differences.
  • Returning minutes directly without checking the circular gap.

Interview preparation tip

Circular time problems always require checking the wrap-around gap. A clean formula: after sorting, the circular gap is 1440 - sortedList[last] + sortedList[0]. Practice parsing "HH:MM" strings fluently — convert to integer minutes immediately and never work with raw strings in computations. This problem also has a bucket sort O(1440) variant: mark all minute values in a boolean array of size 1440 and scan for the minimum gap — a useful optimization to mention in interviews.

Similar Questions