## Solutions to some Microsoft interview questions

A couple of weeks ago, someone on reddit posted a link to a collection of Microsoft Interview Questions. As someone who interviewed with them while in college, I was curious to see what kind question they were asking folks. After reviewing the list, I thought I’d work out a few that looked interesting:

Imagine an analog clock set to 12 o’clock. Note that the hour and minute hands overlap. How many times each day do both the hour and minute hands overlap? How would you determine the exact times of the day that this occurs?

Thus^{*}: 12:00:00 AM, 01:05:27 AM, 02:10:54 AM, 03:16:21 AM, 04:21:49 AM, 05:27:16 AM, 06:32:43 AM, 07:38:10 AM, 08:43:38 AM, 09:49:05 AM, 10:54:32 AM, 12:00:00 PM, 01:05:27 PM, 02:10:54 PM, 03:16:21 PM, 04:21:49 PM, 05:27:16 PM, 06:32:43 PM, 07:38:10 PM, 08:43:38 PM, 09:49:05 PM and 10:54:32 PM.

^{*} Floor the conversion from to hour, minute and second of day.

Pairs of primes separated by a single number are called prime pairs. Examples are 17 and 19. Prove that the number between a prime pair is always divisible by 6 (assuming both numbers in the pair are greater than 6). Now prove that there are no ‘prime triples.’

Assuming:

Twin Primes:

Prime Triples:

There are 4 women who want to cross a bridge. They all begin on the same side. You have 17 minutes to get all of them across to the other side. It is night. There is one flashlight. A maximum of two people can cross at one time. Any party who crosses, either 1 or 2 people, must have the flashlight with them. The flashlight must be walked back and forth, it cannot be thrown, etc. Each woman walks at a different speed. A pair must walk together at the rate of the slower woman’s pace.

For example if Woman 1 and Woman 4 walk across first, 10 minutes have elapsed when they get to the other side of the bridge. If Woman 4 then returns with the flashlight, a total of 20 minutes have passed and you have failed the mission. What is the order required to get all women across in 17 minutes? Now, what’s the other way?

One way:

- Woman 1 and 2 Cross, woman 1 returns. 3 minutes total.
- Woman 3 and 4 Cross, woman 2 returns. 15 minutes total.
- Woman 1 and 2 Cross. 17 Minutes total.

Other way:

- Woman 1 and 2 Cross, woman 2 returns. 4 minutes total.
- Woman 3 and 4 Cross, woman 1 returns. 11 minutes total.
- Woman 1 and 2 Cross. 17 minutes total.

If you had an infinite supply of water and a 5 quart and 3 quart pail, how would you measure exactly 4 quarts?

- Fill 5 quart pail with 5 quarts of water from source.
- Fill 3 quart pail with 3 quarts of water from 5 quart pail.
- Empty 3 quart pail.
- Empty 5 quart pail containing 2 quarts of water into 3 quart pail.
- Fill 5 quart pail with 5 quarts of water from source.
- Fill 3 quart pail with water from 5 quart pail.
- 5 quart pail now contains 4 quarts of water.

Suppose you have an array of 1001 integers. The integers are in random order, but you know each of the integers is between 1 and 1000 (inclusive). In addition, each number appears only once in the array, except for one number, which occurs twice. Assume that you can access each element of the array only once. Describe an algorithm to find the repeated number. If you used auxiliary storage in your algorithm, can you find an algorithm that does not require it?

public int duplicateNumber(int[] A) { int count = 0; for(int k = 0; k < A.Length; k++) count += A[k]; return count - (A.Length * (A.Length - 1) >> 1); }

Count the number of set bits in a number. Now optimize for speed. Now optimize for size.

public int bitsUsed(int n) { int count = 0; while(n != 0) { count += n & 1; n >>= 1; } return count; }

[…] >> sourcecode My Top 10 Source Code Search Engines First saved by mmbuzon | 1 days ago Microsoft Interview Questions First saved by joshuailug | 2 days […]

Recent Faves Tagged With "sourcecode" : MyNetFaves2008-09-22 at 10:50 am