IBM Interview Question for Developer Program Engineers


Team: ISL
Country: India
Interview Type: In-Person




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

Algorithm is here :

Take two pointer P and Q;
Move P twice as Q, and move Q one step at once.
start with P = Head = Q;
Q = Q->next;
P = P->next->next; -- check condition of termination(case when no loop)
repeat until P == Q;

Complexity : O(Length of linked list)
Prove is here :
It is easy to see that if the linked list has a loop then after some time both the pointer will be in the loop.
Consider the time when they are in the loop. Now let First pointer is at Ith node, and Second pointer is at Jth node in linked list. Lets assume that node at Ith place is faster one. So Ith node require J-I+1 move in order to reach to Jth node, and we know that First pointer is moving 1 node relative to second one.which mean it move one node nearer to second in every step. so in J-I+1 step's it will reaches to node second. Hence proved.

- sonesh March 05, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

One way to prove it mathematically:

Assume there is a loop in the list, we have:
a = index of pointer P
b = index of pointer Q
start = index of the loop's starting point
len = loop's length (how many elements in the loop)
t = how many times we have moved pointers of P and Q since both of them are in the loop

We get:
a = start + t % len
b = start + 2t % len

So the distance between P and Q will be:
distance = | a - b | = | t % len |

When there is a loop, it is always possible that
t % len == 0

When there is not a loop, distance will never be 0.

So this approach works.

- Terence August 17, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

n* detectloop(n* hptr)
{
	n * slow,*fast;
	slow=fast=hptr;
	do

	{
      slow=slow->next;
      fast=fast->next->next;      
	}while(slow!=fast);
slow=hptr;
while(slow!=fast)
	{
	slow=slow->next;
	fast=fast->next;
	}
	return slow;
}

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

There is the no loop in your example :). I explained this, Interviewer was expecting some other answer.

- PCB March 05, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

let this be address of list
100->200->300->400->500->600->300(loop back to 3rd node)

let two pointers p and q

initailly p,q are @ 100

then p @200
q @300

then p @300
q @500

then p @400
q @300

then p @500
q @500

slow n fast pointer will point to node whoose
next node is making loop

slow move with x speed
fast will move with 2x speed
they will meet at point where actually loop
tend to start

but there is a assumption that head node must not
create a loop.
100->200->300->400->500->600->100(loop back at 1st node)
then above calculations fails

- Anonymous March 11, 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