Angle Between Hands of a Clock - Problem

Imagine you're looking at an analog clock and need to determine the smaller angle between the hour and minute hands. This classic geometry problem appears frequently in coding interviews!

Given two integers hour (1-12) and minutes (0-59), calculate the smaller angle in degrees formed between the hour and minute hands.

Key Points:

  • The hour hand moves continuously (not in discrete jumps)
  • Return the smaller of the two possible angles
  • Answers within 10^-5 of the actual value are accepted

Example: At 3:15, the minute hand points at 3, and the hour hand is 1/4 of the way between 3 and 4, creating a 7.5° angle.

Input & Output

example_1.py — Python
$ Input: hour = 12, minutes = 30
Output: 165.0
💡 Note: At 12:30, minute hand is at 180° (pointing down), hour hand is at 15° (halfway between 12 and 1). Difference is 165°, which is smaller than 195°.
example_2.py — Python
$ Input: hour = 3, minutes = 30
Output: 75.0
💡 Note: At 3:30, minute hand is at 180°, hour hand is at 105° (halfway between 3 and 4). Difference is 75°.
example_3.py — Python
$ Input: hour = 3, minutes = 15
Output: 7.5
💡 Note: At 3:15, minute hand is at 90°, hour hand is at 97.5° (3×30° + 15×0.5°). The smaller angle is 7.5°.

Constraints

  • 1 ≤ hour ≤ 12
  • 0 ≤ minutes ≤ 59
  • Answers within 10-5 of the actual value will be accepted as correct

Visualization

Tap to expand
12:00Minute: 90°Hour: 97.5°7.5°12:00 (0° angle)3:15 (7.5° angle)Clock Hand Angle ExamplesFormulas:Minute Hand: minutes × 6°Hour Hand: (hour % 12) × 30° + minutes × 0.5°
Understanding the Visualization
1
Minute Hand Movement
The minute hand completes 360° in 60 minutes, moving at 6° per minute
2
Hour Hand Movement
The hour hand moves 30° per hour (360°/12) plus 0.5° per minute for continuous movement
3
Calculate Positions
Apply formulas to find exact angular positions of both hands
4
Find Smaller Angle
Take the minimum of the difference and its complement (360° - difference)
Key Takeaway
🎯 Key Insight: Clock hands move at predictable rates - use mathematical formulas instead of complex algorithms to calculate angles efficiently in O(1) time.
Asked in
Google 45 Amazon 38 Apple 32 Microsoft 28
35.2K Views
Medium Frequency
~15 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen