Calculate Minimal Clock Hand Separation Angle
Math & Bit ManipulationAuto-graded · 26 test casesLast updated
Parse 24-hour timestamp strings (including optional seconds and milliseconds) and calculate the minor angle between analog clock hands.
Given a timestamp string representing a time on a 24-hour clock, determine the smallest non-negative angle (in degrees) between the hour hand and minute hand on a standard 12-hour analog clock.
The input time string will be formatted in one of three ways:
"HH:MM""HH:MM:SS""HH:MM:SS.mmm"
Where:
HHis an integer hours value in[0, 23].MMis an integer minutes value in[0, 59].SSis an integer seconds value in[0, 59].mmmis an integer milliseconds value in[0, 999].
Both hands move continuously and smoothly as time passes:
- The minute hand completes a full $360^\circ$ revolution every 60 minutes ($6^\circ$ per minute).
- The hour hand completes a full $360^\circ$ revolution every 12 hours ($30^\circ$ per hour, or $0.5^\circ$ per minute).
- Seconds and milliseconds advance both hands accordingly.
Return the smaller of the two angles formed between the two hands as a floating-point value (a number in the range $[0.0, 180.0]$).
Constraints
timematches one of the valid string formats described above.- Hours, minutes, seconds, and milliseconds are within their standard non-negative ranges.
Examples
Example 1
Input: clock_hand_angle('00:00')
Output: 0
Example 2
Input: clock_hand_angle('03:00')
Output: 90
Example 3
Input: clock_hand_angle('12:30')
Output: 165
Example 4
Input: clock_hand_angle('23:59:59')
Output: 0.09166666666664014
Example 5
Input: clock_hand_angle('23:59:59.999')
Output: 0.00009166666660576084
Hints
Hint 1
Convert the 24-hour format into a 12-hour analog dial by taking `hours % 12`.
Hint 2
Express the elapsed time into total sub-units (e.g., total fractional minutes or total milliseconds) to easily calculate absolute hand angles relative to the 12 o'clock position.
Hint 3
The hour hand moves at $30^\circ / 3600000$ degrees per millisecond, while the minute hand moves at $6^\circ / 60000$ degrees per millisecond. Find the absolute angle difference and return `min(diff, 360 - diff)`.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
To calculate the angle between the hour and minute hands:
- Parse the Input String: Split the input string
timeon:delimiters. Extracthourandminute. If present, extractsecondandmillisecondfrom the third component. - 12-Hour Representation: Convert 24-hour hours into 12-hour dial positions using
hour % 12. - Continuous Fractional Motion: Compute total continuous units elapsed:
- Fractional hour: $\text{hour_12} + \frac{\text{minute}}{60} + \frac{\text{second}}{3600} + \frac{\text{millisecond}}{3,600,000}$
- Fractional minute: $\text{minute} + \frac{\text{second}}{60} + \frac{\text{millisecond}}{60,000}$
- Calculate Angular Positions:
- Hour hand angle from 12 o'clock: $\text{fractional_hour} \times 30^\circ$
- Minute hand angle from 12 o'clock: $\text{fractional_minute} \times 6^\circ$
- Determine Minimal Separation:
- Compute the absolute difference: $\text{diff} = |\text{hour_angle} - \text{minute_angle}|$
- Compute the minor arc angle: $\min(\text{diff}, 360^\circ - \text{diff})$
Complexity Analysis
- Time Complexity: $\mathcal{O}(1)$ — String splitting and fixed mathematical operations execute in constant time.
- Space Complexity: $\mathcal{O}(1)$ — Uses a constant number of auxiliary variables.
def clock_hand_angle(time: str) -> float:
parts = time.split(":")
hour = int(parts[0])
minute = int(parts[1])
second = 0
millisecond = 0
if len(parts) == 3:
second_part = parts[2]
if "." in second_part:
second_text, millisecond_text = second_part.split(".")
second = int(second_text)
millisecond = int(millisecond_text)
else:
second = int(second_part)
fractional_hour = (
hour % 12
+ minute / 60
+ second / 3600
+ millisecond / 3600000
)
fractional_minute = minute + second / 60 + millisecond / 60000
hour_angle = fractional_hour * 30
minute_angle = fractional_minute * 6
difference = abs(hour_angle - minute_angle)
return min(difference, 360.0 - difference)