Amazon Interview Question for Software Engineer / Developers






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

/*  merging two linked lists */

node * Merge( node * L1, node * L2 )
{
    if( L1 == NULL )
        return L2;
    else if( L2 == NULL )
        return L1 
    else
    {
        node *p=L1, *q=L2, *r;
        
                while( p->next!= NULL && q!=NULL)
        {
            r = p->next;
            p->next = q;            
			p = q;
			q = r;
        }
        if( p->next == NULL )
        {
            p->next = q;
        }
        return L1;
    }
    
}

- siva.sai.2020 December 15, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

wat da crap is *r ? no logic!

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

L1: 1->2->3->4->5
L2: 6->7->8->9->10

P is pointer on L1 and q is a pointer on L2. r is temp pointer .

Resultant list L: 1->6->2->7->3->8->4->9->5->10

=====================================

Hello Sir, can you merge two lists without using third pointer ?

- siva.sai.2020 December 16, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Just imagine.. u declared a pointer *r,
p->next->next = r; and u r using r without declaring it...

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

start merging from the end

- ap January 20, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is a recursive implementation of alternateMerge.. the final result is available in the list pointer l1. So the function does not return anything.

The implementation assumes the lists are of equal size, so if any of the list ends, the alternateMerge ends, ignoring the rest of the elements in the other list.

void alternateMerge(node *l1,node *l2)
{
	if(l1==NULL || l2==NULL) return;
	node *temp;
	temp=l1->next;
	l1->next=l2;
	l2=l2->next;
	l1->next->next=temp;
	alternateMerge(temp,l2);
}

- a.khan December 23, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

gud job khan !

- Anonymous January 01, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

//Returns head of merged list.
// lists can be of unequal sizes.

Node alternateMerge(Node a, Node b)
{
    if (a==null) return b;
    if (b==null) return a;
     
    b.next = alternateMerge(a.next, b.next);
    a.next = b;
    return a;
}

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

Linked List. NA

- weijiang2009 February 06, 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