Goldman Sachs Interview Question
Software Engineer / Developers@2nd Anonymous
You seem to be headed in the right direction.
@Ashis
Also understand that the hour hand may not necessarily point perfectly to number markings. It may also be in between two number markings. So we also need to include an additionally small angle as well eg- Check the hour hand at time 2.30 or so. Its in between 2 & 3
Hour hand needs to progress 30 degrees in a span of 60 minutes.
So as one of the answers above Math.abs((hours*30+minutes*(30/60))-minutes*6)
Please advise
public int clock(int hours, int min){
int angle = 0;
int d_hours = (1/12*hours)+(5*hours);
if(d_hours<min){angle = min-d_hours;}
else{angle=d_hours-min;}
return angle;
}
You are right. But didn't you forget you need to times the angle.
public static double clock(int hours, int minutes){
int minAngle = 360 / 60; // 6
double p = 5.0 / 60 * minutes;
double hourAngle = 360/12; // 30
double h = hours * hourAngle + p * minAngle;
int m = minutes * minAngle;
return Math.abs(h -m - 360);
}
/*
* Goldman Sachs:
* Write a method to calculate the acute angle between a clock
*/
public class Clock{
public static void main(String args[]){
long hours = 3;
long minutes = 15;
// Angle made by the hour hand in hours and minutes
long angle_hour = 30* hours;
double angle_Min_hour = 0.5*minutes;
long mins = 6 * minutes;
System.out.println(angle_hour+":"+angle_Min_hour+":"+mins);
System.out.println("Diff:"+((angle_hour+angle_Min_hour)-mins));
}
}
example to understand the above code better:
If you look at a clock and the time is 3:15, what is the angle between the hour and the minute hands? (The answer to this is not zero!)
Hour Hand : 12 Hours = 360
1 Hour = 360/12= 30 Degrees.
3 Hours = 3*30 = 90 Degrees.
15 Min = 30/60*15 = 7.5 Degrees.
Therefore, total angle covered by Hour Hand is := 90+7.5 = 97.5
Minuted Hand: 60 Min = 360
1 Min = 6 Degree
Therefore 15 Min = 15*6 = 90 Degree
The net difference is = 97.5-90= 7.5 Degrees.
return angle?
- Anonymous October 29, 2009