Microsoft Interview Question for Software Engineer / Developers


Team: Windows Live
Country: United States
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
5
of 5 vote

The hour hand move 360 deg in 12 Hr i.e every 12 * 60 = 720 min it rounds 360 deg.
so per min 0.5 deg.
And minute hand move 360 deg in 60 min i.e 6 deg per min.

Angle of Hr Hand = 0.5 * (60H + M)
Angle of Min = 6 * M

Theta angle = Angle of Hr hand - Angle of Min

- jainrajrahul January 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

kya baat hain...
jus make it an absolute difference...

- kher.ajinkya February 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

That angle is a periodic function. Inside its period the function is linear. Thus all we need is find a period. If x is a period and minute hand rotates 12 time faster than the hour hand, then we can write down equation for that:12x-12=x. Thus x=12/11of hour is a period when the minute hand over the hour hand. It's very efficient to store pre calculations in constants. So the function requires only two operations to be computed. One should note that function returns angle in radians.

public class Clock
    {
        private static long Period = TimeSpan.FromHours(1).Ticks * 12 / 11;
        private static double Scale = 2 * Math.PI / Period;
       
        public double GetAngle(TimeSpan time)
        {           
            return Scale * (time.Ticks % Period);
        }     
    }

- Weerel February 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Considering that the hour format is in 24 Hrs a day.

Angle = ((MinuteHandReading/5)*30) - ((HourHandReading % 12)*30)

- Vijay Rajanna January 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

float getAngle(int hour, int min)
{
float hourAngle = (hr%12)*30 + 1/2*(min%60);
float minAngle = (min%60)*6;
return (abs(hourAngle-minAngle));
}

- Wayne January 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You have assumed that minute could be above 60 minutes. If that's the case why are you taking the mod in the hour calculation. I.e. 70 minutes and you are getting rid of 1 hour by doing that which is wrong.

- Rayden January 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Angle Between two minutes min1 and min2 = 0.5 degree.
i.e Angle for one minute = 0.5 degree.
Now calculate total minutes betwn hour hand and minute hand = n minuetes.
anagle = n * 0.5 degree;

- Santosh March 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I saw this question a long time ago.
Most people forget to take into account that not only does the number of minutes affect the position of the hour hand, but so does the current number of seconds! Here's a solution:

this assumes the following Time struct.

struct Time
{
	int hour;
	int min;
	int sec;
};

double Problems::Problem1::findAngle(Time t)
{
	//Ensure correct values.
	//Should also check negatives, but we can skip these for now.
	while (t.sec >= 60)
	{
		t.sec = t.sec - 60;
		t.min++;
	}
	while (t.min >= 60)
	{
		t.min = t.min - 60;
		t.hour++;
	}
	t.hour = t.hour % 12;

	//first find angle from 0 (straight up) of both.
	double angleHour = (t.hour / 12.0) + (t.min / (12.0 * 60.0)) + (t.sec / (12.0 * 60.0 * 60.0));
	double angleMin = (t.min / 60.0) + (t.sec / (60.0 * 60.0));
	//results are 0-1(.99999...) value of how far along the clock each hand is.
	//for instance: .5 represents straight down.

	if (angleHour > angleMin)
	{
		//hour hand is further, so we need to swap. (This avoids and problems with negatives, and no need for absolute value)
		double t = angleHour;
		angleHour = angleMin;
		angleMin = t;
	}

	//see if there is more than a .5 difference, if so, we need the other angle (smallest angle of the two)
	double difference = angleMin - angleHour;
	if (difference > .5)
	{
		difference = 1 - difference;
	}

	//Assuming we want the result in Degrees
	return difference * 360;
}

- Pat September 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

float getAngle(int hour, int min)
{
float hrangl = (360/12)*hour;
float minangl = (360/60)*min;
return (abs(hrangl - minangl) );
}

- srikant January 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is incorrect as it does not account for the small movement of the hour hand in between whole hours. For example, at 3:30, the hour hand will be halfway between the 3 and 4, which is actually 105 degrees from the 12.

- Jonathan January 22, 2012 | Flag


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More