Amazon Interview Question for Software Engineer / Developers






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

1) Create the copy of every node in the list and insert it in original list between current and next node.
create the copy of A and insert it between A & B..
create the copy of B and insert it between B & C..
Continue in this fashion, add the copy of N to Nth node.
2) Now copy the arbitrary link in this fashion

original->next->random = original->random->next;
/*Traverse two nodes in every iteration*/

3) Now restore the original and copy linked lists in this fashion in a single loop.
original->next = original->next->next;
copy->next = copy->next->next;

- MSD May 26, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

cool .. This approach is neat

- Akhil May 29, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Too hard for me to grasp. Anyone could come forward to help me out solving this technique? Thanks.

- anonymous June 02, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Draw the figures of nodes on your paper. You will see. This approach is really cool.

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

nice algo. loved the solution especially the random stuff!!

- Abhi July 03, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Flawless!!

- Chandan Prakash July 05, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

your algorithm is beyond Godlike! :)

- Ultimate Solution July 08, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

ultimate solution

- anony July 29, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@MSD good one..

- inx August 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

excellent method

- sumesh September 29, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Another simple sol is to use recursion...

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

This method needs the list to be traversed 3 times,

Just wondering can we not do it in single pass...?

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

Brilliant...

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

fod diya yaar!

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

shouldn't original->next->random = original->random->next;

be

original->next->random = original->random;

- Nakul Agrawal August 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

clean and neat solution.

- bhuleskar April 13, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

won't work if the random pointer creates a cycle.

- srini June 03, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

saw the answer somewhere else few days back

1) create new link list and copy the data field and next pointer of the original link list.
2) not redirect the next pointer of each node of the original list to the corresponding node in new list'
3)new_node->random_prt = old_node->random_prt->next

- Anonymous May 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I've some doubt if it works or not. If you change NEXT of original list's nodes to point to corresponding copy, how could you ever iterate through the original list (in step 3) by iterating NEXT.

- anon June 02, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The key is to keep one-to-one relationship between the old list and the new list, either through hash or simply build two arrays to keep track.

Then when building the new list, copy val and set next prt properly to new next added node make new_node->random_ptr = old_node->random_ptr, ie new node's random ptr points to the old list as the new list is not completed yet.

Finally, using the saved one-to-one relationship, modify the random_ptr of new_node to the correct new node in the list

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

can't we just copy the nodes in two passes? In the first pass, copy the next pointers and in the second pass, copy the random pointers.

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

in reply to msd....in your approach are all the random pointers remain same as in the original list

- abc June 30, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes... Absolutely...
This little piece of modification would make it flawless...

original->next->random = original->random->next->next

BTW, good work, abc !..

- Sangeeth Kumar July 11, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

i didn't understand abc comments
i think msd solution was right.
Can you please elaborate ...

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

One more solution:
1. Create a copy of input by just following the next pointer
2. But when you are copying just keep on doing 2 things.
(i) Make sure that arbit pointer of the new node points to corresponding old node.
A' -> arbit = A
(ii) And the next pointer of old node point to the corresponding new node
A->next = A'
where A is old node and A' is new node.

3. Now traverse the new list again and do the below steps :
A'->arbit = A'->arbit->arbit->next;
A->next = A' ->next->arbit.

- Anonymous August 02, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This won't work.

Consider a list like this:

A -> B

A->arbit = B
B->arbit = A

Now, in the rd step, when you are at the second node (B'), your B'->arbit->arbit->next will be pointing to B.

- walletless August 10, 2011 | 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