# The Clock Examiner

> Two hands on a clock - how wide is the gap?

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

Domain: Python · Difficulty: medium · Seniority: L3

## Problem

Given hour (0-23) and minute (0-59), return the smaller angle in degrees between hour and minute hands of an analog clock (in the 12-hour face; take hour mod 12). Hour hand moves continuously.

## Worked solution and explanation

### Why this problem exists in real interviews

This is a variation on the clock angle problem that tests the same **continuous position math** but at medium difficulty, expecting candidates to derive the formula without hints. It probes mathematical modeling skill and attention to the continuous movement of both hands.

---

### Break down the requirements

#### Step 1: Compute the hour hand's absolute angle

The hour hand moves 0.5 degrees per minute total. Position: `(hour % 12) * 30 + minute * 0.5`.

#### Step 2: Compute the minute hand's absolute angle

The minute hand moves 6 degrees per minute. Position: `minute * 6`.

#### Step 3: Take the absolute difference and cap at 180

The clock is circular, so the actual angle is `min(diff, 360 - diff)`.

---

### The solution

**Continuous angle calculation with circular cap**

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

> **Time and Space Complexity**
>
> **Time:** O(1). Constant arithmetic operations.
> 
> **Space:** O(1). No auxiliary data structures.

> **Interviewers Watch For**
>
> Can you derive the 0.5 degrees/minute for the hour hand from first principles? Strong candidates explain: 360 degrees / 12 hours / 60 minutes = 0.5.

> **Common Pitfall**
>
> Returning the raw difference without capping at 180. An angle of 350 degrees should be reported as 10 degrees.

---

## Common follow-up questions

- How many times per day do the hands overlap? _(Tests combinatorial reasoning: the hands overlap 22 times in 24 hours, not 24.)_
- What if you need to return the angle as a fraction to avoid floating point? _(Tests knowledge of the `fractions.Fraction` module or manual integer math.)_
- How would you find all times where the angle is exactly 90 degrees? _(Tests solving the equation algebraically and enumerating solutions.)_
- What edge case occurs at 12:00? _(Tests that `hour % 12` correctly maps 12 to 0.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/the_clock_examiner)
- [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). 100% free data engineering interview prep. Live code execution against Postgres 16, Python 3.11, and Spark sandboxes. No paywall, no premium tier, no signup gate.