1344. Angle Between Hands of a Clock
Asked at Salesforce
Difficulty
Medium
Time
O(1)
Space
O(1)
Pattern
Math
Companies
1
Problem
You are given a time as two integers: hour (1 to 12) and minutes (0 to 59). A clock has two hands, and at that moment they point in two directions. Return the smaller of the two angles formed between the hands, measured in degrees.
Two things make this trickier than it looks. First, the hour hand does not jump from one number to the next — it moves continuously. At 3:30 the hour hand sits halfway between 3 and 4, not exactly on 3. Second, the two hands split the circle into two angles that add up to 360; you must return the smaller one, so the answer is always between 0 and 180.
Your answer is accepted if it is within 10^-5 of the exact value, so small floating-point error is fine.
Two hands, two angles, pick the small one
Constraints
- ●
1 <= hour <= 12 - ●
0 <= minutes <= 59
Examples
hour = 12, minutes = 30165hour = 3, minutes = 3075hour = 3, minutes = 157.5Asked At
1 companies| 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).
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).
Compute each hand, then take the smaller gap
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
O(1) timedef 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 runs in O(1) time and O(1) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.
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.