FastPrepClock Hand Angle with Subsecond Precision

Clock Hand Angle with Subsecond Precision

Scale AI logoScale AI● EasyFULLTIMEONSITE INTERVIEW
Learn

Problem statement

Given a valid time on a 12-hour analog clock, return the smaller angle in degrees between the hour hand and the minute hand.

The input time uses one of these forms:

  • HH:MM
  • HH:MM:SS
  • HH:MM:SS.mmm

Both hands move continuously. Seconds and milliseconds therefore affect both the minute-hand and hour-hand positions.

Function

smallerClockAngle(time: String) → double

Examples

Example 1

time = "03:45"return = 157.5

At 03:45, the minute hand is at 270 degrees and the hour hand is at 112.5 degrees. Their smaller separation is 157.5 degrees.

Example 2

time = "12:00:30"return = 2.75

After 30 seconds, the minute hand has moved 3 degrees and the hour hand has moved 0.25 degrees, so the smaller angle is 2.75 degrees.

Example 3

time = "06:00:00.500"return = 179.9541666667

Half a second after 06:00, both hands have moved continuously. Their smaller separation is approximately 179.9541666667 degrees.

Constraints

  • time is a valid zero-padded 12-hour clock time in one of the three documented formats.
  • 01 <= HH <= 12, 00 <= MM, SS <= 59, and 000 <= mmm <= 999.
  • Missing seconds or milliseconds are treated as 0.
  • Return a value in the closed range [0, 180]. Answers within 10^-6 of the correct value are accepted.

More Scale AI problems

See Scale AI hiring insights
public double smallerClockAngle(String time) {
    // Write your solution here
}
time"03:45"
expected157.5
Checking account…