Archive for March, 2011

Microsoft SDET Interview Experience-2

A few days back I wrote down my SDET interview experience with Microsoft. The experience can be read at http://wp.me/p1pzxx-x.

Today a friend of mine who also works in Microsoft currently as SDET shared his interview experience for my blog.  Here are the questions he was asked during his interview with Microsoft.

Okey here are the questions they asked me in a total of 10 interviews :).
1. You have to play a game. You have a 4×4 matrix with random number from 1-15 in it. One box is empty. Only following moves are available you can swap top or bottom or right or left element with the empty box (no diagonal swaps). You have to sort the cube 1 – 15 in the 2D array and the hole should move to the end.
2. you have an array of 100 elements which can have numbers from 1 – 99. The array is filled one number is repeated, what is the number.
3. you have to print the list of all the files in a directory and all its sub directories.
4. you have a number, consider this number on a key pad where it has 3/4 english language alphabets associated with it, like 1 corresponds to abc and so on. Print all the possible alphabet permutations for this number.
5. develop the back functionality for internet explorer’s back button.
6. Write test cases for you FYP, for your best project and for the projet you are working on currently.
7. what is good code. test a pen. You have to make a presentation and you dont have the book what will you do.
8. There is a function which draws a line on the screen. Test the function.
9. You have a circular singly link list. Add an element at the end of the list but you cannot traverse the list.
10. Find duplicates in a string.
11. Give a lecture on memory.
12. what is process, thread.
13. Propose an algo/data struct for memory manager.
14. Propose and algo/data struct for timer manager.

I hope his interview proves beneficial for everyone who is going to appear for an interview. Good luck ! and see you soon @ Microsoft.

 

Advertisements

Leave a Comment

Print a tree level by level (Level Order Traversal)

Printing a tree level by level

A common interview question in which you would be asked to write a code that would print a binary tree by level. For example the below binary tree when passed to your code should print 7,6,11,3,5,13,12



 

 

 

 

 

This is also known as level order traversal. The level order traversal of a tree is also breadth first traversal of the tree. The concept of level order traversal is to visit the root and then visit all nodes 1 level away, then all nodes 2 levels away and so on. We would need a queue to perform this kind of traversal.

The psuedocode for the program would be as follows:

Create an empty queue “Q”
enqueue the root element of the tree
while queue is NOT empty
dequeue an element from the queue
print element
Get children of the dequeued element
enqueue children

Having written the psuedocode its now very straight forward to convert this into a working C++ code.

 

 

Leave a Comment

Microsoft Technical Interview tips !

Over the past weeks or so I have been asked this question a lot of times “How to approach a Microsoft Interview?”   Based on my experience I would suggest the following to anybody who is going to appear for an interview for a technical position with Microsoft in coming future.

  • Know your Data Structures & Algorithms! If you read my interview experience you would see almost every question revolved around some data structure or the other. I suggest you review all popular data structures and algorithms and see how you can solve different problems using these data structures. On top of that it goes without saying that algorithms such as post order, pre order and in order traversal of a tree, traversing a linked list, reversing a linked list, reversing a string , hash tables, binary search trees, sorting algorithms, recursion vs. iterative should be on your finger tips! You should also be able to derive the space and time complexities of your code and how could you further optimize it.
  • Practice coding on a white board: Most of us have a mindset that we can only code using our editor and compiler to be sure that we have done it correctly. However life is not that straight forward! During your interviews you would have to write a piece of code on the white board where you won’t be having the services of our old friend “compiler”. I realized it during my interview that writing code on a white board is something that needs practice. So I suggest while you prepare for your interview get a white board and practice all your codes on it. Have a friend go through them with you once you have written them down.
  • Ask questions (logical): Most of the questions that are presented in the interview are very vague and it has a purpose. The interviewer is looking at how you dissect the problem before coming up with the solution; do you have that thinking ability to properly understand the problem before coming up with a solution for example “Delete a specific node from a linked list” if you jump straight down on writing a code for deleting a node without even being sure what kind of a linked list we have you are getting yourself in trouble! You can ask your interviewer questions like “is it a singly linked list? Doubly or circular linked list?” “What kind of data does It store” etc. This gives the interviewer an impression that you have the thinking skills to properly nail down a problem so that you are sure that you understood it fully. Probe for more details !
  • Don’t panic: Even though you might have practiced a lot of common interview questions but the actual interview questions might still be different. Just ensure that you have covered all the bases by revising all core concepts and make use of them to come up with a solution. The interviewers are not looking at exact answers to a problem rather how you approach a problem when pushed in a difficult situation. There is no harm in asking the interviewer to elaborate on the question if you couldn’t understand it the first time. Be confident!

  • Think aloud: As I just mentioned above that Interviewers are not looking for an exact solution to a problem and most of the times are looking how you approach a problem. The major thing here is that even if you are bogged down by the problem on hand; talk out aloud your thought process so the interviewer knows exactly on what lines your mind is thinking. I have seen many real examples of people who failed to solve a problem yet got offers because they were clear in communication with the interviewer.  I was confident enough in all the programming questions that were thrown at me so most of the time I dived straight into coding them on the white board which lead to an interviewer giving me feedback that “most of the interviewers don’t want you to jump straight into the code rather than thinking and doing some homework on the problem talking aloud”. So in a nut shell don’t dive straight into code and talk through your algorithm with your interviewer before writing the code.

I guess that’s very much about everything that you might need when you interview with Microsoft but if you if feel I left out something please leave a comment and I will be more than glad to answer or help. Hoping to see you soon at Microsoft Campus! Best of luck!

Leave a Comment

Deleting the entire linked list

This is another common interview question. The interviewer wants to see how you tackle the problem and maintain pointers to correctly de allocate all the memory reserved by the linked list.

Suppose you start with the head element and traverse the list and delete elements as you move along…but wait a minute! Do you advance the pointer first or delete the pointer first? Suppose you advance the pointer than how would you delete the previous node? And if you delete the node than you can’t advance as your node contains the address of the next element.  So to perform this function we would need to maintain two pointers one which would hold the current node and one which would hold the next node in the linked list. Here is a sample code in C++

Void Delete (Node ** head)
{

Node* temp = *head;
while (temp) {

Node * temp1 = temp->next;
delete temp;
temp = temp1;
}

*head = NULL;

}

Updated courtesy supergokhan

 

Comments (2)

Detecting a cycle in a linked List (Floyd’s Cycle Detection)

OK here is another very interesting interview problem which I wanted to discuss today. The problem at hand is that you are given a singly linked list and (unfortunately) it contains a cycle i.e. some node points back to an earlier node in the linked list. Your task being a software engineer is to detect if  the linked list contains a cycle.

My initial brainstorming gave me an idea that I should traverse the list and mark nodes on my way so if I reach a node that has already been marked Hurrah !! Got ya !

Lets see how this approach works. So now I need to maintain a status for every node so I updated my Node Structure as follows

Struct Node {
int data;
Node * link;
bool status;
}

Firstly I would have to initialize all nodes with status value equal to “false”

Node * temp = head;
while (temp) {
temp->status = false;
temp = temp->link;

}

Having done that now my job is very simple. I can detect the cycle as follows:

Node * current = head;
bool isCycle = false;

while (current){

if(current->status ==true){
isCycle = true;

}

current->status = true;
current=current->next;

}

The approach seems to be fine but it required us to change our original Node data structure. Supposing the interviewer wants you to come up with a solution that doesn’t change the underlying data structure than you need to figure out some other way. Let’s look at a different algorithm that achieves this.

Most of us would remember the rabbit and turtle story from our childhood where the rabbit was very quick and turtle was very slow during the race. Consider they were racing on a straight track and for sometime assume the rabbit didn’t become complacent and instead kept on running he would have reached the finish line much quicker than the turtle, however consider they were racing in a circular field and there was no finish line the rabbit would have moved ahead quickly and eventually would have crossed the turtle again after some time. Can we use this story for our help? Yes we surely can ! this algorithm is a classical example of Floyd’s Cycle Detection Algorithm or also known as Tortoise and Hare Algorithm. So if we have a cycle in our linked list than some where along the traversal the faster pointer (Hare) would meet the slower pointer (turtle) indicating we have a cycle , else the hare would reach the end of the linked list if there is no cycle. Let’s see how we can code this.

//Hare moves twice the speed of the Turtle so if we have a cycle eventually Hare would meet the turtle again.

bool isCycle(Node *head) {

Node * turtle, * Hare;

turtle=Hare=head;

while (Hare){

if(Hare>link ==turtle->link){

return true;

}

turtle = turtle->link;
Hare= Hare->link->link;

}

return false;

}

Comments (1)

mth from the last element of a singly linked list

Write a program to find the mth from the last element of a singly linked list

Singly Linked List

So for the linked list shown m= 0 would return the node with information field 1203 (tail element) and m = 1 would return node with information field 1202.

Solution:

This is a very common interview question. Let’s look at a very simple solution.

  1. Traverse the list and count the number of nodes.
  2. Calculate the offset of the mth from last element from the start using  offset = (length – m)
  3. Traverse the linked list again (for int i=1 ; i <=offset; i++) and return the current node.

However there must be a better way of approaching this instead of using loops to traverse the list twice.

Another solution can be as follows:

  1. Initialize two pointers p and q both pointing to the head of the linked list.
  2. Now let’s make the distance between p and q equals to m by incrementing the q pointed m times.
  3. Now check if q-> next == NULL (means we have reached the end of the linked list and p is at m positions from the last element so we return node pointed by p) else increment both points p = p-> next and q = q-> next and repeat step 3 again.

Rough implementation in C++ would be as follows:

Node * mthfromLast(Node * head, int m){

if(head==NULL|| m < 0)
{   return NULL;
}

Node * p, *q = head;

for (int i = 0 ; i < m; i++){
if(q->next == NULL)

{ return NULL;
}

q = q-> next;
}
while(q->next!= NULL) {

p = p-> next;

q = q-> next;

}
return p;
}

Singly Linked List Image was taken from : http://www.tech-faq.com/images/singly-linked-list_clip_image002.jpg

Leave a Comment

Raymond Davis

Dawn Newspaper 3/17/2011

ISLAMABAD: Federal Minister for Information and Broadcasting Dr Firdous Ashiq Awan said on Wednesday that Raymond Davis was released after the payment of blood money (diyat) in accordance with the Shariat Law of Pakistan.”

While skimming through today’s news paper the above headline caught my attention and like many Pakistanis a series of questions came up in my mind. Yes that’s true that Islam has laid down a proper code for accepting money against bloodshed to forgive the culprit. However on what grounds Raymond has been released from other cases such as carrying illegal arms and doing actions such as spying and putting the whole integrity of a nation on line? Which Shariat law or Country law allows foreigner to invade country secrets carry illegal arms and than just go away??? Any Pakistani involved in a similar action in US would have been let go this easily?.  Raymond should have been put under trial and test for all offenses before allowing to fly off away from the country. It’s a shame that Government officials preplanned everything to allow him a safe exit.

Honorable minister although your statement reflects your loyalties to your party and to your seat and NOT to Pakistani nation, but would you have accepted bloodshed money decision if God forbids something of similar sort had happened to your loved ones?

Let’s for a change draw a line at some point in degrading ourselves in front of  US.

 

Leave a Comment

Older Posts »