## Flipkart Interview Question for Site Reliability Engineers

• 0

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++;
}
/* 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
}``````

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

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

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.

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

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

no this won't do that.

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.

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;
}``````

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

did not understand your third point here.

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

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

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

@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.

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

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

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

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

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.

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

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

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.

### 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.