# Hands Apart

> Two hands. One gap. One number.

Canonical URL: <https://datadriven.io/problems/hands_apart>

Domain: Python · Difficulty: easy · Seniority: L3

## Problem

A calendar widget renders an analog clock face and needs to know how far apart the two hands sit. Given a `hour` (1 to 12) and a `minute` (0 to 59), return the smaller of the two angles in degrees between the hour and minute hands, remembering that the hour hand drifts continuously toward the next mark as the minutes pass rather than jumping on the hour.

## Worked solution and explanation

### What this is really testing

Strip the clock face away and this is a question about whether you believe the hour hand actually moves. Almost everyone nails 3:00 equals 90 degrees, writes `hour * 30`, and ships it. The candidates who pass remember that by 3:30 the hour hand has already crept a quarter of the way toward 4, so the gap is 75 degrees, not 90. Get that one detail wrong and every non-zero minute is off by up to 15 degrees, with passing visible tests hiding the bug.

> **Trick to solving**
>
> Convert each hand to an absolute position in degrees, take the absolute difference, then fold any result over 180 by comparing against 360 minus the difference. The minute hand sweeps a full 360 degrees every 60 minutes; the hour hand sweeps 360 degrees every 12 hours, which means it also creeps as the minutes tick.

---

### Break down the requirements

#### Step 1: Place the minute hand

Sixty minutes cover the whole face, so each minute is 6 degrees: `minute * 6`.

#### Step 2: Place the hour hand

Each hour mark is 30 degrees apart, and the hand drifts 0.5 degrees for every minute past the hour: `(hour % 12) * 30 + minute * 0.5`. The `% 12` is what keeps 12 o'clock at 0 rather than 360.

#### Step 3: Fold to the smaller arc

Take the absolute difference, then return `min(diff, 360 - diff)`. The face is circular, so the two hands always carve out two arcs that sum to 360; you want the smaller one.

---

### The solution

**Continuous hand positions**

```python
def clock_angle(hour: int, minute: int) -> float:
    minute_angle = minute * 6
    hour_angle = (hour % 12) * 30 + minute * 0.5
    diff = abs(hour_angle - minute_angle)
    return min(diff, 360 - diff)
```

> **Time and space**
>
> **Time:** O(1). Pure arithmetic, no iteration.
> 
> **Space:** O(1). Only scalar variables.

> **Interviewers watch for**
>
> Whether you account for the hour hand's continuous movement. Treating 3:anything as 90 degrees is the single tell that separates someone who reasoned about the physical clock from someone who pattern-matched on the hour.

> **Common pitfall**
>
> Dropping the `hour % 12`. When hour is 12 the hand sits at the 0-degree mark, not 360, and skipping the modulo leaves you with phantom angles on the noon and midnight cases.

---

## Common follow-up questions

- What if the input uses 24-hour format? _(Tests whether you apply `% 12` correctly for values like 13 or 23.)_
- At what times are the hands exactly opposite, at 180 degrees? _(Tests reverse-engineering the formula to solve for minute given a target angle.)_
- How would you handle seconds for sub-degree precision? _(Tests extending the formula: seconds add 0.1 degrees to the minute hand and about 0.0083 to the hour hand.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/hands_apart)
- [Python Interview Questions](https://datadriven.io/python-interview-questions)
- [Data Engineering Interview Prep Guide](https://datadriven.io/data-engineer-interview-prep)
- [Daily Challenge](https://datadriven.io/daily)

---

Source: DataDriven (https://datadriven.io). DataDriven is the data engineering interview community. Live code execution in SQL, Python, and Spark sandboxes. Every feature is open to every member.