Amazon Interview Question for Software Engineer in Tests


Country: India




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

Make two passed through link list.
Pass1: Construct new link list.Store newNode corrosponding to each oldNode in a map.
Pass2. Assing random pointer to approprate node.

Code:

Node cloneNode(Node head){

Node t = head;
Node p = null;
Node nhead = null;

while(t!= null){
	Node tmp = new Node();
	tmp.data = t.data;
    	if(p!=null){
         p.next = tmp;
        }
         else{
          nhead= tmp;
         }
        mp.put(t,tmp);
        p = tmp;
        t= t.next;
} 

mp.put(null,null);

t= head;
tmp = nhead; 
while(t!= null){
  Node n = mp.get(t.random);
  tmp.random = n;
  t= t.next;
  tmp= tmp.next;

}

return nhead;

}

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

this post will help you
geeksforgeeks dot org archives 1155
could you share interview place????

- senthil October 13, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thanks for the solution, the question was asked in the online test for Amazon Bangalore.

- vinaysachdeva23 October 13, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The third solution is particularly good.

- eugene.yarovoi October 15, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

very good solution

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

Isn't this just a matter of running through the linkedlist twice. Once to create a list with next pointers and second pass for the random pointers?

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

Make two passed through link list.
Pass1: Construct new link list.Store newNode corrosponding to each oldNode in a map.
Pass2. Assing random pointer to approprate node.

Code:

Node cloneNode(Node head){

Node t = head;
Node p = null;
Node nhead = null;

while(t!= null){
Node tmp = new Node();
tmp.data = t.data;
if(p!=null){
p.next = tmp;
}
else{
nhead= tmp;
}
mp.put(t,tmp);
p = tmp;
t= t.next;
}

mp.put(null,null);

t= head;
tmp = nhead;
while(t!= null){
Node n = mp.get(t.random);
tmp.random = n;
t= t.next;
tmp= tmp.next;

}

return nhead;

}

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

Why need to go through the list twice? Why random pointers cannot be copied with the “next” pointer? What is the mp.get function? Can we do this without this function?

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

We need to go through loop 2 times because in first loop we dont have node corresponding to current node in new list .So in pass one construct nodes and put them in map mp

- akshay October 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I tried the same problem and I got the required output .
Below is the code:
#include<stdio.h>
#include"malloc.h"
#include"conio.h"

struct node
{
struct node *next;
struct node *random;
int val;

};

void printList(struct node *head)
{
while(head!=NULL)
{
printf("\nval = %d\t random = %d ",head->val,head->random->val);
head=head->next;
}
printf("\n");
}

void push(struct node **head_ref, int new_data)
{
struct node *new_node =
(struct node *)malloc(sizeof(struct node));
new_node->val= new_data;
new_node->next = *head_ref;
*head_ref = new_node;

}
struct node* copy_list(struct node *root)
{
printf("\ninside copy_list: ");
struct node *res=NULL;

struct node *cur = root;
struct node *next, *tail=NULL;


while(cur != NULL)
{
printf("val = %d\t random = %d ",cur->val,cur->random->val);
if(res == NULL)
{
push(&res, cur->val);

res->random = cur->random;

tail = res;
printList(tail);
}
else
{


push(&(tail->next),cur->val);

tail=tail->next;
tail->random = cur->random;


}
cur = cur->next;
}


printf("\nres printing");
printList(res);
return res;

}



int main()
{
printf("Given Linked List: ");
struct node *head = NULL;

/* Create following linked list
1->2->3->4->5 and
random of 1 -> 3
random 0f 2->1
random of 3->5
random of 4 ->5
random of 5->2*/

push(&head,5);
push(&head,4);
push(&head,3);
push(&head,2);
push(&head,1);
head->random = head->next->next;
head->next->random = head;
head->next->next->random = head->next->next->next->next;
head->next->next->next->random = head->next->next->next->next;
head->next->next->next->next->random = head->next;

printf("\nGiven Linked List: ");
printList(head);

head=copy_list(head);

printf("\nGiven Linked List After perform: ");
printList(head);
getch();
}

can anyone pls check and suggest if this code is correct ?

- yagami November 01, 2011 | 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