Amazon Interview Question for SDE1s


Country: United States
Interview Type: Written Test




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

There is well known solution for finding loops in the list.
Maintain two pointers fast and slow.
fast change as follows fast = fast.next.next
slow change as follows slow= slow.next
If they meet at some point then there is a loop.
If fast reach the end of the list then there is no loop.

- thelineofcode December 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

An implementation of the "fast and slow pointers" solution. The fast pointer moves two steps every time and the slow pointer only moves one step. If they meet at some point then there is a loop.

struct ListNode { 
	int value; 
	ListNode *next; 
};

bool hasLoops(ListNode *myList) { 
	ListNode *fast = myList, *slow = myList;

	while (true) {
		fast = fast->next;
		if (fast != NULL) {
			/* the second move */
			fast = fast->next;
		}

		slow = slow->next;

		if (fast == NULL) {
			return false;
		}

		if (fast == slow) {
			return true;
		}
	}
}

- luyangkk December 31, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In C++:

bool hasLoops(Node* head)
{
    if(head == NULL || head->next == NULL) return false;
    
    //at least two nodes or a one-node loop
    Node *pSlow = head->next, *pFast = pSlow->next;
    if(pFast == NULL) return false;

    //every time, move pFast two nodes and pSlow one node forward
    //if list has an end, pFast must get there first
    for(; true; pFast = pFast->next, pSlow = pSlow->next){
        if(pFast == pSlow) return true;

        pFast = pFast->next;
        if(pFast == NULL) return false;
    }

    return false;
}

- uuuouou December 31, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Java code for the same algo - thanks to @thelineofcode

public class LoopInLinkedList {
	
	private static class Node
	{
		Node next = null, prev = null;
	}
	
	public static void main(String[] args)
	{
		Node head = new Node();
		Node currentNode = head, prevNode = head;
		
		//Adding 12 nodes to the linked list. Modify the # to add more or less nodes
		for(int i=0; i<12; i++)
		{
			currentNode.next = new Node();
			prevNode = currentNode;
			currentNode = currentNode.next;
			currentNode.prev = prevNode;
		}
		
		//Comment out the following line to avoid the loop or point to a specific node in the linked list
		currentNode.next = currentNode.prev.prev.prev.prev.prev;
		
		Node fast = head, slow = head;
		while(true)
		{
			if(fast != null)
				fast = fast.next;
			else
				break;
			 
			if(fast != null)
				fast = fast.next;
			else
				break;
			
			if(slow != null)
				slow = slow.next;
			else
				break;
			
			if(fast == slow)
				break;
		}
		
		if(fast == slow)
			System.out.println("Found a loop!");
		else
			System.out.println("Could not find a loop!");
	}
}

- ruharish December 31, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class ListLoopQuestion { 

public static class ListNode { 

public int value; 

public ListNode next; 
} 

public static boolean hasLoops( ListNode myList ) { 
	if(myList == null || myList.next == null) {
		return false;
	}

ListNode slow = myList;
	ListNode fast = myList.next; // this pointer is going to travel twice as fast as the first

	// If we find a null value as a next element then we don’t have loops
while(fast != slow) {
		slow = slow.next;
		if(fast.next != null && fast.next.next != null)
			fast = fast.next.next;
		else
			return false;
	}
	
	// if the pointers meet at some point there is a loop in the list
	return true;
	
}

}

- everardo January 01, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

What is the algo to Find the starting point of the loop?

- pritish January 01, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

When the fast and the slow pointers meet, move any of the pointer to start of the linkedlist i.e head. Let the other pointer be where it is...
Now move both pointers by ONE till they meet again..
the node they meet is the starting point of the loop.

- Heramb January 09, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static boolean hasLoops( ListNode myList ) { 
    for (ListNode hare = myList, tortoise = myList; true; ) {
        if (hare.next == null || hare.next.next == null) {
            return false;
	}
        if (hare.next == tortoise || hare.next.next == tortoise) {
            return true;
	}
        hare = hair.next.next;
        tortoise = tortoise.next;
    }
}

- Anonymous January 01, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

github.com/techpanja/interviewproblems/blob/master/src/collections/customlinkedlist/LinkedListImpl.java

#hasLoop()

- techpanja January 02, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

careercup. com/question?id=5968086721101824

- ankit sharma January 03, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static boolean hasLoops(listNode mylist){
listNode slow=mylist;
listNode fast =mylist;
while(fast!=null && fast.next!=null){
slow=slow.next;
fast = fast.next.next;
if(slow==fast){
break;
}
if(fast == null || fast.next ==null){
return null;
}
slow=mylist;
while(slow!=fast){
slow=slow.next;
fast=fast.next;
}
return fast;
}

- fydeng January 04, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Finding a loop in linked list single iteration:
we can use HashSet implementation of java. Below is code:

Public boolean hasLoop(Node linkList) {
if (linkList == null)
	return false;
Set address = new HashSet();
while(linkList.next !=  null) {
	if(address.contains(linkList.next)){
		return true;
	} else 
		address.add(linkList.next);
}
return false;
}

- King007 January 05, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

what if the loop is bigger? I mean if the loop is not between the adjacent nodes. I am thinking if we put the addresses of each node in an array or hashmap as we traverse the linked list...and search if the address is already in the array before putting a new one..would cover the above scenario.

Thoughts?

- Praveen January 06, 2014 | 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