Amazon Interview Question for Software Engineer / Developers


Country: United States




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

@hussain this code also changes the input lists, other way to do the same thing without altering i/p lists would be to
1. create an empty linked list
2. start moving a pointer on each linked list
3. in each iteration add the data in the new linkedlist
4. if any of the linked list has reached the tail then just append the other one to the new linked list

static LinkedList mergLinkedList(LinkedList first, LinkedList second){
        LinkedList lst = new LinkedList();
        LinkedList p1 = first;
        LinkedList p2 = second;
        while(p1!=null && p2!=null){
            lst.insert(p1.data);
            lst.insert(p2.data);
            p1=p1.next;
            p2=p2.next;
        }
        while(p1!=null){
            lst.insert(p1.data);
            p1 = p1.next;
        }
        while(p2!=null){
            lst.insert(p2.data);
            p2 = p2.next;
        }
        LinkedList.delete(lst, 0);
        return lst;
    }

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

public static ListNode mergeLists(ListNode l1, ListNode l2) {
        ListNode l3 = null;
        ListNode merged = null;
        
        if(l1 == null)
            return l2;
            
        if(l2 == null)
            return l1;
            
        l3 = l1;
        l3.next = l2;
        merged = l3;
        l1 = l1.next;
        l2 = l2.next;
        l3 = l3.next;
        
        while(l1 != null || l2 != null) {
            
            if(l1 != null) {
                l3.next = l1;
                l3 = l3.next;
                l1 = l1.next;
            }
            
            if(l2 != null) {
                l3.next = l2;
                l3 = l3.next;
                l2 = l2.next;
            }
        }
        
        return merged;
    }

where ListNode is something like:

public class ListNode {
 int val;
 ListNode next;
 ListNode(int x) {
     val = x;
     next = null;
 }
}

- luckless March 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int mergrList(Node first,Node second){
      Note temp1,temp2;
      temp1  = first.next;
      temp2 = second.next;
      while(first != null && second != null){
             first.next = second;
             second.next = temp1;
             first = temp1;
             second = temp2;
             temp1 = first.next;
             temp2 = second.next;
     }
     // If any one is empty copy the remaining as such in the link.

}

- hussain March 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Arent you looking for the list to be in sorted order as well?

- vencool March 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Arent you looking for the resultant list to be in sorted order as well?

- vencool March 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Below is a perfectly working code,, it will be in place merging as no additional memory is required (other than storing pointer)

#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>

struct _node{
int n;
struct _node *pNext;
};
typedef struct _node node;

enum _listType{
even =1,
odd
};
typedef enum _listType listType;

node* intermixList(node *pListOne, node *pListTwo);
node* createList (int iNodes, int evenOdd);
void displayList(node *pCurr);
void mixList(node *pListOne, node *pListTwo);
main()
{
	node *pListOne, *pListTwo, *pStartList, *pCurr, *pNode;
	int i;
	listType lt;
	pListOne = pListTwo = NULL;	
	lt = even;
	pListOne = createList(8, 2);
	pListTwo = createList(5, 1);
	displayList(pListOne);
	displayList(pListTwo);

	mixList(pListOne, pListTwo);
	displayList(pListOne);

}

node* createList (int iNodes, int evenOdd)
{
	int i, j, k ;
	node *pNode, *pStart, *pCurr;
	pNode = (node *) malloc( 1 * sizeof(node));
	pNode->n = ((evenOdd ==1) ?  2 : 1);
	pNode->pNext = NULL;
	pStart = pNode;

	k = (evenOdd == 1) ? 4:3;
	for (i = k, j = 1; j<=iNodes - 1; i=i+2, j++)
	{
		pCurr = (node *) malloc( 1 * sizeof(node));
		pCurr->n = i;
		pNode->pNext = pCurr;
		pNode = pCurr;
	}
	pNode->pNext = NULL;
	return pStart;
}

void displayList(node *pCurr)
{
	printf("Started printing the List\n");
	while(pCurr != NULL)
	{
		printf("\tValue in node is %d\n", pCurr->n);
		pCurr= pCurr->pNext;
	}
	printf("Ended printing the List\n\n");
	return;
}

void mixList(node *pListOne, node *pListTwo)
{
	node *pTemp1, *pTemp2, *pFirstList, *pSecondList;
	pFirstList = pListOne;
	pSecondList = pListTwo;
	if(pFirstList->pNext == NULL)
	{
		pFirstList->pNext = pSecondList;
		return;
	}
	else
	{
		while((pSecondList->pNext != NULL) && (pFirstList->pNext != NULL))
		{	
			pTemp1 = pFirstList->pNext;
			pFirstList->pNext = pSecondList;
			pTemp2 = pSecondList->pNext;
			pSecondList->pNext = pTemp1;
			pFirstList = pTemp1;
			pSecondList = pTemp2;
	
		}

		if (pFirstList->pNext == NULL)
		{
			pFirstList->pNext = pSecondList;
		}
		else if (pSecondList->pNext == NULL)
		{
			pTemp1 = pFirstList->pNext;
			pFirstList->pNext = pSecondList;
			pSecondList->pNext = pTemp1;	
		}	
	}
	return;
}

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

just forget about the enum, they are not used..

Also, i tried running the code provided by - luckless, and it is not working ..!!!

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

Hllw guys..Mm new to programming..How can I learnt?

- tango yompa April 23, 2017 | 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