Introduction
LeetCode 1344, Angle Between Hands of a Clock, is a classic mathematics and geometry problem frequently asked in coding interviews.
Unlike many algorithmic problems involving arrays, trees, or dynamic programming, this challenge focuses entirely on understanding how an analog clock works and converting that understanding into a simple mathematical formula.
The goal is to determine the smaller angle formed between the hour hand and the minute hand for a given time.
This problem is an excellent example of how mathematical observation can transform what appears to be a simulation problem into a constant-time solution.
Problem Link - Angle Between Hands of a Clock
Problem Statement
Given:
- An integer
hour - An integer
minutes
Return the smaller angle formed between:
- The hour hand
- The minute hand
of an analog clock.
The answer should be accurate within 10^-5.
Example 1
Input
Output
Explanation
At 12:30:
- Minute hand points at 6
- Hour hand lies halfway between 12 and 1
The smaller angle between them is:
Example 2
Input
Output
Example 3
Input
Output
Understanding Clock Mathematics
To solve this problem efficiently, we first need to understand how clock hands move.
Minute Hand Movement
A clock contains:
and
Therefore:
The minute hand moves:
Formula:
Example:
Hour Hand Movement
The clock has:
and
Therefore:
The hour hand moves:
Formula:
However, most beginners miss one important detail.
The Hour Hand Never Stays Still
At 3:30, the hour hand is not exactly at 3.
It moves continuously as minutes pass.
Since:
and
the hour hand moves:
per minute.
Therefore:
Deriving the Final Formula
Minute hand angle:
Hour hand angle:
Difference:
Substituting:
Simplifying:
This gives one angle.
But clocks always form two angles.
Choosing the Smaller Angle
Suppose:
The other angle would be:
Since the problem asks for the smaller angle:
Optimal Java Solution
Dry Run
Input
Hour Hand Angle
Minute Hand Angle
Difference
Smaller Angle
Output:
Dry Run 2
Input
Formula
Since:
Choose:
Output:
Why This Solution Is Optimal
Many beginners attempt to:
- Simulate clock positions
- Create arrays
- Use loops
None of these are required.
The entire problem can be solved using a direct mathematical formula.
No iteration is needed.
No additional data structures are needed.
Complexity Analysis
Time Complexity
Only a few arithmetic operations are performed.
Space Complexity
No extra memory is used.
Common Interview Mistakes
Mistake 1
Ignoring minute movement of the hour hand.
Wrong:
Correct:
Mistake 2
Returning the larger angle.
The problem asks for:
Always compare:
and
Mistake 3
Forgetting absolute value.
Without:
negative angles may occur.
Key Takeaways
- Minute hand moves 6° every minute.
- Hour hand moves 30° every hour.
- Hour hand also moves 0.5° every minute.
- The formula simplifies to:
- Always return the smaller of the two possible angles.
- The solution runs in O(1) time and O(1) space.
Conclusion
LeetCode 1344: Angle Between Hands of a Clock is a beautiful mathematical problem that demonstrates how understanding the underlying mechanics of a clock leads to an elegant constant-time solution.
Instead of simulating movement, we directly calculate the positions of both hands using geometry and arithmetic. This results in a clean, interview-friendly solution with optimal performance.
Problems like this highlight an important lesson in programming:
Sometimes the best algorithm is not an algorithm at all—it is mathematics.





