Interview Questions

  • View all algorithm tutorials
  • blank
  • Simple clock angle

    Want more content about improving your career, algorithms, and interview practice? Check out our newsletter!

    The question is as follows:
    You will be given a number N that represents where the minute hand currently is on a clock. Your program should return the angle that is formed by the minute hand and the 12 o'clock mark on the clock.

    Example

    If the input is: 15 then your program should return 90 because a 90-degree angle is formed by the minute hand and the 12 o'clock mark on the clock. We'll solve this challenge by first calculating what angle is created by each minute passing on a clock. Once we calculate this number, we multiply it by the input to determine the final angle. More information regarding this problem, and how to calculate the angle between both clock hands, can be found here.

    Code

    function simpleClockAngle(num) {
    
      // we got 6 because 360/60 = 6
      // 360 represents the full number of a degrees in a circle and
      // 60 is the number of minutes on a clock, so dividing these two numbers
      // gives us the number of degrees for one minute
      return 6 * num;
    
    }
    
    simpleClockAngle(15);
    
    def simpleClockAngle(num):
    
      # we got 6 because 360/60 = 6
      # 360 represents the full number of a degrees in a circle and
      # 60 is the number of minutes on a clock, so dividing these two numbers
      # gives us the number of degrees for one minute
      return 6 * num
    
    print simpleClockAngle(15)
    
    Run Code
    Run Code
    mrdaniel published this on 12/30/15 | javascript, bootcamp, Fullstack Academy
    Comments
  • +
  • 11
  • -
  • Maybe also mod the whole thing by 360 to be safe.
    return (6 * num) % 360;
  • +
  • 1
  • -
  • Slavasprogis, to fix your problem with the left side of the clock, I've modified the code a bit:
    def simple_clock_angle(num):
        # This method returns the angle made by the minute hand and the hour hand given that the hour hand is at 12
        if num <= 30:
            return 6 * num
        else:
            return 180 - (num - 30) * 6   # This allows for measuring the angle on the top half of the hour (between 31 - 59)
    
    I hope this makes sense
  • +
  • 0
  • -
  • code pen here of working clock with javascript: https://codepen.io/hollyeplyler/pen/pZpLLZ
  • +
  • 0
  • -
  • Oh, wait. I AM dumb. There's no need to bother cross multiplying because the ranges never change so it's always going to be num * 6. Duh!
  • +
  • 0
  • -
  • Sorry if this is dumb, but isn't this just simple cross multiplication? 15/60 = ?/360 15 * 360 = 5400 5400 / 60 = 90.
  • +
  • -9
  • -
  • I think it should give the same result if we do this :
    print simpleClockAngle(15)
    or this:
    print simpleClockAngle(45)
    , but it doesn't
    Log in to submit a comment.