Flipkart Interview Question for Site Reliability Engineers


Team: GGn
Country: United States
Interview Type: In-Person




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

1. Take two pointers, fast and slow. At each iteration, the fast pointer moves forward two nodes and the slow pointer moves forward one node. If the two pointers meet, that means there is a loop. Break out of the loop.
2. Now start a counter to find out the length of the loop. Initialize the counter to 0. Don't move the slow pointer, only move the fast pointer forward. For every iteration, increment the counter by 1. When the slow and fast pointers meet, the counter's value will give you the length of the loop.
3. Now that you have the length of the loop (loopLength), initialize both slow and fast pointers to the head of the linked list. Start a for loop and move the fast pointer forward loopLength number of times. At this point, the slow pointer points to the head and the fast pointer is loopLength nodes ahead of the slow pointer.
4. Now just move the slow and fast pointers forward till they meet. Where they meet is the beginning of the loop.

public Node getBeginingOfLoop() {
	Node slowIterator = this.head;    // slow pointer
	Node fastIterator = this.head;    // fast pointer
	/*
	 * slow pointer moves ahead one node
	 * fast pointer moves ahead two nodes.
	 * If the two pointers meet, break out of the loop
	 */
	while(fastIterator.getNext().getNext() != null) {
		slowIterator = slowIterator.getNext();
                fastIterator = fastIterator.getNext().getNext();
		if(slowIterator == fastIterator) {
			break;
		}
	}
	/*
	 * find out the length of the loop by keeping slow
	 * pointer stationary and moving the fast pointer
	 * forward till they meet
	 */
	int loopLength = 0;
	while(slowIterator != fastIterator) {
		fastIterator = fastIterator.getNext();
		loopLength++;
	}
	slowIterator = this.head;    // initialize pointers to head
	fastIterator = this.head;
	/* move the fast pointer loopLength nodes ahead */
	for(int iii = 0; iii < loopLength; iii++) {
		fastIterator = fastIterator.getNext();
	}
	/*
	 *now the slow pointer is at the head and fast pointer is
	 * loopLength nodes ahead of slow pointer.
	 * Start moving pointers ahead till they meet
	 */
	while(slowIterator != fastIterator) {
		fastIterator = fastIterator.getNext();
		slowIterator = slowIterator.getNext();
	}
	return fastIterator;    // where they meet is the starting of the loop
}

- Bonzo July 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

geeksforgeeks.org/archives/12225 code for detect and remove loop.

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

the diagram is not coming fine here..take it as 4 is joined to 7 ,and 0 joined to 9.

- sujita July 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

circular_LL(node *root){
p=root;
q=root;
do{
p=p->next;
q=q->next->next;
}while(p->data!=q->data && p!=null);
}

This code will show the meeting point and detects the circular linked list

- Anonymous July 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

no this won't do that.

- sujita July 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Let each node contains a flag 'isVisited' which is set to 'False' initially..

while traversing the list modify the 'isVisited' variable to 'True' in each node....

when you find the node with 'isVisited' variable with 'True'.. then it is circularly linked list and this node is the meeting point.

- cobra July 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. take two pointers fast & slow.
2. Move slow once & fast twice until they do not meet or fast becomes NULL(No cycle)
3. Move slow to root & move slow & fast one step at a time until they do not meet. This is the start of the cycle. Here is pseudo code:

findCycle(root)
{
	struct node* slow,*fast;
	slow=fast=root;
	while(fast && fast->next && fast!=slow)
	{
		fast=fast->next->next;
		slow=slow->next;
	}

	if(!fast || !(fast->next)) return NULL;

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

- Aashish July 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

did not understand your third point here.

- sujita July 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Fix fast at the meeting point, move slow to the root.
Now move both slow & fast once until they do not meet.

- Aashish July 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@shondik: Even I did not understand your 3rd point here. Consider a case where cycle exists for the entire linked list i.e., 1->2->3->4->1 (the first '1') then the fast and slow pointer will never meet and the last while loop will not come out.

- CVD July 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

download cracking coding interviews pdf . this questions is explained well in it.

- chad July 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

use 2 stacks
pop out stacks when found the meeting point
O(n) space and time Complexity

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

How about slow and fast = 3 * slow. They meet at a point which is the required meeting point

- aditya October 26, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Have two pointer which is pointed to head of the linked list initially.Then Increment the first pointer by 1, and increment the second pointer by 2 . If these 2 pointers then theres is a circle and meeting point is value to be print.

- Ani July 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

but I don't think they will meet at 4 ..which we need.

- sujita July 07, 2012 | 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