Adobe Interview Question for Software Engineer / Developers






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

1. let pointer p1 and p2 and p3 are pointing to the head of linked list.
2. move p1 by one node and p2 by two node.
3. if loop exists p1 and p2 will meet at some node.
4. now start moving p3 and p1(or p2) by one node. p3 and p1(or p2) will meet at the node from where loop starts.

Proof:
let x=number of nodes from head to the node from where loop starts.
let y=number of nodes from the node from where the loop starts to the node where p1 and p2 meets.
let z=number of nodes from the node on which p1 and p2 meets to the node where loop starts.

Number of node traveled by p1 is x+y.
Number of node traveled by p2 is x+y+z+y.
since p2 is as fast as twice of p1 therefore
2(x+y) = x+y+z+y
=>x=z.
Thus the node from which loop starts is equidistant from the head and the node where p1 and p2 meets.

- Tulley February 24, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

awesome

- Anonymous February 24, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

In the above algorithm, y and z are the same.

- Bond February 24, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Tully..how you can say that..p2 will travel only.(x+y+z+y)..may be it will travel (x+y+n*(z+y)) where n vary from 1 to any number depending upon size of loop..

- Om Prakash Pal February 25, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Well said Om.
Lets see like this.
Loop size is y+z. suppose p1 iterates the loop n times before meeting p2. So in this case p2 will do 2n iterations. So when both p1 and p2 meets total node traveled by p1 is x+n(y+z) and total distance traveled by p2 is x+z+2n(y+z)
so we have (p2 is twice as fast as p1)
2p1 = p2
2(x+n(y+z))=x+z+2n(y+z)
=>x=z

- Tulley February 25, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Tulley...
Lets..total length of linked list is 25 and size of loop in 4...
now please validate your logic...

- Om Prakash Pal February 26, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Tulley..I think your answer is correct nut explanation is not correct..
Let me try like this..
Let ..size of loop is L and length of rest is x..and assume they will meet at position y(from the first node of the loop)...
..then..
2(x+k1*L+y)=(x+k2*L+y)..where k1<k2 and k1 will have only value 0 or 1(but its not very impnt)..
so..
(x+y)=(k2-k1)*L...
this means that if we are at any node in linked list and if traverse (x+y)node then will again back at same position...
i.e. if we are at position y and traverse x+y node then we will reach again at y---> if we will traverse just x node from y then we will be at start node(first) of loop..if x=z as z=(L-y)..
..pls let me know if you have any prob with this...

- Om February 26, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry..in 9th line it will..
this means that if we are at any node in loop and if traverse (x+y)node then will again back at same position.

- Om Prakash Pal February 26, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Om.. can you describe your logic for

1-2-3-4-5-6-7-8-9

and 9 points back to 2, then how your logic will detect first node in the loop?

- sur March 01, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

My logic is the mathematical proof of concept:-
where slow pointer(speed v) and fast pointer (speed 2v) will meet,.. starting node of loop will be equal distance from this meeting point and from head of linked list....
you can check with your example....it will work..

- Om Prakash Pal March 02, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

wiki Cycle_detection would help.

- ftfish February 24, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think either CareerCup 150 or Programming Interviews Exposed has this exact problem and the solution is as described by Tulley.

- airfang613 February 25, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int validateCircularList(node* head) {
	map<signed int, int> addList;
	map<signed int, int>::iterator it;
	node* tail;

	//add head
	int add = (int) (head);
	cout << endl;
	cout << endl;

	addList.insert(pair<signed int, int> (add, head->val));

	//rest of the nodes
	head = head->next;
	do {
		int tempAdd = (int) (head);
		it = addList.find(tempAdd);

		if (it == addList.end()) {
			addList.insert(pair<signed int, int> (tempAdd, head->val));
			tail = head;
			if (head->next != NULL)
				head = head->next;
			else
				return 0;
		}
		else {
			cout << endl << "LOOP exists:" << endl;
			cout << "loop starts = " << (head->val) << "and Loop ends = "
					<< tail->val << endl;
			return 1;
		}
	} while (1);
	return 0;
}

- bingo February 25, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

nice :P

- madarbingo March 15, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I won't type the code here but I did the same problem few days back.

Algo----
take two pointers p1 & p2.
begin a loop moving p1 one node at a time and p2 2 nodes at a time while one of them reaches end OR they meet at some node.
If they meet loop is detected.

Now at the point where loop is detected start moving p2 one node at a time and begin a counter(keeping p1 at same position).

When they meet again this counter will give the size of loop.

Now when we have the size of the loop we start from the beginning node again.
Now offset p2 counter no. of nodes from the start.

Now keep moving p1 and p2 one node at a time. The point where they meet will give the starting point of the loop. Try to visualize with any example.

- peloooo February 26, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

can some1 explain what the following line mean in above comment
Now offset p2 counter no. of nodes from the start.

- alias February 27, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I meant to start p2 n positions from the start where n = length of loop(counter)

- peloooo February 28, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Good solution.

- gavinashg March 02, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

for find out the starting of loop in link list,
first findout the node at which both p1 (incremented by 1)& p2 (incremented by 2) are met.
After that always third node from this meeting node is start node of loop, strongly verified.

Amit Singh

- Amit Singh March 10, 2011 | Flag Reply


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