Wednesday, March 15, 2006

Microsoft Interview Questions - 2

When I went to Microsoft for the second time, the interview was more pressurizing.

Interview 1 - The interviewer asked me a lot of questions about my project and the disucssions started with a lunch, which my interviewer hosted in an nearby restaurant. After more than 30 mins discussion about my favorite project, he gave a design question, which was not hard.

Q 1. Given a data store and multiple getters & setters, write a program to maintain consistency.

I gave out a simple queue concept, where getters and setter are allowed one by one to access the data store and in this case it would be like a semaphore. However, this would make the system inefficient by killing parallelism. So, I gave optimization, by adding 2 points:

1. If there are consecutive getters in the queue, allow them all in parallel

2. If there are consecutive setters, throw out all but the last, as they dont make any useful change and simulate the exceptions. I left the exception simulation for hardware and did these two things to have the data store with high parallelism and consistency.

The interviewer seemed to be pleased by the design.

The next interview was slightly more abstract. The interviewer grilled me on team working and bothered about the team-aspects of my favorite projet - a distributed file systems project. Then she gave a question:

Q 2. There are a number of tasks and they have their dependencies. Design data structures to represent them and write a program to schedule the tasks such that the dependencies are not violated.

I asked about other questions, like time deadlines, etc, but the tasks didnt have time deadlines and if a task had all its dependencies satisfied it can execute.

I designed a Directed Acyclic Graph (DAG) as I believed that it has to be a graph as tasks can have many to many relationship, it has to be directed as dependencies are always unidirectional and it has to be acylic so that it can be scheduled. To represent the graph, initially I took the adjacency matrix format, but soon realized that the graph will be very dynamic as new tasks keep coming and existing taks are scduled and removed. So, I designed based on adjacency lists and wrote a program that would run through the graph to find a node without dependency, schdule it and traverse the entire graph to find out its dependents and breaking the links. The interviewer was satisfied with teh design and after a long discussion (I dont exactly remember the details) the interview ended.

Q 3. The interview had a simple question, on which I stumbled initially. I was given a long string of numerals and have to write a funtion that checks if it contained all numbers from 000 to 999 (numbers are represented in 3 digits).

I first created a 1000 element hash array one for number from 0 to 999 and set it to false initially. Then I read through the array from 0 to len - 2 & in each iteration I take out i, i+1, i+2 characters, convert to number N and setting the element in the hash array[N] to true. At the end if all the elements in hash arry are true, then all numbers are there in the given string and the function returns true. Then, I did the folloowing optimizations:

1. To check all the elements in the hash array to be true, I have to go through the entire array in O(N) operations. Instead, I could just keep track of number of true elements in the array with a simple counter. Thus, everytime I set an element in the array to true from false, I increment the counter. If the counter reaches 1000, then it means that the entire array has true values and I can stop the fuction there itself, instead of goin through the entire length of the string. I can also eliminate the O(N) checking at the end.

2. The minimum length of the string that could have all 1000 numbers is 1002. I took a minute or two to realize this fact. I saw that the loop has to run atleast 1000 times to set 1000 true values, and to run 1000 times, len has to be atleast 1002. So, if the string length is less than 1002, right at the start we can return false.

3. To go a step further, everytime I find a duplicate element (the array[N] has true already) I increement a duplicate counter. At anytime, the length of the string > 1002 + d. If it is less than 1002 + d, then it means it wont contain all the numbers. This can effectively, reduce teh possibility that the entire string has to be searched.

The interviewer was impressed with the design and so I consider that it was positive.

Q 4. It was the famous tic-tac toe problem. Given a large N * N tic-tac-toe board (instead of reguard 3*3 tic-tac toe), the fucntion has to determine whether any player is winning in the current round, given the board state. Create appropriate data structures to represent the board.

I started out from the simplest of solutions. Take the board as a N*N array, go through all the rows and columns and diagonals to see whether they have same player's coins (for efficiency we can represent the two players as true and false and ignoring the free spaces, and if we XOR all the rows and then columns and diagonal and if get Negative (and full row or column) , then one of them wins). It was simple but was a O(2*N*N+2*N) = O(N^2). I knew i could do better.

I then had sentinels for each row, column and diagnals (2N+2 sentinels) for each player. Every time, I place a coin in the board in (x,y), I increment the row_sentinel[x], column sentinal[y] & diagonal[1] if x == y or diagaonal[2] if x == N-y-1; If any of these elements equals N, then the corresponding player wins. It is cleaner and effecient and any point I need to check just the 2N+2 sentinals and it is a O(N) operation. I still could do better.

I created a super-sentinal for each player that keeps track of max value among sentinels. Thus, everytime I update the sentinals, I would check to see if it greater than the value of super sentianl and if yes, I would update teh super-sentinal. Thus, at anytime to see if a player is winning, I need to check (super-sentinal == N). It is extremely simple and highly efficient and is a O(1) operation.

The interviewer seemed to be very impressed. I clinched the job.

Q 5. Now the interview with the Product Manager (a person responsible for many core aspects of MSDN). Last interview with a head of the group, is always a positive sign and so I was upbeat. But, the question was extremely tricky and one of the stunning problems with a very simple solution.

There is an array of odd and even numbers. Now, sort them in such a way that the top portion of the array contains odd numbers, bottom portion contains even numbers. The odd numbers are to be sorted in descending order and the even numbers in ascending order. You are not allowed to use any extra array and it has to use a conventional sorting mechanism and should not do any pre or post processing.

I'll give you a clue (which I had to secure after a painful 20 mins).. You can use operator overloading and write the solution in 5 lines.. If you solve now, it would still be great, because I solved it after 20 mins and with a lot of hints.

Friday, March 03, 2006

How I got into Microsoft

I hope you wud have seen my earlier post on Microsoft interview. I didnt update my blog, after that, due to so many events... Let me recount some of them.

After coming from the Microsoft interview - 1, I was totally confident and felt happier that the interview went well. I had more than 5 interviews and the interviewers gave a fell-good factor. So, I was pretty upbeat about the results. So, I started waiting for the results from Monday after the interview on Friday (10th Feb). But, as days started moving, I didnt get any results. I was panicked. I called up the recruiter and she asked me to wait further, while most others had gotten the negative results. By Friday, I felt pretty dejected and the recruiter called me up and conveyed her regrets that the team profiles didnt match with me and so they cudnt offer me. I knew exactly what she was talking, as in couple of interviews I gave a strong liking for distributed systems (the teams were dealing with security and subscription) and expressed my desired to change the teams, once I join Microsoft. I realised how politicially incorrect, that was. After all, it is the team that is goin to get you into Microsoft and if you dont show an interest in the team, the team wont be interested in you.

So, the Rule No. 1: Always show interest and passion in what you work. If you r interviewed for sweeping, convince the interviewer that sweeping and mopping are the best jobs in the world.

Rule No. 2: Never be too candid and open in your remarks. In business and politics, the art of 'sweet lies' have to be learnt.

That weekend, I was a bit moody, as the Microsoft 'rejection' came at the same time as rejections for my PhD admission from MIT & Stanford. Truly, I believed it is the end of the world. Not becaz, some big company kicked me out, but because I cant do much better on my interview and I wont know what to improve on to bring a success. On Monday, I took on the pre-planned trip to Virginia to interview with the company, where I interned. Its a really good one for my research and had very good pay package. I went and spoke to them, had a couple of interviews and the CEO (its a small company, after all) liked me and was upbeat in offering me a designer job. But, the only thing about the place was there was something that was bothering my intuition about that company. I associated it with a bit of damp air and without sun shine. Whereas Microsoft appeared fresh and sunny and it was a psychologically important factor for me to have sunlight at work. But, I was to seal the offer in a couple of days.

But, then on Tuesday, Microsoft got back to me and asked me about the team preference and in one not-so-usual case they were offering me a second interview and with a team closer to my interests - XML enterprise team (though the name didnt appear cool to me, their work on distributed tools and systems for MSDN interested me). And I wanted to interview immediately, as I had give the decision to the other company. So, tickets were booked urgently and within 2 days, I went back to Seattle. This time, I was not as much upbeat as I was last time.

Rule No. 3: Know what you like and dislike and we need not cheat ourselves on that. If stick to what you like, the doors of opportunity will open eventually.

I'm not goin 2 explain the interview process in much of detail. Its goin to be a separate post. But, this time the interview process were different. In general, it was more like kicking my ass. The interviews were deliberately pressurising.

The first interview needed me to explain clearly why I'm choosing distributed system rather than anything else. Second one, asked me why I'm choosing Software Designer position, though my interests suited me the Program Manager position. I argued that I'm not experienced enough to handle the post of a Program Manager, rightaway and eventually would like to become one. Third interview pressurised me on my team skills . Fourth one, with the development manager (who was the same person who interviewed me first, at my campus) I was asked about my research interests and why I wanted to get into Microsoft. The last interview, with the project manager was even more pressurizing. He told me all my negative reviews from the all the previous interviews. He told me that I might not be a good team player, I didnt listen to the interviewers, I made my problems harder.... I was smiling at him, as he said I think you have the abilities to become a computer scientist. This final interview gave me enormous confidence and a liking to work under such a person. I decided that I'm not goin 2 do a PhD for now, then and there, and to make my decision easier CMU sent a rejection in that same evening.

I appeared more confident, because interview by Product Manager at the end, is a positive sign. I told my recruiter that I wanted the result within the next 24 hours and true the word. She had it ready next day. The next morning, I was too late for the flight -- 50 min before the flight, I was at my hotel 30 min away from the airport. So, when I stopped over at Detroit, in the evening, I called her up and she gave me the good news. I GOT THROUGH.... Now, remaining is negotiation. I said, since I was at the airport, I would negotiate the offer the next day.

The next day (Wed, 1 March) I set up the time for negotiation. The package details have to be strictly confidential, by agreements, so I cant reveal any of that. The recruiter went in great detail in explaining all the benefits, including shares and relocation. I was not extremely satisfied with the base-salary, since the other company's salary was equally good. But, over the course of the week, the negotiation went well and I was convinced of the process and finally accepted it on 6th March. It was a great feeling... Now over to the legal processes...

Collected links about me