MediumMathUpdated Sep 2026

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

minute hand: 30 × 6 = 180°hour hand: 3 × 30 + 30 × 0.5 = 105°gap = |180 − 105| = 75°other side = 360 − 75 = 285°answer = min(75, 285) = 75
At 3:30 the hands are 75° apart on one side and 285° on the other, so the answer is 75°.

Constraints

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

Examples

Example 1
Inputhour = 12, minutes = 30
Output165
WhyThe minute hand is at 180°. The hour hand is at 0° (12 o'clock) plus half of 30° because 30 minutes have passed, so 15°. The gap is 165°, and the other side is 195°, so the smaller angle is 165°.
Example 2
Inputhour = 3, minutes = 30
Output75
WhyThe minute hand is at 30 × 6 = 180°. The hour hand is at 3 × 30 = 90° plus 30 × 0.5 = 15°, so 105°. The difference is 75°, and the other side is 285°, so the answer is 75°.
Example 3
Inputhour = 3, minutes = 15
Output7.5
WhyThe minute hand is at 15 × 6 = 90°. The hour hand is at 90° plus 15 × 0.5 = 7.5°, so 97.5°. The gap is 7.5°, which is already smaller than 352.5°.

Asked At

1 companies
CompanyDifficulty
SalesforceMediumView all Salesforce questions →

How to Think About It

1

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).

2

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.

3

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.

4

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).

5

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

minute_angle = minutes × 6hour_angle = (hour % 12) × 30 + minutes × 0.diff = |hour_angle − minute_angle|return min(diff, 360 − diff)Four steps, no loops — the hour hand's 0.5° per minuteis what makes it continuous.
Each hand has a simple degree formula, and the final min() always picks the angle under 180°.
ComplexityTime O(1)Space O(1)The answer comes from a few arithmetic operations, so the cost does not depend on the input values.

What Trips People Up in Real Interviews

1

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.

2

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.

3

Returning the larger angle instead of the smaller one. The problem asks for the smaller angle, so return min(diff, 360 - diff).

4

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.

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) time
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)

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

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.