Interview Questions

Simple clock angle

The question is as follows:
## 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

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.

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)

mrdaniel
published this on 12/30/15 **|**

11

Maybe also mod the whole thing by 360 to be safe.

return (6 * num) % 360;

jimmergoober

commented on 06/06/16

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

Hitman1978

commented on 07/02/18

0

code pen here of working clock with javascript:
https://codepen.io/hollyeplyler/pen/pZpLLZ

heplyler

commented on 07/30/18

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!

notSure

commented on 07/14/18

0

Sorry if this is dumb, but isn't this just simple cross multiplication?
15/60 = ?/360
15 * 360 = 5400
5400 / 60 = 90.

notSure

commented on 07/14/18

-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

slavasprogis

commented on 07/12/17

Log in to submit a comment.