Angle Between Hands of a Clock
Asked at Salesforce
Problem
Given two integers hour and minute, return the smaller angle (in degrees) formed between the hour and minute hands of a clock. This is a math problem that tests your understanding of angular relationships and modular arithmetic.
Asked At
| Company | Difficulty | |
|---|---|---|
| Salesforce | Medium | View all Salesforce questions → |
How to Think About It
Brute force: draw a clock, compute the exact position of each hand. The minute hand moves 360/60 = 6 degrees per minute. The hour hand moves 360/720 = 0.5 degrees per minute (or 30 degrees per hour plus 0.5 per minute).
Key insight: minute_angle = minute * 6. hour_angle = (hour % 12) * 30 + minute * 0.5. The angle between them is abs(hour_angle - minute_angle). Take the smaller of this and 360 - this.
Why hour moves with minutes: the hour hand does not jump between hour marks. It moves continuously. At 3:30, the hour hand is halfway between 3 and 4, not exactly at 3. So hour_angle = (hour % 12) * 30 + minute * 0.5.
The min angle formula: diff = abs(hour_angle - minute_angle). Return min(diff, 360 - diff). This ensures you always get the smaller angle (between 0 and 180 degrees).
Visual walkthrough for hour=3, minute=30:
minute_angle = 30 * 6 = 180 degrees.
hour_angle = (3 % 12) * 30 + 30 * 0.5 = 90 + 15 = 105 degrees.
diff = |180 - 105| = 75 degrees.
min(75, 360-75) = min(75, 285) = 75 degrees.
For hour=12, minute=30:
minute_angle = 180. hour_angle = 0 + 15 = 15.
diff = 165. min(165, 195) = 165.
Edge cases: midnight (0:0, angle = 0), 6:00 (angle = 180), 12:15 (angle = 82.5), overlapping hands (angle = 0).
Optimal Approach
Step 1: Compute minute_angle = minute * 6 (each minute mark is 6 degrees apart).
Step 2: Compute hour_angle = (hour % 12) * 30 + minute * 0.5 (each hour is 30 degrees, plus continuous movement).
Step 3: diff = abs(hour_angle - minute_angle).
Step 4: Return min(diff, 360 - diff).
Walkthrough with hour=3, minute=30:
- minute_angle = 30 * 6 = 180.
- hour_angle = 3 * 30 + 30 * 0.5 = 90 + 15 = 105.
- diff = |180 - 105| = 75.
- Return min(75, 285) = 75.
Walkthrough with hour=12, minute=30:
- minute_angle = 180. hour_angle = 0 + 15 = 15.
- diff = 165. Return min(165, 195) = 165.
Time: O(1) - simple arithmetic. Space: O(1).
What Trips People Up in Real Interviews
Forgetting that the hour hand moves continuously. At 3:30, the hour hand is at 105 degrees (not 90). The hour hand moves 0.5 degrees per minute.
Not taking modulo 12 for the hour. At 12:30, the hour should be treated as 0. Use hour % 12 to handle 12 o'clock correctly.
Returning the larger angle instead of the smaller one. The problem asks for the smaller angle, so return min(diff, 360 - diff).
Using integer division for hour_angle and losing the fractional part. The hour hand moves 0.5 degrees per minute, so use floating point: minute * 0.5.
Confusing degrees with radians. The problem uses degrees. Make sure your trigonometry is consistent - though this problem does not need sin/cos, just linear angle computation.
Solution Code
def angleClock(hour, minute):
minute_angle = minute * 6
hour_angle = (hour % 12) * 30 + minute * 0.5
diff = abs(hour_angle - minute_angle)
return min(diff, 360 - diff)Frequently Asked Questions
What is the Angle Between Hands of a Clock problem?
Given two integers hour and minute, return the smaller angle (in degrees) formed between the hour and minute hands of a clock. This is a math problem that tests your understanding of angular relationships and modular arithmetic.
How do you solve Angle Between Hands of a Clock?
The optimal approach is described in detail above, including step-by-step walkthroughs, complexity analysis, and solution code in Python. Scroll up to the "Optimal Approach" section.
What companies ask Angle Between Hands of a Clock?
Angle Between Hands of a Clock is asked at Salesforce. It is a medium difficulty problem.
What are common mistakes on Angle Between Hands of a Clock?
- Forgetting that the hour hand moves continuously. At 3:30, the hour hand is at 105 degrees (not 90). The hour hand moves 0.5 degrees per minute.
- Not taking modulo 12 for the hour. At 12:30, the hour should be treated as 0. Use `hour % 12` to handle 12 o'clock correctly.
- Returning the larger angle instead of the smaller one. The problem asks for the smaller angle, so return min(diff, 360 - diff).
- Using integer division for hour_angle and losing the fractional part. The hour hand moves 0.5 degrees per minute, so use floating point: `minute * 0.5`.
- Confusing degrees with radians. The problem uses degrees. Make sure your trigonometry is consistent - though this problem does not need sin/cos, just linear angle computation.