1344. Angle Between Hands of a Clock
Problem Description
This problem asks you to calculate the angle between the hour and minute hands on an analog clock given a specific time.
Given two inputs:
hour
: an integer representing the hour (0-12 format)minutes
: an integer representing the minutes (0-59)
You need to find the smaller angle formed between the hour hand and the minute hand on a clock face and return it in degrees.
Key points to understand:
- A clock is a circle with 360 degrees
- The minute hand moves 360 degrees in 60 minutes, which means it moves
6 degrees per minute
- The hour hand moves 360 degrees in 12 hours (720 minutes), which means it moves
30 degrees per hour
or0.5 degrees per minute
- The hour hand doesn't jump from hour to hour - it moves continuously. For example, at 3:30, the hour hand is halfway between 3 and 4
- Since there are two possible angles between any two clock hands (one going clockwise and one going counterclockwise), you need to return the smaller one
- The answer should be between 0 and 180 degrees
For example:
- At 3:00, the hour hand points at 3 and the minute hand points at 12, forming a 90-degree angle
- At 3:15, the minute hand is at 3 (90 degrees from 12), and the hour hand has moved slightly past 3 (at 97.5 degrees from 12), forming a 7.5-degree angle
Intuition
To find the angle between the clock hands, we need to determine where each hand is positioned on the clock face in terms of degrees from the 12 o'clock position.
Let's think about how each hand moves:
For the minute hand: It completes a full 360-degree rotation in 60 minutes. So in 1 minute, it moves 360/60 = 6 degrees
. Therefore, at minutes
minutes past the hour, the minute hand is at 6 * minutes
degrees from 12 o'clock.
For the hour hand: This is trickier because the hour hand moves continuously, not in discrete jumps. It completes a full 360-degree rotation in 12 hours (720 minutes). This means:
- In 1 hour, it moves
360/12 = 30 degrees
- In 1 minute, it moves
30/60 = 0.5 degrees
At any given time, the hour hand's position depends on both the current hour and the minutes that have passed. For example, at 3:30, the hour hand isn't pointing directly at 3; it's halfway between 3 and 4.
So the hour hand's position is: 30 * hour + 0.5 * minutes
degrees from 12 o'clock.
Once we have both positions, we calculate the absolute difference between them. However, this difference might be greater than 180 degrees. Since we want the smaller angle, if the difference is more than 180 degrees, we should take 360 - diff
instead. This gives us the angle going the "shorter way" around the clock.
The final answer is: min(diff, 360 - diff)
, ensuring we always return the smaller of the two possible angles between the hands.
Learn more about Math patterns.
Solution Approach
The implementation follows directly from our understanding of how clock hands move:
-
Calculate the hour hand position:
h = 30 * hour + 0.5 * minutes
- The base position is
30 * hour
degrees (since each hour represents 30 degrees) - We add
0.5 * minutes
to account for the continuous movement of the hour hand as minutes pass
- The base position is
-
Calculate the minute hand position:
m = 6 * minutes
- The minute hand moves 6 degrees per minute from the 12 o'clock position
-
Find the absolute difference between the two positions:
diff = abs(h - m)
- This gives us one of the two possible angles between the hands
-
Return the smaller angle:
return min(diff, 360 - diff)
- If
diff
is greater than 180 degrees, then360 - diff
gives us the smaller angle going the other way around the clock - We use
min()
to automatically select the smaller of the two angles
- If
For example, if the time is 3:15:
- Hour hand position:
h = 30 * 3 + 0.5 * 15 = 90 + 7.5 = 97.5
degrees - Minute hand position:
m = 6 * 15 = 90
degrees - Difference:
diff = abs(97.5 - 90) = 7.5
degrees - Final answer:
min(7.5, 360 - 7.5) = min(7.5, 352.5) = 7.5
degrees
The algorithm runs in O(1)
time and space complexity since we're only performing a fixed number of arithmetic operations.
Ready to land your dream job?
Unlock your dream job with a 5-minute evaluator for a personalized learning plan!
Start EvaluatorExample Walkthrough
Let's walk through the solution with the time 9:30.
Step 1: Calculate the hour hand position
- The hour hand moves 30 degrees per hour and 0.5 degrees per minute
- At 9:30:
h = 30 * 9 + 0.5 * 30 = 270 + 15 = 285
degrees from 12 o'clock
Step 2: Calculate the minute hand position
- The minute hand moves 6 degrees per minute
- At 30 minutes:
m = 6 * 30 = 180
degrees from 12 o'clock
Step 3: Find the absolute difference
diff = abs(285 - 180) = 105
degrees
Step 4: Return the smaller angle
- We have two possible angles: 105 degrees or 360 - 105 = 255 degrees
min(105, 255) = 105
degrees
Therefore, at 9:30, the angle between the hour and minute hands is 105 degrees.
Let's verify with another example: 3:15
- Hour hand:
h = 30 * 3 + 0.5 * 15 = 90 + 7.5 = 97.5
degrees - Minute hand:
m = 6 * 15 = 90
degrees - Difference:
diff = abs(97.5 - 90) = 7.5
degrees - Final answer:
min(7.5, 360 - 7.5) = 7.5
degrees
The smaller angle at 3:15 is 7.5 degrees.
Solution Implementation
1class Solution:
2 def angleClock(self, hour: int, minutes: int) -> float:
3 # Calculate hour hand position in degrees
4 # Hour hand moves 30 degrees per hour (360/12 = 30)
5 # Hour hand also moves 0.5 degrees per minute (30/60 = 0.5)
6 hour_angle = 30 * hour + 0.5 * minutes
7
8 # Calculate minute hand position in degrees
9 # Minute hand moves 6 degrees per minute (360/60 = 6)
10 minute_angle = 6 * minutes
11
12 # Calculate the absolute difference between the two hands
13 angle_difference = abs(hour_angle - minute_angle)
14
15 # Return the smaller angle between the two possible angles
16 # (the direct angle or the reflex angle)
17 return min(angle_difference, 360 - angle_difference)
18
1class Solution {
2 public double angleClock(int hour, int minutes) {
3 // Calculate the angle of the hour hand from 12 o'clock position
4 // Hour hand moves 30 degrees per hour (360° / 12 hours = 30°)
5 // Hour hand also moves 0.5 degrees per minute (30° / 60 minutes = 0.5°)
6 double hourAngle = 30.0 * hour + 0.5 * minutes;
7
8 // Calculate the angle of the minute hand from 12 o'clock position
9 // Minute hand moves 6 degrees per minute (360° / 60 minutes = 6°)
10 double minuteAngle = 6.0 * minutes;
11
12 // Calculate the absolute difference between the two angles
13 double angleDifference = Math.abs(hourAngle - minuteAngle);
14
15 // Return the smaller angle between the two possible angles
16 // The angle can be measured clockwise or counter-clockwise
17 return Math.min(angleDifference, 360.0 - angleDifference);
18 }
19}
20
1class Solution {
2public:
3 double angleClock(int hour, int minutes) {
4 // Calculate the angle of the hour hand from 12 o'clock position
5 // Hour hand moves 30 degrees per hour (360/12 = 30)
6 // Hour hand also moves 0.5 degrees per minute (30/60 = 0.5)
7 double hourAngle = 30.0 * hour + 0.5 * minutes;
8
9 // Calculate the angle of the minute hand from 12 o'clock position
10 // Minute hand moves 6 degrees per minute (360/60 = 6)
11 double minuteAngle = 6.0 * minutes;
12
13 // Calculate the absolute difference between the two angles
14 double angleDifference = abs(hourAngle - minuteAngle);
15
16 // Return the smaller angle between the two possible angles
17 // The angle could be measured clockwise or counter-clockwise
18 return min(angleDifference, 360.0 - angleDifference);
19 }
20};
21
1/**
2 * Calculate the angle between hour and minute hands on a clock
3 * @param hour - The hour value (1-12)
4 * @param minutes - The minutes value (0-59)
5 * @returns The smaller angle in degrees between the two hands
6 */
7function angleClock(hour: number, minutes: number): number {
8 // Calculate hour hand position in degrees
9 // Hour hand moves 30° per hour (360°/12 hours)
10 // Plus 0.5° per minute (30°/60 minutes)
11 const hourHandAngle: number = 30 * hour + 0.5 * minutes;
12
13 // Calculate minute hand position in degrees
14 // Minute hand moves 6° per minute (360°/60 minutes)
15 const minuteHandAngle: number = 6 * minutes;
16
17 // Calculate the absolute difference between the two angles
18 const angleDifference: number = Math.abs(hourHandAngle - minuteHandAngle);
19
20 // Return the smaller angle between the two possible angles
21 // (the direct angle or its complement to 360°)
22 return Math.min(angleDifference, 360 - angleDifference);
23}
24
Time and Space Complexity
Time Complexity: O(1)
- The algorithm performs a fixed number of arithmetic operations regardless of input values
- Calculating hour hand position:
30 * hour + 0.5 * minutes
- constant time - Calculating minute hand position:
6 * minutes
- constant time - Computing absolute difference:
abs(h - m)
- constant time - Finding minimum between two values:
min(diff, 360 - diff)
- constant time - All operations are basic arithmetic that execute in constant time
Space Complexity: O(1)
- The algorithm uses only a fixed amount of extra space
- Variables
h
,m
, anddiff
store single numeric values - No data structures that grow with input size are used
- The space requirement remains constant regardless of the input values
Learn more about how to find time and space complexity quickly.
Common Pitfalls
1. Forgetting to Account for Hour Hand's Continuous Movement
The most common mistake is treating the hour hand as if it jumps discretely from hour to hour, rather than moving continuously as minutes pass.
Incorrect approach:
hour_angle = 30 * hour # Wrong! This assumes hour hand stays at exact hour position
Correct approach:
hour_angle = 30 * hour + 0.5 * minutes # Accounts for gradual movement
For example, at 3:30, the incorrect approach would place the hour hand at exactly 90 degrees (pointing at 3), when it should actually be at 105 degrees (halfway between 3 and 4).
2. Not Handling the 12 O'Clock Position Correctly
When the hour is 12, some might incorrectly calculate the hour hand position as 360 degrees instead of 0 degrees.
Potential issue:
# If hour = 12 and minutes = 30 hour_angle = 30 * 12 + 0.5 * 30 = 360 + 15 = 375 degrees # Wrong!
Solution:
def angleClock(self, hour: int, minutes: int) -> float:
# Normalize hour to 0-11 range
hour = hour % 12
hour_angle = 30 * hour + 0.5 * minutes
minute_angle = 6 * minutes
angle_difference = abs(hour_angle - minute_angle)
return min(angle_difference, 360 - angle_difference)
3. Returning the Larger Angle Instead of the Smaller One
The problem specifically asks for the smaller angle between the two hands. Some solutions might forget to compare with the reflex angle.
Incorrect approach:
return abs(hour_angle - minute_angle) # Could return angles > 180 degrees
Correct approach:
diff = abs(hour_angle - minute_angle)
return min(diff, 360 - diff) # Always returns angle ≤ 180 degrees
For instance, if the calculated difference is 270 degrees, the correct answer should be 90 degrees (360 - 270), not 270 degrees.
Which type of traversal does breadth first search do?
Recommended Readings
Math for Technical Interviews How much math do I need to know for technical interviews The short answer is about high school level math Computer science is often associated with math and some universities even place their computer science department under the math faculty However the reality is that you
Coding Interview Patterns Your Personal Dijkstra's Algorithm to Landing Your Dream Job The goal of AlgoMonster is to help you get a job in the shortest amount of time possible in a data driven way We compiled datasets of tech interview problems and broke them down by patterns This way
Recursion Recursion is one of the most important concepts in computer science Simply speaking recursion is the process of a function calling itself Using a real life analogy imagine a scenario where you invite your friends to lunch https assets algo monster recursion jpg You first call Ben and ask
Want a Structured Path to Master System Design Too? Don’t Miss This!