Clock Angle Between Hour and Minute Hands

viaGlassdoor

Problem: Given a time (e.g., 3:15), find the angle between the hour hand and the minute hand of an analog clock.

Constraints: Time given in hours and minutes (0-11:59).

Example: At 3:15, the minute hand points at the "3" mark (90 degrees), and the hour hand has moved a quarter of the way from 3 to 4 (an additional 7.5 degrees past the 3 mark) -> angle = 7.5 degrees.

Approach: Minute hand angle = minutes * 6 degrees (360/60). Hour hand angle = (hour % 12) * 30 + minutes * 0.5 degrees (it moves 0.5 degrees per minute). The answer is the absolute difference between the two angles, taking the smaller of the angle and 360 minus the angle.

Add a follow-up question they asked
No follow-ups yet. Be the first to add one.
asked …
LeaderboardSalary
Language
Account