Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

1. Find the length of 1st list says l1
2. Find the length of 2nd list says l2
3. Traverse the bigger list by (l1-l2) nodes from the beginning
4. From there, traverse both the lists in steps of 1 and check at each step if there is a merge by comparing the addresses of nodes
TC : O(m +n)

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

1. Find the length of 1st list says l1
2. Find the length of 2nd list says l2
3. Traverse the bigger list by (l1-l2) nodes from the beginning
4. From there, traverse both the lists in steps of 1 and check at each step if there is a merge by comparing the addresses of nodes
TC : O(m +n)

- Sadasiva Prasad January 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If two linked list merge their last or first node has to be same..
So here is the algo O(m+n)
1 Compare the first nodes of both lists if equal return true else go to step 2.
2 Traverse first list to get the last node (NON NULL)
3. Traverse second to get the last node (NON NULL)
4. compare the two nodes, if equal then lists are merged.

- loveCoding January 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@AmzFAILFacebookFailMSFTFail, The question is to find the merging point of the two linked list? or just to check is the two linked list merge. For the former one, the solution from "manan" would suffice, else solution from "Sadasiva" is the right one.

- Suriya January 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

p = head1 (bigger list); q = head2 (smaller list)
bool listMerge = false;
while(q!=null)
if(p->data== q->data) {
   p = p->next;
   q = q->next;
   listMerge = true;
}
else 
{
 // if this flag toggles then we found a mismatch hence return false
 // otherwise we have not yet found even the first node in the bigger list
 // which matched hence continue the traversal of bigger list
  if(listMerge == true)
     return false;
  else
     p = p->.next;
 }
 return true;

- Anuja January 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

node *mergecheck(node *l1,node *l2)
{
 int len=0;
 node *p;
 for(p=l1;p!=NULL;p=p->next,len++)
  ;
 for(p=l2;p!=NULL;p=p->next,len--)
  ;
 for(;len>0;len--,l1=l1->next)
  ;
 for(;len<0;l2=l2->next)
  ;
 for(;l1!=l2;l1=l1->next,l2=l2->next)
  ;
 return l1;
}

- Cairn January 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

only need to check the pointers of last nodes of two lists
if (last1 == last2 && last1 != null)
return true;
return false

- asuran April 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about using a hashmap here.
First list, parse the list and add to hashmap such that key=address of node and value=value of node.

For each of the second list elements' check if its address is already there in the hash table. If it is not available, then continue to the next element of the second linked list.

If at any point an entry is present in the hashtable=> Merge occurred.

Space Complexity O(m)
Time Complexity O(n).

- Pavan Dittakavi May 08, 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