Amazon Interview Question


Country: India




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

A linked list that is standalone will not share its final node with any other list. So, traverse all the lists, and store all the final nodes as keys in a map, associating final nodes with (linked list head, count) pairs. In the end, iterate through all the key-value pairs, and when count == 1, include the linked list head in the output.

If there's a grand total of D elements distributed throuhout K lists, the runtime here will be O(D+K), which is optimal.

If you didn't want to use a map, you could get O(D + KlogK) with a sort.

- eugene.yarovoi November 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

cant properly understand what question demands.. please elaborate if possible..

- rockstar November 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

A standalone linked list has distinct start and end nodes.

Approach - Assume that all linked lists are unique to start with. Repeat for each linked list. Compare head with head of other linked lists. Navigate to the tail and compare with other tails. If head or tail is not unique, then not unique.

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

I agree to @rockstar's comment. The question is not clear. Pls elaborate.

- VJ November 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yeah... when the question itself does not sound clear, people start putting their own views and it sounds way too complicated than it actually is..

- Kary December 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

In any case it's better to sort. Need an array to store heads (M in no.) of resulting lists. Sort this array in MlogM. Now, for every original linked list (N in no.), find it's head(in logM) in just sorted heads. If found, traverse it completely to check with the original list, if they are same, add respective head to the answer.
Total time complexity: O(MlogM)+O(NlogM)+O(N*avg_no_in_merged_lists).

- ashu1.220791 November 10, 2012 | 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