Adobe Interview Question for Software Engineer / Developers






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

Pseudo code:
1. Two pointers start from head
2. One pointer advances one while the other advances two.
3.1 If two pointers meet, there is a loop
3.2 If pointer reachs the end, there is no loop
4. Find out how many nodes in the loop, say k.
5. Reset one pointer to the head, and the other pointer to head + k.
6. Move both pointers at the same pace, they will meet at loop starting node
7. Move one pointer till its next node is the loop starting node. It's the loop ending node
8. Set the next node of the loop ending node to fix the loop
9. Print the list

#include <stdio.h>

struct Node {
  int value;
  struct Node* next;
  Node(int v): value(v), next(NULL) {}
};

struct List {
  Node* head;
  List(): head(NULL) {}
  void push_back(Node *node) {
    if (head == NULL) {
      head = node;
      return;
    }
    Node *cur = head;
    while(cur->next != NULL) {
      cur = cur->next;
    }
    cur->next = node;
  }
};
int main()
{
  List l;
  // HEAD->1->2->3->'4'->5->6->7->'4'->5->6->7->'4'->5->6->7-> ...
  l.push_back(new Node(1));
  l.push_back(new Node(2));
  l.push_back(new Node(3));
  Node *four = new Node(4);
  l.push_back(four);
  l.push_back(new Node(5));
  l.push_back(new Node(6));
  l.push_back(new Node(7));
  l.push_back(four);
  Node *p1 = NULL, *p2 = NULL;
  while(1) {
    if (p1 == NULL && p2 == NULL) {
      // 1. Two pointers start from head
      p1 = l.head;
      p2 = l.head;
    } else {
      // 3.1 If two pointers meet, there is a loop
      if (p1 == p2)
        break;
    }
    // 3.2 If pointer reachs the end, there is no loop
    if (p2->next == NULL || p2->next->next == NULL) {
      printf("There is no loop in the list.\n");
      return 0;
    }
    // 2. One pointer advances one while the other advances two.
    p1 = p1->next;
    p2 = p2->next->next;
  }
  // 4. Find out how many nodes in the loop, say k.
  unsigned int k = 1;
  p2 = p2->next;
  while (p1 != p2) {
    p2 = p2->next;
    k++;
  }
  printf("There are %d nodes in the loop of the list.\n", k);
  // 5. Reset one pointer to the head, and the other pointer to head + k.
  p1 = l.head;
  p2 = l.head;
  for (unsigned int i = 0; i < k; i++)
    p2 = p2->next;
  // 6. Move both pointers at the same pace, they will meet at loop starting node
  while(p1 != p2) {
    p1 = p1->next;
    p2 = p2->next;
  }
  printf("node %d is the loop starting node.\n", p1->value);
  // 7. Move one of the pointer till its next node is the loop starting node.
  // It's the loop ending node
  p2 = p2->next;
  while(p2->next != p1)
    p2 = p2->next;
  printf("node %d is the loop ending node.\n", p2->value);
  // 8. Set the next node of the loop ending node to fix the loop
  p2->next = NULL;
  // 9. Print the list
  Node *cur = l.head;
  printf("The list without loop is: ");
  while(cur != NULL) {
    printf("%d ", cur->value);
    cur = cur->next;
  }
  printf("\n");
}

Output:
$ ./remove-loop-from-linked-list
There are 4 nodes in the loop of the list.
node 4 is the loop starting node.
node 7 is the loop ending node.
The list without loop is: 1 2 3 4 5 6 7

- leeym March 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Is it a C code, or C++ code.
My question is how to create a looped linked list. This part of code is not clear??

- Rahul March 08, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

C++, but only by a couple initializers.

- Anonymous March 08, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

soultion is correct as per pseudo code.. assuming the code adheres to it

- fabregas March 10, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nice solution

- sam March 10, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

To find where the node where the loop happens:

Take 2 pointers A B. A goes 1 node each time. B goes 2 nodes each time. If they meet there is a loop.

Now make B stop at where they meet. Restart A. This time both of them go 1 node at a time. When they meet again it is where the loop happens. Just make the next pointer NULL.

- Rayden March 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

No, this is not correct... I checked with an example given above, 1->2->3->4->5->6->7->4->5->6->7.
And it didn't gave the correct result, of finding the loop starting node.

- Rahul March 08, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

It does work. In your example they meet at 4. This is a solution from the Cracking Interview book. You can better understand the solution if you read the whole explanation there.

- Rayden March 08, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

1->2->3 and 3 back to 1 making loop. when you do they will meet at 1. Now as you said make the 1->next = null. Is this correct??

- ajayverma128 September 19, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Node n1 = n2 = head;
while (true) {
  n1 = n1.next;
  n2 = n2.next.next;
  if (n1 == n2) 
    break;
}
n1 = head;
while (n1.next != n2.next) {
  n1 = n1.next;
  n2 = n2.next;
}
n2.next=null

- Sathya March 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This one doesn't seems to be complete, as when the linked list is not looped, this code will fail.

- Rahul March 08, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

yes...but de question confirms dat linked list has a loop

- Sathya March 08, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi Sathya,
I think second while should be like

Node* Prev = NULL;
while (n1.next != n2.next) {
  Prev = n2;
  n1 = n1.next;
  n2 = n2.next;
}
Prev.next=null

- Tulley March 08, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

That doesn't handle the case where the loop starts at the head.

- gngr44 March 08, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi Tulley
Are u sure abt it cos I m getting correct answers for my code.Take this for eg

1-->2-->3-->4-->5
        |_______|
(3,4,5 is the loop)

now both ptrs n1,n2 start at 1
n1 points to 2,n2 points to 3
n1 points to 3,n2 points to 5
n1 points to 4,n2 points to 4

now n1 starts at 1,n2 at 4
n1 points to 2,n2 points to 5

both next are same so loop breaks and n2.next set to null

let me know if any thing wrong with this

- Sathya March 08, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Sathya: you are correct. i overlooked the condition in while loop. u r checking if both next are same and i saw it as if u are checking n1 and n2 are same.

- Tulley March 09, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool LoopCorrectn(nodeptr ptr)
{
nodeptr slow = ptr;
nodeptr fast = ptr;
nodeptr head = ptr;
nodeptr head2=NULL;
do
{
if(slow)
slow = slow->link;
else
return 1;

if(fast->link)
fast =fast->link->link;
else
{
return 1;

}
}while(slow!=fast);

slow=slow->link;
fast->link=NULL;
head2 = slow;

int len1 =0;
while(head)
{
len1++;
head=head->link;
}
int len2=0;
while(head2)
{
len2++;
head2=head2->link;
}
head=ptr;
head2=slow;
if(len1<=len2)
{
if(len1<len2)
{
for(int dlen =len2-len1;dlen>0;dlen--)
head2=head2->link;
}

}
else
{
for(int dlen =len1-len2;dlen>0;dlen--)
head=head->link;
}


while(head->link!=head2->link)
{
head = head->link;
head2 = head2->link;
}
head2->link=NULL;

fast->link=slow;
}

- Anonymous March 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It's not clear how the linked list is implemented. Single linked list or double linked list. Suppose single linked list:
1) head->elem1->elem2->elem3->head
2) head->elem1->elem2->elem3->tail->head

pp1 described above.
pp2:
if (tail != null && tail.next != null && tail.next = head){
tail.next = null;
}

- m@}{ March 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

sathya is wrong ...if you take 1->2->3->4->->5->6->7
|.....|
then n1->next never equals to n2->next??????

- vikas September 30, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

if u take loop as 5->6->7

- vikas September 30, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

it doesn't seem to work when the list looks like 1->2->3->4->5>6->7->8->9->7

- mns July 26, 2013 | 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