Amazon Interview Question
Software Engineer / Developersi can give you the algorithm how to perform.we find out the angle of minutes hand and hour hand reference point which is 12 o'clock in clockwise direction. now we find the difference between angle of both hand. if angle is smaller than 180 its is small sector else its is larger sector. and now we can see second hand. check if it lies between hour hand and minutes hand. it is in small sector. now how to find angles of hour and minutes hand.
for minutes hand. with every increase of 1 minutes 6 degree increase in angle from reference point. so angle = 6 * mm. now for hand .. we have to find exact position of hour hand.now we can find exact position of hour hand with the help of minutes hand. degree of hours hand = (hh+mm/12)* 6.
1. Identify the angles of all the hands (hour, minutes and seconds) w.r.t. reference i.e 12:00. Name the angles as h, m and s
2. Let x = |h-s| ; y = |m-s|
3. If (|x+y| == |h-m|)
a. If ( |h-m| < 180deg)
print (Smaller Section)
b. else
printf (Larger Section)
4. else
a. If ( |h-m| > 180deg) //note its greater than here
print (Smaller Section)
b. else
printf (Larger Section)
{
1. Identify the angles of all the hands (hour, minutes and seconds) w.r.t. reference i.e 12:00. Name the angles as h, m and s
2. Let x = |h-s| ; y = |m-s|
3. If (|x+y| == |h-m|)
a. If ( |h-m| < 180deg)
print (Smaller Section)
b. else
print (Larger Section)
4. else
a. If ( |h-m| > 180deg) //note its greater than here
print (Smaller Section)
b. else
print (Larger Section
}
-Reformatted
Convert Hour hand position to minutes. Assuming that all seconds/minutes are marked as 1 to 60
1::20::22 can be translated as Hour hand at --> 1 X 5 = 5 and Minute hand at 20.
Since Second hand '22' is not between 5 and 22. It is in the other segment.
Based on this you can also easily point out whether seconds hand is in the major segment or minor segment.
Float S=ss/60;
Float M=mm*60+ss/3600
Float H=hh*3600+mm*60+ss/86400;
If (abs(H-M)==0.5)
Return special case;
If(H>M)
{
If((H-M)>0.5)
{
If(S>H||S<M)
Return small;
If(S<H&&S>M)
Return large;
}
Else
{
If(S>H||S<M)
Return large;
If(S<H&&S>M)
Return small;
}
}
Else
{
If((M-H)>0.5)
{
If(S>M||S<H)
Return small;
If(S<M&&S>H)
Return large;
}
Else
{
If(S>M||S<H)
Return large;
If(S<M&&S>H)
Return small;
}
}
#include "stdafx.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
float hh=1, mm=30,ss=22;
float HrPoint = (5*hh) + (mm/12) + (ss/720);
float MinPoint = mm + ss/60;
if((MinPoint - HrPoint) < 30)
{
if(ss > HrPoint && ss < MinPoint)
cout << "SMALL";
else
cout << "BIG";
}
else
{
if(ss > HrPoint && ss < MinPoint)
cout << "BIG";
else
cout << "SMALL";
}
cout << endl;
return 0;
}
if the time is represented as hh:mm:ss in 12 hour format
then degree of the hour hand at this point is given by
hh*(360/12) + mm*(360/(60*12)) + ss*(360(/60*60*12))
A B C
The factor A represents the degree by which hour hand moves every hour
the factor B represents the degree by which hour hand moves every minute
the factor C represents the degree by which hour hand moves every second
Similarly the degree of the minute hand is represented as follows
mm*(360/(60)) + ss*(360(/60*60))
A B
The factor A represents the degree by which minute hand moves every minute
the factor B represents the degree by which minute hand moves every second
The degree of the second has is given by ss*(360/60)
If the degree of the second hand falls with the range of the degree of the hour and the minute hand then it is in the inside sector else in the outside one
eg: for 5:01:15
degree of hour hand = 150.625
degree of minute hand = 7.5
degree of second hand = 90
As 90 lies between 150.625 and 7.5, the second hand lies in the inside sector
i can give you the algorithm how to perform.we find out the angle of minutes hand and hour hand reference point which is 12 o'clock in clockwise direction. now we find the difference between angle of both hand. if angle is smaller than 180 its is small sector else its is larger sector. and now we can see second hand. check if it lies between hour hand and minutes hand. it is in small sector. now how to find angles of hour and minutes hand.
- Anonymous November 02, 2010for minutes hand. with every increase of 1 minutes 6 degree increase in angle from reference point. so angle = 6 * mm. now for hand .. we have to find exact position of hour hand.now we can find exact position of hour hand with the help of minutes hand. degree of hours hand = (hh+mm/12)* 6.