Amazon Interview Question
Software Engineer / Developerssome corrects
double angle_in_degree(int hours,int minutes)
{
double value = double((720+(hours%12)*60 - minutes*11)%(720))/2;
return value>180 ? 360-value: value;
}
anyways, this was my answer:
Given 'hr' hours and 'min' minutes, angle = modulus( 0.5*(60*hr-11*min) )
i think that angle must be <= 180 degree, i.e. for 1:40 -> 170 degree, but not 190.
(and also hours may be in 24h format)
Suppose 0<=hour<12, 0<=min<60
var d = Math.Abs((30*hour + min/2 - 6*min));
if(m%2==0)
return d.ToString() + " degrees";
return d.ToString() + " degrees 30 minutes";
30*hour comes from the hour that has 30 degrees.
6*min comes from minutes because a minute has 6 degrees.
min/2 comes from the fact that the hour hand is moved towards next hour. if min is even, we don't have any minutes in the result. if the min is odd, we have 30 minutes in the result...
public class Clock {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println(getAngle(2,11));
}
public static double getAngle(int hours, int mins) {
System.out.println(hours * 30.0 + 30.0*mins/60.0);
System.out.println(mins*360/60);
double angle = Math.abs((hours * 30 + 30.0*mins/60.0) - (mins*360.0/60.0));
return angle;
}
}
An hour hand takes 12hrs to cover 360 degrees
Therefore in 1hr->30 and 1min->0.5
A minute hand takes 60 mins to cover 360 degrees
Therefore 1min-> 6 degrees
So the function to calculate angle with 12 hours clock is
degree between hour and minute hand =
30h + 0.5m - 6m
The first term represents the angle covered by hour hand and the second term represents the additional coverage after 'm' minutes and third term represents the angle covered by minute hand
An hour hand takes 12hrs to cover 360 degrees
Therefore in 1hr->30 and 1min->0.5
A minute hand takes 60 mins to cover 360 degrees
Therefore 1min-> 6 degrees
So the function to calculate angle with 12 hours clock is
degree between hour and minute hand =
30h + 0.5m - 6m
The first term represents the angle covered by hour hand and the second term represents the additional coverage after 'm' minutes and third term represents the angle covered by minute hand
double angle_in_degree(int hours,int minutes)
- strezh October 04, 2010{
double value = ((720+(hours%12)*60 - minutes*11)%(720))/2;
return value>180 ? 360-value: value;
}