Amazon Interview Question for Software Engineer / Developers






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

Detect Loop using Floyd’s Cycle detection algo and get the pointer to a loop node.
2) Count the number of nodes in loop. Let the count be k.
3) Fix one pointer to the head and another to kth node from head.
4.)Move both pointers at the same pace, they will meet at loop starting node.

- geeeks July 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

+1

- Anonymous July 20, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

use two pointers....head.next,head.next.next

- ceg July 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

it can be used to find loop in linked list. Then used a stack to find where the list is joined.

- Dinesh July 17, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

it can be used to find loop in linked list. Then used a stack to find where the list is joined.

- Dinesh July 17, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

My soln will work with O(n2) complexity can anyone come up with some better way ??

- Aditya July 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Make another copy of the linkedlist.

modify the link traversal in the following way

struct node *current=first;
while(current->next!=NULL){
  current=current->next;
  current->data+=1;
 }

now start traversing the original and the copied linked list simultaneously and compare the data of the nodes of each node, which ever node has the difference=2 , is the start of the cycle

Complexity : Time: O(n)
Space: O(n)

- saurabhsonu001 July 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

count the total number of nodes in the loop. this we can find out using tortoise and hare algorithm. after that find out the total number of nodes in the link list. then the node at which the loop is starting will be : total number of nodes in the link list - total number of nodes in the loop + 1.

- superstart July 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can you please tell me how to get the length of link-list containing loop.

- ashish August 01, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use a HashMap. Start at head. Store the address (or hash code) of the code you are at currently in this HashMap. When you come to a node, check to see if the object is present in the HashMap. The first hit is where the cycle starts. It is O(n) but requires extra storage.

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

What about the collision in the hash table?? Does it not create a false positive??

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

N - number of nodes.
M - number of nodes in the cycle.

Then, the address of where the cycle start is the (N - M + 1) node.

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

It can be done in O(n) .... Two separate loops.
In one loop check where the fast pointer meets slow pointer.O(n)
After that keep slow pointer at that position and make fast pointer point to head.
Keep advancing both pointers one place at a time.O(n)
The place where they meet now is the start of loop.

- Anonymous September 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

How will second part work?

- Rishi T October 17, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

@ Geeks... isnt it shld be (k+1)th node in 3rd step....

1).Detect Loop using Floyd’s Cycle detection algo and get the pointer to a loop node.
2) Count the number of nodes in loop. Let the count be k.
3) Fix one pointer to the head and another to kth node from head.
4.)Move both pointers at the same pace, they will meet at loop starting node.

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

traverse hole array and hash the addresses of the node
if address already contained by hash table therefore loop start with this node ...

- rinku goel March 06, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. mark each node you have traversed.
2. the first "marked" node you encounter is the answer.

- ABHI.AKS.SINGH June 16, 2015 | 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