Morgan Stanley Interview Question for Software Engineer / Developers






Comment hidden because of low score. Click to expand.
2
of 1 vote

Using 2 pointers, we can find out if there is a loop by moving the 1st pointer one node at a time and the 2nd one, two nodes at a time, until they meet.
Once they meet, hold on pointer stationary and move the other one node at time, till they meet again - This gives the length of the cycle/loop, say = N.
Now move both the pointers to the start of the linked list/head. Move pointer1 N positions ahead, and then begin to move both pointers one node at a time, till they meet(means keep a distance of N between the 2 pointers). The point where they meet is the start of the cycle.

- JRS March 07, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

very nice

- Anonymous June 10, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sorry the figure got altered accidentally....

1-->2-->3-->4-->5-->6-->7-->8-->3(8th node points to 3rd node - this is the cycle)

- JRS March 07, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

nice solution!!

the questions says that you can use only two pointers but for the solution mentioned above. one head pointer and a variable N are used extra.

If we use two+head pointers, and two variables. Then also you could get the starting node of the loop.


1st pointer T1 moves one by one and counts its position n1
one each: 2nd pointer T2 moves from head to T1 and counts its position n2
whenever n1!=n2 break

n2 is the index of the starting pointer and T3 will be the node of the starting pointer

- abid March 07, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi abid,

can you explain it clearly. suddenly where did T3 comes in between?

- HeHe March 09, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I know by using two pointers, I am looking for loop detection using only one pointer. ??

- YetAnotherCoder October 01, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think the total number of iterations to detect looping (including self loop) would be 2n - 1.

1->2->3->4->5->6->4

Let me know what you think

- kannan September 05, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Just move 1st pointer to it's next and the 2nd pointer to it's next->next until the values of the two nodes meet. The place where they meet, keep one pointer there and point the second pointer to the very first node where we started, in the example case: we will meet at 7. So, keep one pointer pointing to 7 and another to 1. Now start shifting one node at a time. The place they meet again is the starting node.

- IntelliHub September 18, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Just move 1st pointer to it's next and the 2nd pointer to it's next->next until the values of the two nodes match. The place where they meet, keep one pointer there and point the second pointer to the very first node where we started, in the example case: we will meet at 7. So, keep one pointer pointing to 7 and another to 1. Now start shifting one node at a time. The place they meet again is the starting node of the loop.

- IntelliHub September 18, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Good solution.

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

nonsense, try it with additional 8->9.->10->11->3 and this algorithm fails.

- AT April 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry, the algorithm is correct !!

- AT April 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I tried every possible way to find if alog doesn't work. I'm amazed to know the position where those pointer meet to detect loop. i.e. Then the pointer which kept at 7 and second moved to 1 are equidistant from start of loop.

May I know logic behind it? It works for any number of elements in given linked list, inside or outside loop

- nerd January 30, 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