Microsoft Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

Assuming that by mid element you mean a node which is equidistant from the initial node whether you see from right or left i.e.

-1->2->3->4->1-
here 3 is middle as it is equidistant from head, which points to 1, from left and right.

Algorithm :

1. Take two pointers and point them to the initial node (head).
2. Move one pointer (turtle) by one step, another pointer(rabbit) by two step.
3. if rabbit == head OR rabbit->next == head then return turtle as the mid element.

Code :

Node* findMid(Node* head){
	if(!head || !head->next) return head;

	else{
		Node* turtle,*rabbit;
		turtle=rabbit=head;

		while(rabbit!=head || rabbit->next!=head){
			rabbit = rabbit->next->next;
			turtle = turtle->next;
		}

		return turtle;
	}
}

- Ashish May 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

it depends upon the no of nodes in the linkedlist. If it is even the logic will work. But what if the node is odd?

- debnath May 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@debnath Let's say there are 3 nodes then the second node from initial state will be the middle one which the above code will return and in case #nodes are even for eg. 4 then the middle element can be 2 or 3 and the code will return 3th node which is indeed correct.

- Ashish May 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

it is all nice but circulare list could also start in head and never come back to it:
1>2>3>4>5>3....
your solution only cover private case of fully circular list.

- avi August 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think circular list means tail->next=head;
your example is just a list with loop in it, not circular list.

Anyway, you can ask the interviewer what he means by circular, this kind of question shouldn't matter.

- WangYao.Pw November 03, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think circular list means tail->next=head;
your example is just a list with loop in it, not circular list.

Anyway, you can ask the interviewer what he means by circular, this kind of question shouldn't matter.

- WangYao.Pw November 03, 2013 | Flag
Comment hidden because of low score. Click to expand.
2
of 4 vote

have two pointer faster and slower, increase one node for a slower and two nodes for faster pointer and see when faster pointer reach the starting node again, slower pointer will point to the middle element.

- sjain May 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

That's a correct solution, however it's cleaner to do this in two steps:
First, find the length of the list (let's call it N) by iterating over it until we reach the starting node.
Now, move forward from the starting node N/2 steps. Done.

This is better than using two pointers, because you only need one pointer, and the number of forward movements is exactly the same. The difference is that it's done in two independent steps, rather than doing the full iteration and the half iteration at the same time, which is unneccessary and a lot more confusing.

- gen-y-s May 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
4
of 4 votes

but what if fast pointer doesn't meet initial node while taking 2 steps? For this u need to chek at each step of fast pointer if initial node is reached.

- amnesiac May 20, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 3 vote

Here is a fool proof way that will take O(n).

1.) make a temp value and set it equal to head.next
2.) iterate temp through linked list while incrementing a count
3.) if temp = head break out of loop. else increment count and set temp to temp.next
4.) divide count by 2. this is how many nodes away from the head node the mid node is
5.) iterate from the head node to the mid node and return the mid node

- Anonymous May 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Agree with 'Flag':
for this question requirement Flag's sol is just perfect
However
problems where there is loop in link-list and looping point is unknown, fast and slow pointer method works

- PKT May 19, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think we use logic of slow and fast pointer here.

code is as follow :

node * middle(node *head)
{
node *sp=NULL;
node *fp=NULL;
sp=head;
fp=head;
while(sp)
{
sp=sp->next;
fp=fp->next;
if(fp->next!=NULL)
fp=fp->next;
if(fp->data==head->data)
return sp;

}
return NULL;
}

- Suhas May 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

If the list contains duplicate data, this logic fails

- nightingale May 17, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

This is pretty close.
1) You should check the pointer fp -> next with head (before the 2nd next),
2) What if the list is odd length, what if its even. You took care of the odd case. If it's even, even though there's no "middle", the loop will go infinite
.

- Mo Lam May 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

above solution will be fail if doubly linked list has duplicate node value. and this condition

if(fp->next!=NULL)
     fp=fp->next;

should not use because it is circular linked list....

- neelabhsingh May 20, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void GetMidNode(){
if(Head == null)
return;

if(Head.next == Head){
System.err.print("Mid Element : " + Head.i);
return;
}

CircularNode t1,t2;
t1 = t2 = Head;

do{
t1 = t1.next;
t2 = t2.next.next;
}while(t2.next != Head);

System.out.print("Mid Element : " + t1.i);
}

- Rahul Singh Rathore May 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If it's circular, isn't the definition of "mid element" more or less an oxymoron?

- Anonymous May 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int MidInCircList(struct Node *node)
{
if(!node || !node->next)
return -1;
struct Node *head=node;
struct Node *first=node->next,*second=node;
while(first!=head)
{
first=first->next;
if(first!=head)
{
second=second->next;
first=first->next;
}
}
return second->data;
}

- Anonymous May 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//Use 2 pointers, one slow and other fast
int middle(node* head)
{ node* p,*q;
p=head;q=head;
if(!q)
while(q->next!=head && q->next->next!= head)
{
p=p->next; q=q->next->next;
}
if(q->next==head) return p->value;
else return p->next->value;
}

- neha May 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It just another version of finding middle of linked listwith following modifaction
1.Storing head in a temp variable.
2.The termination condition will now be while(p2>next!=Head&&p2->next->next !=head)

- blackfever May 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use fast & slow pointer...
Move fast pointer by 2 nodes & slow pointer by 1 node. When fast pointer takes each move check if initial node reached. At that time move slow pointer by 1. As soon as u get intial node, That is the middle node.

- amnesiac May 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is very close, just need the following
1. After each increment of fast pointer check if head is reached, in case list has odd number of items
2. Instead of comparing data of fast pointer to the head of data pointer, compare the fast pointer itself with the head pointer. This is in case the list has two items with the same data.

- rajp May 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

we can solve this by two pointers intialy both pointing two intial node and move first pointer to left side another to right side ..ther meeting node is the middle one

- Anonymous May 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Since it may not be guaranteed the list is indeed circular, extra cautious checks are done:

typedef struct SCList
{
    int Data;
    struct SCList *Next;
} TSCList, *PSCList;

PSCList FindMedian(PSCList head)
{
    bool fStart = false;
    PSCList median = NULL;
    PSCList fast = NULL;

    if (head)
    {
        fStart = true;        
        median = head;
        fast = head;
        while (fast && (fast->Next != head && fast != head || fStart))
        {
            fStart = false;
            if (fast->Next && (fast->Next->Next != head && fast->Next != head))
            {
                fast = fast->Next->Next;
                median = median->Next;
            }
            else
            {
                fast = fast->Next;
            }
        }
    }

    return median;
}

- AK November 11, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

A detailed explaination is available here:

ultrainterviews.com/question/interviewer-asked-me-to-find-if-a-linked-list-is-circular-or-has-ends/

- arj December 28, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 3 vote

Have to pointer objects and traverse in opposite directions.
After each step, check if the both objects point to same location.
When both objects point to same location, that is the mid element.

- sravan May 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Sarvan, I think your answer is really good. But just want some psuedo code.
Could you pls write some pseudo code?

- rasmiranjanbabu May 17, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

it is applicable only if you have doubly circular linked list.

- sjain May 17, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

sajain is right. In a regular linked list you can't move in reverse direction.

have 2 pointers fast and slow. fast pointer moves in speed of (speed of slowpointer*3). the 2 pointer will meet in the middle element.

- Prithvi May 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Prithvi - your solution will only work if the there were even number of nodes

- Astro May 20, 2013 | 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