Given two numbers, hour and minutes, return the smaller angle (in degrees) formed between the hour and the minute hand.
Answers within 10-5 of the actual value will be accepted as correct.
Example 1:
Input: hour = 12, minutes = 30 Output: 165
Example 2:
Input: hour = 3, minutes = 30 Output: 75
Example 3:
Input: hour = 3, minutes = 15 Output: 7.5
Constraints:
1 <= hour <= 120 <= minutes <= 59When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
To find the angle between the clock hands, the brute force approach is to consider all possible minutes from the start of the clock until the given time. We would track the position of both the hour and minute hand for every single minute that passes.
Here's how the algorithm would work step-by-step:
def angleClock(hour, minutes):
# The hour hand moves 0.5 degrees every minute (30 degrees / 60 minutes).
hour_hand_angle = 0.0
# The minute hand moves 6 degrees every minute (360 degrees / 60 minutes).
minute_hand_angle = 0.0
current_hour_on_clock_face = 12
current_minute_on_clock_face = 0
# This loop simulates the clock ticking minute by minute from 12:00 to the target time.
while not (current_hour_on_clock_face == hour % 12 and current_minute_on_clock_face == minutes):
current_minute_on_clock_face += 1
if current_minute_on_clock_face == 60:
current_minute_on_clock_face = 0
current_hour_on_clock_face += 1
if current_hour_on_clock_face == 13:
current_hour_on_clock_face = 1
# We recalculate the absolute angle of each hand from the 12 o'clock position for every minute passed.
hour_hand_angle = ((current_hour_on_clock_face % 12) + current_minute_on_clock_face / 60.0) * 30
minute_hand_angle = current_minute_on_clock_face * 6
# The absolute difference gives one angle; the other is the reflex angle (360 - difference).
angle_difference = abs(hour_hand_angle - minute_hand_angle)
return min(angle_difference, 360 - angle_difference)The key is to figure out the exact position of each hand independently and then find the difference between their angles. We treat the clock face as a full circle of 360 degrees and calculate the angle of the minute hand and the hour hand from the 12 o'clock position.
Here's how the algorithm would work step-by-step:
def angle_clock(hour, minutes):
# A full circle is 360 degrees, and a clock has 60 minutes.
# So, each minute corresponds to 360 / 60 = 6 degrees.
minute_hand_angle = minutes * 6.0
# The hour hand's position is affected by both the hour and the minutes.
# Each hour is 360/12 = 30 degrees, and each minute adds 0.5 degrees (30/60).
hour_hand_angle = (hour % 12 + minutes / 60.0) * 30.0
angle_difference = abs(hour_hand_angle - minute_hand_angle)
# The problem asks for the smaller angle between the hands.
# If the calculated angle is > 180, the other side is the shorter path.
if angle_difference > 180:
return 360.0 - angle_difference
else:
return angle_difference| Case | How to Handle |
|---|---|
| Input time is 12:00 | The hour hand calculation for 12 o'clock should correctly map to 0 degrees to prevent miscalculations. |
| Input time is 12:30 | The hour hand should be correctly calculated as halfway between 12 and 1, not just pointing at 12. |
| The two hands are exactly aligned (e.g., 12:00) | The absolute difference between the two hand angles will be zero, which is the correct minimum angle. |
| The calculated angle is greater than 180 degrees | The solution must return the smaller angle, calculated as 360 minus the larger angle. |
| The hands form a straight line (180 degrees, e.g., 6:00) | The calculated angle will be exactly 180, which is its own smaller angle, so no special logic is needed. |
| Maximum valid inputs (hour = 12, minutes = 59) | The calculation uses standard arithmetic types which will not overflow with these input constraints. |
| Minimum valid inputs (hour = 1, minutes = 0) | The minute hand angle is correctly calculated as zero, and the hour hand is at exactly 30 degrees. |
| Floating point precision issues | Using double-precision floating-point numbers for angle calculations maintains sufficient accuracy for the problem. |