PracHub
QuestionsPremiumLearningGuidesInterview PrepCoaches

Quick Overview

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.

  • medium
  • Scale AI
  • Coding & Algorithms
  • Software Engineer

Compute clock hand angle with seconds and milliseconds

Company: Scale AI

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You are given a time in the format `"HH:MM"`, representing a 24-hour clock time with hours and minutes. Extend this to potentially include seconds and milliseconds as follow-ups. 1. **Base problem** Given a string `time` in the format `"HH:MM"`, where: - `0 <= HH <= 23` - `0 <= MM <= 59` Compute the **smallest positive angle** (in degrees) between the hour hand and the minute hand on an analog clock that shows this time. The result can be a floating point number. 2. **Follow-up 1: Add seconds** Now suppose the input is `"HH:MM:SS"`, with: - `0 <= SS <= 59` Recompute the smallest angle between the hour and minute hands, taking seconds into account (the hour and minute hands move continuously, not in discrete jumps). 3. **Follow-up 2: Add milliseconds** Now suppose the input is `"HH:MM:SS.mmm"`, with: - `0 <= mmm <= 999` Recompute the smallest angle between the hour and minute hands, taking seconds and milliseconds into account. Specify the formulas you would use for the hand positions, how you handle 24-hour times on a 12-hour clock face, and then implement a function that returns the minimal angle between the two hands as a floating point value. Input: a time string in one of the above formats. Output: the smallest angle between the hour and minute hands, in degrees (a floating point number). Describe the algorithm and then implement it in your preferred programming language.

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

Given a valid 24-hour time string in the format "HH:MM", compute the smallest angle in degrees between the hour hand and the minute hand on an analog clock. Treat the clock face as a 12-hour clock, so hour 0 and hour 12 both map to the 12 o'clock position. Use these formulas: hour_angle = (HH % 12) * 30 + MM * 0.5, minute_angle = MM * 6. The answer is the smaller of diff and 360 - diff, where diff = |hour_angle - minute_angle|.

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

  1. On a 12-hour clock, convert the hour with HH % 12 before computing the hour hand position.
  2. The hour hand moves continuously, so each minute shifts it by 0.5 degrees.

Part 2: Compute Clock Hand Angle for HH:MM:SS

Given a valid 24-hour time string in the format "HH:MM:SS", compute the smallest angle in degrees between the hour hand and the minute hand on an analog clock. The hands move continuously. Convert the 24-hour value onto a 12-hour clock face using HH % 12. Use these formulas: hour_angle = (HH % 12) * 30 + MM * 0.5 + SS / 120, minute_angle = MM * 6 + SS * 0.1. Then compute diff = |hour_angle - minute_angle| and return min(diff, 360 - diff).

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

  1. The minute hand is no longer exactly on a minute mark; each second moves it by 0.1 degrees.
  2. 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

Given a valid 24-hour time string in the format "HH:MM:SS.mmm", compute the smallest angle in degrees between the hour hand and the minute hand on an analog clock. The hour and minute hands move continuously, including the effect of milliseconds. Convert the 24-hour value to a 12-hour clock face using HH % 12. Use these formulas: hour_angle = (HH % 12) * 30 + MM * 0.5 + SS / 120 + mmm / 120000, minute_angle = MM * 6 + SS * 0.1 + mmm * 0.0001. Then compute diff = |hour_angle - minute_angle| and return min(diff, 360 - diff).

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

  1. Split the input into hours, minutes, and a final seconds.milliseconds part, then split that last part again.
  2. Milliseconds affect both hands, but by different amounts: 1/120000 degree for the hour hand and 1/10000 degree for the minute hand.
Last updated: May 20, 2026

Loading coding console...

PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Implement a Dependency-Aware Task Scheduler - Scale AI (easy)
  • Schedule Ready Tasks by Deadline - Scale AI (medium)
  • Implement a Task Processor - Scale AI (medium)
  • Update a Neuron Grid - Scale AI (medium)
  • Implement multi-head attention and LLM sampling - Scale AI (easy)