Amazon Interview Question for Software Engineer / Developers


Country: India
Interview Type: Phone Interview




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

take to pointer *a and *b.
1. start the pointers from head.
2. Move one pointer to one step and other to two step forward
3. when both meets at some point, it concludes that there is loop in the list...

- scorpion May 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

That solution works if we are assuming that the loop goes back only to the previous node. If the loop goes back two nodes or three nodes this solution fails to identify the loop.

Here's my solution:
Keep a set of nodes, V, the visited set.
iterate through the linked list, and as you visit a node,
first check if it is already in the visited set.
If it is already in the visited set, you have a loop, return true.
Otherwise, add the node to the visited set.

If you have looped through and never returned true, then there is no loop. return false.

- David May 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

why do u think the sol will fail if loop is of 2,3 nodes ? (yo)

- code756 May 24, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@ david
why do u think the sol will fail if loop is of 2,3 nodes ? (your sol will be inefficient for large data,this on the other hand is a well known sol for the said prob )

- code756 May 24, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

the linked list must be end at the loop start point?
Is it looks like "6"?

- kurskk141 May 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Assuming the list to be singly linked list, after finding the list to be looped as suggested by Mr.scorpion, we keep a pointer in the loop and start another pointer at the start of the list, we keep incrementing the pointer and keep rotating the another pointer which should be pointed to the pointer in the loop by exactly one circle, if we hit the pointer which is walking through the list we found the starting point of the list.

- krishna May 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

1. Start with 2 pointers at head
2. Move pointer A one node at a time, move pointer B one node at a time
3. If there is a loop, eventually they will meet.
To find the node where the loop starts:
4. After A and B meet, move A back to head.
5. Move both A and B 1 node at a time. They meet at the node where the loop starts.

- Matt May 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Step 2 correction: Move pointer A one node at a time, move pointer B *two* nodes at a time.

- Matt May 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

To find the point where the loop starts - Two nodes pointing to the same 'next' node is the node from where the loop starts. So Start from any node and add the dest node id to a hashSet. Everytime before u add check if the destination node id is already present in the hashset.

- Abhi May 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Correction - 'Start from any node' will not work. You have to start from the head of the list

- Abhi May 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

yes @kurskk141....it may be like a 6...... it may be like a '0' also.

- Man OS May 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Detection of the loop:
-------------------------------
Start with two pointers(Fast and slow) at the head.
Fast will move forward fastly(Two nodes at a time)
Slow will move forward slowly(one node at a time)
Slow == Fast => there is a loop in the linked list.

Loop start point:
-------------------------
Let p1 be the node where Fast and Slow are meeting.
Set p2 = p1.

Advance p2 and wait till p2 == p1, Keep on counting the nodes in between. At the end of this we would have the number of nodes(say count) inside the loop.

Now set temp1 = temp2 =head;
move temp2 till count (Number of nodes in the loop)
Now start moving both the pointers (one node at a time)
when temp1 == temp2 (This is the start of the loop)

- PK May 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It should be number of moves.... not number of nodes for the count.....

Well... i have a little confusion... what is considered to be beginning of the loop....

eg: a->b->c->d->e->c...

here beginning of the loop is e or c....

If its c, your solution is correct....
If its e.... a slight modification..... check if temp1->next==temp2.... temp1 is the beginning of the loop.

- Sachin May 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

every thing is clear except the the algo for finding the starting pt of the loop...please someone explain in clear way..

- yogi.rulzz May 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

But how can there be Head in a circular linked list ? I am not getting it .Please explain someone by diagrammatically or by some other means.Thanks

- sujamait May 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Below code find the loop in O(n) time with the starting point of the loop

/ *
  *  Algorithm
  * Set node value to INT_MIN as an indicator that node is visited
  *  
  * Recursive go to next node until node is NULL or Node value is INT_MIN
  * IF node is NULL then there is no loop
  * IF Node value is INT_MIN then node is previously visited and 
  * the current node is the starting point of loop  
  *
  * While returning reset the node value so that Link List shouldn't alter
*/

struct node {
	int value;
	struct node *next;
};
typedef struct node * Linklist;


//If there is loop in link list return 0 else return 1
int findLoopInLL(Linklist head, Linklist * start)
{
	int result,value;
	if(head)
	{
		if(head->value == INT_MIN)
		{
			*start = head;
			return 0;
		}
		value = head->value;
		head->value = INT_MIN;
		result = findLoopInLL(head->next, start);
		head->value = value;
		return result;
	}
	start = NULL;
	return 1;
}

- Bidhan Pal June 02, 2012 | 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