Skip to main content

1344. Angle Between Hands of a Clock

medium

Compute the smaller angle (in degrees) between the hour and minute hands of a clock. Pure geometry — but a classic gotcha when forgetting that the hour hand moves continuously with the minutes.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Problem

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.

Constraints

  • 1 <= hour <= 12
  • 0 <= minutes <= 59

Examples

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

Example 4

Input
hour = 4, minutes = 50
Output
155

Example 5

Input
hour = 12, minutes = 0
Output
0

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

Hints

Progressive — try the first before opening the next.

Hint 1

Minute hand: 360 degrees per 60 minutes = 6 degrees per minute.

Hint 2

Hour hand: 360 degrees per 12 hours = 30 degrees per hour PLUS 0.5 degrees per minute (continuous motion as minutes pass).

Hint 3

Compute both angles relative to 12 o'clock; the absolute difference is one of the two angles between the hands.

Hint 4

Take min(diff, 360 - diff) to get the smaller angle. Watch the hour == 12 case: treat as 0.

Solution approach

Reveal approach

Compute each hand's angle measured clockwise from 12 o'clock. Minute hand: minutes * 6. Hour hand: (hour % 12) * 30 + minutes * 0.5 — the +0.5 per minute is the bit interviewees forget. Take the absolute difference; that gives one of the two angles between the hands. Return min(diff, 360 - diff) to pick the smaller. O(1) time and space. Use floating-point arithmetic since the +0.5 per minute can produce fractional answers like 7.5 (3:15). The hour % 12 wraps 12 to 0 so 12 o'clock anchors at the top correctly.

Complexity

Time
O(1)
Space
O(1)

Related patterns

  • math
  • geometry

Related problems

Asked at

Companies reported asking this problem (sourced from public Glassdoor, Blind, and Levels.fyi interview posts).

  • Amazon
  • Bloomberg

Practice these live with InterviewChamp.AI

Drill Angle Between Hands of a Clock and Math problems under real interview conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →