Compute clock hand angle with seconds and milliseconds
Company: Scale AI
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates understanding of angular geometry and numeric precision in time-based systems, testing competency in parsing varying time formats and computing continuous clock-hand positions; it is in the Coding & Algorithms domain and requires both conceptual understanding of angular motion and practical application in precise computation. Interviewers commonly ask this problem to assess an applicant's ability to model continuous motion, handle 12/24-hour representations and edge cases, and manage seconds- and milliseconds-level precision when deriving and implementing numerical formulas.
Part 1: Compute Clock Hand Angle for HH:MM
Constraints
- time is a valid string in the format "HH:MM"
- 0 <= HH <= 23
- 0 <= MM <= 59
Examples
Input: "00:00"
Expected Output: 0.0
Explanation: Both hands are at 12, so the angle is 0.
Input: "03:00"
Expected Output: 90.0
Explanation: The hour hand is at 3 and the minute hand is at 12.
Input: "12:30"
Expected Output: 165.0
Explanation: At 12:30, the hour hand is 15 degrees from 12 and the minute hand is at 180 degrees.
Input: "23:59"
Expected Output: 5.5
Explanation: Near midnight, the hands are very close; the minimal angle is 5.5 degrees.
Solution
def solution(time):
from fractions import Fraction
hh_str, mm_str = time.split(":")
hh = int(hh_str)
mm = int(mm_str)
hour_angle = Fraction((hh % 12) * 30, 1) + Fraction(mm, 2)
minute_angle = Fraction(mm * 6, 1)
diff = abs(hour_angle - minute_angle)
answer = min(diff, Fraction(360, 1) - diff)
return float(answer)
Time complexity: O(1). Space complexity: O(1).
Hints
- On a 12-hour clock, convert the hour with HH % 12 before computing the hour hand position.
- The hour hand moves continuously, so each minute shifts it by 0.5 degrees.
Part 2: Compute Clock Hand Angle for HH:MM:SS
Constraints
- time is a valid string in the format "HH:MM:SS"
- 0 <= HH <= 23
- 0 <= MM <= 59
- 0 <= SS <= 59
Examples
Input: "00:00:00"
Expected Output: 0.0
Explanation: Both hands are at 12.
Input: "03:00:00"
Expected Output: 90.0
Explanation: This is the same as 3:00 exactly.
Input: "12:15:30"
Expected Output: 85.25
Explanation: The second component shifts both hands slightly from their HH:MM positions.
Input: "23:59:54"
Expected Output: 0.55
Explanation: Very close to midnight, the hands are only 0.55 degrees apart.
Solution
def solution(time):
from fractions import Fraction
hh_str, mm_str, ss_str = time.split(":")
hh = int(hh_str)
mm = int(mm_str)
ss = int(ss_str)
hour_angle = (
Fraction((hh % 12) * 30, 1)
+ Fraction(mm, 2)
+ Fraction(ss, 120)
)
minute_angle = Fraction(mm * 6, 1) + Fraction(ss, 10)
diff = abs(hour_angle - minute_angle)
answer = min(diff, Fraction(360, 1) - diff)
return float(answer)
Time complexity: O(1). Space complexity: O(1).
Hints
- The minute hand is no longer exactly on a minute mark; each second moves it by 0.1 degrees.
- The hour hand also moves during the current minute and second, so include both MM and SS in its angle.
Part 3: Compute Clock Hand Angle for HH:MM:SS.mmm
Constraints
- time is a valid string in the format "HH:MM:SS.mmm"
- 0 <= HH <= 23
- 0 <= MM <= 59
- 0 <= SS <= 59
- 0 <= mmm <= 999
Examples
Input: "00:00:00.000"
Expected Output: 0.0
Explanation: Both hands are exactly at 12.
Input: "03:00:00.000"
Expected Output: 90.0
Explanation: Exactly 3 o'clock gives a right angle.
Input: "12:15:30.600"
Expected Output: 85.305
Explanation: Milliseconds make a small additional shift beyond HH:MM:SS.
Input: "23:59:54.600"
Expected Output: 0.495
Explanation: Near midnight, the minimal angle is less than half a degree.
Solution
def solution(time):
from fractions import Fraction
hh_str, mm_str, sec_ms_str = time.split(":")
ss_str, ms_str = sec_ms_str.split(".")
hh = int(hh_str)
mm = int(mm_str)
ss = int(ss_str)
ms = int(ms_str)
hour_angle = (
Fraction((hh % 12) * 30, 1)
+ Fraction(mm, 2)
+ Fraction(ss, 120)
+ Fraction(ms, 120000)
)
minute_angle = (
Fraction(mm * 6, 1)
+ Fraction(ss, 10)
+ Fraction(ms, 10000)
)
diff = abs(hour_angle - minute_angle)
answer = min(diff, Fraction(360, 1) - diff)
return float(answer)
Time complexity: O(1). Space complexity: O(1).
Hints
- Split the input into hours, minutes, and a final seconds.milliseconds part, then split that last part again.
- Milliseconds affect both hands, but by different amounts: 1/120000 degree for the hour hand and 1/10000 degree for the minute hand.