Amazon Interview Question for Software Engineer in Tests






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

Element *Merge2LL(Element **head1, Element **head2)
{
	Element *head3 = *head1;
	
	while(*head1 != null && *head2 != null)
	{
		Element *temp = *head1->next;
		Element *temp2 = *head2->next;
		*head1->next = *head2;
		*head2->next = temp;
		if(*head1->next == null)
			*head1 = head2;
		else
			*head1 = temp;
		*head2 = temp2;
	}
	
	if(*head2 != null)
	{
		*head1->next = *head2;
	}
	return head3;

}

- Confused January 13, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Neat solution. It takes care of the condition where first list goes out before the second one. The other case is handled automatically.

- Anonymous February 01, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Modifying the previous code, this code handles all the cases. The trick is to make head2 point to temp2, if temp is null.
So if second list is longer than that's taken care. If first list is longer, then temp will never be null.

Element *Merge2LL(Element **head1, Element **head2)
{
	Element *head3 = *head1;
	
	while(*head1 != null && *head2 != null)
	{
		Element *temp = *head1->next;
		Element *temp2 = *head2->next;
		*head1->next = *head2;
                if(temp != null)
		        *head2->next = temp;
                else
                        *head2->next = temp2;
		*head1 = temp;
		*head2 = temp2;
	}
        return head3;
}

- m.manish.kumar89 September 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

merge(p1,p2){
ptemp=p1->next;
p1->next=p2;
merge(p2,ptemp);
}

I ignore the boundary. add it yourself.

- Anonymous October 14, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

merge(p1,p2){
if(!p1 || !p2)
return;

ptemp=p1->next;
p1->next=p2;
merge(p2,ptemp);
}

- vb November 05, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Iterative
void merge(node *st1, node *st2)
{
while(st1 != NULL || st2 != NULL)
{
node *temp = st1->next;
st1->next = st2;
node *temp2 = st2->next;
st2->next = temp;
st1 = temp;
st2 = temp2;
}
}

- vb November 05, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

List *Merge(List **l1, List **l2) {
	List *head = *l1;
	List *temp1 = NULL, *temp2 = NULL;
	while( *l1 && *l2 ) {
		temp1 = (*l1)->next;
		temp2 = (*l2)->next;
		(*l1)->next = *l2;
		(*l2)->next = temp1;
		*l1 = temp1;
		*l2 = temp2;
	}
	return head;
}

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

Node* mergeMakeNew(Node *head1,Node *head2)
{
Node *head3,*temp1,*temp2;
int count = 0;
head3 = NULL;
temp1 = head1;
temp2 = head2;
while(temp1 != NULL || temp2 != NULL)
{
if(count % 2 == 0)
{
head3 = insert(head3,temp1->n);
temp1 = temp1->next;
}

else
{
head3 = insert(head3,temp2->n);
temp2 = temp->next;
}

count++;
}

if(temp1 != NULL)
{
while(temp1 != NULL)
{
head3 = insert(head3,temp1->n);
temp1 = temp1->next;
}
}

if(temp2 != NULL)
{
while(temp2 != NULL)
{
head3 = insert(head3,temp2->n);
temp2 = temp2->next;
}
}

return head3;
}

- Anonymous December 31, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Node* mergeMakeNew(Node *head1,Node *head2)
{
Node *head3,*temp1,*temp2;
int count = 0;
head3 = NULL;
temp1 = head1;
temp2 = head2;
while(temp1 != NULL || temp2 != NULL)
{
if(count % 2 == 0)
{
head3 = insert(head3,temp1->n);
temp1 = temp1->next;
}

else
{
head3 = insert(head3,temp2->n);
temp2 = temp->next;
}

count++;
}

if(temp1 != NULL)
{
while(temp1 != NULL)
{
head3 = insert(head3,temp1->n);
temp1 = temp1->next;
}
}

if(temp2 != NULL)
{
while(temp2 != NULL)
{
head3 = insert(head3,temp2->n);
temp2 = temp2->next;
}
}

return head3;
}

- Saurabh December 31, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public Node mergelists ( Node head1, Node head2 )
{
	Node Temp;
	Node Head_mln;
	Head_mln = head1;
	while ( head2.next != null && head1.next != null )
	{
		Temp = head2.next;
		head2.next = head1.next;
		head1.next = head2;
		head1 = head2.next;
		head2 = Temp;
	}
	if (head2.next == null)
	{
		Temp = head1.next;
		head1.next = head2;
		head2.next = Temp;
	}
	elseif (head1.next == null)
	{
		head1.next = head2;
	}
	return Head_mln;
}

- Royal December 25, 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