Taro Logo

Angle Between Hands of a Clock

Medium
Asked by:
Profile picture
Profile picture
Profile picture
Profile picture
+1
More companies
Profile picture
83 views

Given two numbers, hour and minutes, return the smaller angle (in degrees) formed between the hour and the minute hand.

Answers within 10-5 of the actual value will be accepted as correct.

Example 1:

Input: hour = 12, minutes = 30
Output: 165

Example 2:

Input: hour = 3, minutes = 30
Output: 75

Example 3:

Input: hour = 3, minutes = 15
Output: 7.5

Constraints:

  • 1 <= hour <= 12
  • 0 <= minutes <= 59

Solution


Clarifying Questions

When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:

  1. What are the valid ranges for the input `hour` and `minutes`? For example, is the hour in a 12-hour (1-12) or 24-hour (0-23) format?
  2. Should the output be an integer or a floating-point number to account for fractional degrees?
  3. How should I handle invalid time inputs, for example, if `minutes` is greater than 59 or `hour` is outside the expected range?
  4. The problem asks for the 'smaller' angle. What should I return if the angle is exactly 180 degrees, since the other angle would also be 180 degrees?
  5. Just to confirm, the hour hand's position is affected by the minutes, correct? For instance, at 3:30, the hour hand is halfway between the 3 and the 4.

Brute Force Solution

Approach

To find the angle between the clock hands, the brute force approach is to consider all possible minutes from the start of the clock until the given time. We would track the position of both the hour and minute hand for every single minute that passes.

Here's how the algorithm would work step-by-step:

  1. Start at the very beginning, when the clock shows 12:00.
  2. Imagine watching the clock tick forward, one minute at a time.
  3. For each minute that passes, figure out exactly where the minute hand points and where the hour hand points.
  4. Keep advancing the clock one minute at a time, recalculating the positions of both hands each time.
  5. Continue this process until the clock shows the exact hour and minute we're interested in.
  6. At that final moment, look at the two positions you've calculated for the hour and minute hands.
  7. The answer is the smaller angle between these two final positions.

Code Implementation

def angleClock(hour, minutes):
    # The hour hand moves 0.5 degrees every minute (30 degrees / 60 minutes).

    hour_hand_angle = 0.0

    # The minute hand moves 6 degrees every minute (360 degrees / 60 minutes).

    minute_hand_angle = 0.0

    current_hour_on_clock_face = 12
    current_minute_on_clock_face = 0

    # This loop simulates the clock ticking minute by minute from 12:00 to the target time.

    while not (current_hour_on_clock_face == hour % 12 and current_minute_on_clock_face == minutes):
        current_minute_on_clock_face += 1
        if current_minute_on_clock_face == 60:
            current_minute_on_clock_face = 0
            current_hour_on_clock_face += 1
            if current_hour_on_clock_face == 13:
                current_hour_on_clock_face = 1

        # We recalculate the absolute angle of each hand from the 12 o'clock position for every minute passed.

        hour_hand_angle = ((current_hour_on_clock_face % 12) + current_minute_on_clock_face / 60.0) * 30
        minute_hand_angle = current_minute_on_clock_face * 6

    # The absolute difference gives one angle; the other is the reflex angle (360 - difference).

    angle_difference = abs(hour_hand_angle - minute_hand_angle)
    return min(angle_difference, 360 - angle_difference)

Big(O) Analysis

Time Complexity
O(h * m)The brute force approach simulates a clock from the beginning. To reach a time of `h` hours and `m` minutes, we must iterate through every minute that has passed. The total number of minutes to simulate is determined by the input hour and minute values. This involves a loop that runs `h` times for the hours, and a nested loop that runs 60 times for the minutes within each hour, plus an additional loop for the final `m` minutes. The total number of operations is roughly proportional to the total minutes passed, which is 60 * h + m, simplifying to O(h * m).
Space Complexity
O(1)The algorithm tracks the position of the hour and minute hand by updating a few variables in a loop. The amount of memory used for these variables, such as the current minute hand position and the current hour hand position, is constant and does not depend on the input time. Therefore, the auxiliary space required remains the same regardless of the hour or minute value provided.

Optimal Solution

Approach

The key is to figure out the exact position of each hand independently and then find the difference between their angles. We treat the clock face as a full circle of 360 degrees and calculate the angle of the minute hand and the hour hand from the 12 o'clock position.

Here's how the algorithm would work step-by-step:

  1. First, think about the minute hand. Since a full circle is 360 degrees and there are 60 minutes, each minute represents a specific turn. We can calculate the minute hand's total angle from the top of the clock (the 12) based on the given minutes.
  2. Next, consider the hour hand. Its position depends on both the hour and the minutes. A full circle has 12 hours, so we can find a base angle for the given hour.
  3. However, the hour hand doesn't just jump from one number to the next. It moves smoothly throughout the hour. We need to add a small extra angle to its position to account for how far into the hour we are, which is determined by the minutes.
  4. Now that we have the precise angle for both the hour hand and the minute hand, measured from the same starting point (the 12), we can find the difference between these two angles.
  5. The result might be a large angle, but the question asks for the smaller angle between the two hands. If the angle we found is more than half a circle (180 degrees), we can find the smaller angle by subtracting our result from the full 360 degrees. This gives us the shortest path between the two hands.

Code Implementation

def angle_clock(hour, minutes):
    # A full circle is 360 degrees, and a clock has 60 minutes.
    # So, each minute corresponds to 360 / 60 = 6 degrees.
    minute_hand_angle = minutes * 6.0

    # The hour hand's position is affected by both the hour and the minutes.
    # Each hour is 360/12 = 30 degrees, and each minute adds 0.5 degrees (30/60).
    hour_hand_angle = (hour % 12 + minutes / 60.0) * 30.0

    angle_difference = abs(hour_hand_angle - minute_hand_angle)

    # The problem asks for the smaller angle between the hands.
    # If the calculated angle is > 180, the other side is the shorter path.
    if angle_difference > 180:
        return 360.0 - angle_difference
    else:
        return angle_difference

Big(O) Analysis

Time Complexity
O(1)The solution involves a fixed number of arithmetic calculations to determine the angles of the hour and minute hands. Specifically, it calculates the minute hand's angle, the hour hand's angle considering the effect of the minutes, finds the absolute difference, and then determines the smaller of the two possible angles. The number of operations does not depend on the input values of hour or minute, meaning the runtime remains constant regardless of the time given. Therefore, the time complexity is O(1).
Space Complexity
O(1)The algorithm calculates the angles using a fixed number of variables to store the angles for the hour and minute hands, and their difference. These variables for intermediate calculations, such as `h_angle`, `m_angle`, and the final `diff`, occupy a constant amount of memory. The space required does not change regardless of the input values for hour and minute, resulting in constant auxiliary space complexity.

Edge Cases

Input time is 12:00
How to Handle:
The hour hand calculation for 12 o'clock should correctly map to 0 degrees to prevent miscalculations.
Input time is 12:30
How to Handle:
The hour hand should be correctly calculated as halfway between 12 and 1, not just pointing at 12.
The two hands are exactly aligned (e.g., 12:00)
How to Handle:
The absolute difference between the two hand angles will be zero, which is the correct minimum angle.
The calculated angle is greater than 180 degrees
How to Handle:
The solution must return the smaller angle, calculated as 360 minus the larger angle.
The hands form a straight line (180 degrees, e.g., 6:00)
How to Handle:
The calculated angle will be exactly 180, which is its own smaller angle, so no special logic is needed.
Maximum valid inputs (hour = 12, minutes = 59)
How to Handle:
The calculation uses standard arithmetic types which will not overflow with these input constraints.
Minimum valid inputs (hour = 1, minutes = 0)
How to Handle:
The minute hand angle is correctly calculated as zero, and the hour hand is at exactly 30 degrees.
Floating point precision issues
How to Handle:
Using double-precision floating-point numbers for angle calculations maintains sufficient accuracy for the problem.