Amazon Interview Question for Senior Software Development Engineers


Country: India
Interview Type: In-Person




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

if(head == null)
            return null;

        if(head.next == null)
            return head;

       ListNode curr = head;
       ListNode next = head.next;
       ListNode newHead = next;
       ListNode prev = null;

        while(true) {
            ListNode tmp = next.next;
            next.next = curr;
            curr.next = tmp;
            
            if(prev != null)
            	prev.next = next;
            
            prev = curr;            
            curr = tmp;
            
            if(curr == null || curr.next == null)
                break;
            
            next = curr.next;
        }
        
        return newHead;

- Anonymous April 27, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Recursive solution in Java...

public static Node swapPairsInList(Node head){
        if(head == null || head.next == null) return head;
        
        Node next = null;
        Node tail = null;
        
        if(head.next != null){
            next = head.next;
            tail = next.next;
            
            next.next = head;
            head.next = tail;
        }
        
        
        if(tail != null && tail.next != null){
            head.next = swapPairsInList(tail);
        }
        return next;
    }

- florentia April 27, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Recursive solution in Java

public static Node swapPairsInList(Node head){
        if(head == null || head.next == null) return head;
        
        Node next = null;
        Node tail = null;
        
        if(head.next != null){
            next = head.next;
            tail = next.next;
            
            next.next = head;
            head.next = tail;
        }
        
        
        if(tail != null && tail.next != null){
            head.next = swapPairsInList(tail);
        }
        return next;
    }

- florentia April 27, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static LLNode reversePair(LLNode head) {		
		if(head == null || head.next == null) 
			return head;
				
		LLNode next1 = head.next;		
		LLNode next2 = next1.next;
		
		next1.next = head;		
		head.next = reversePair(next2);
		
		return next1;
	}

- timpham2003 April 27, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

typedef struct list_item{
int dummy_data;
list_item *next;
}list_item;


list_item *
reverse_list(list_item *header)
{
list_item *first = header;
list_item *second = NULL;
list_item *previous = NULL;
list_item *new_header = header;

// we determine the new header first
if(first != NULL)
{
if (first->next != NULL)
{
// swap first and second item
second = first->next; // current second move to front
first->next = second->next; // current first should link to the third item
second->next = first; // second then link to current first item

new_header = second; // set the new header
previous = first; // remember new second position
}
first = first->next; // move the first pointer to third item or NULL
}
// now we deal with the rest
while(first != NULL)
{
if (first->next != NULL)
{
previous->next = first->next; // fix the previous item next pointer
second = first->next; // re-assign second
first->next = second->next; //
second->next = first;
previous = first;
}
first = first->next;
}
return new_header;
}

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

package in.blogspot.randomcompiler;

class Node {
    int data;
    Node next;
}

public class ReversePairs {

    public static void main(String[] args) {
        Node node = new Node();
        node.data = 1;
        Node head = node;
        for(int i=2; i<=101; i++) {
            Node temp = new Node();
            temp.data = i;
            node.next = temp;
            node=temp;
        }
        printNodes(head);

        head = reversePairs(head);
        
        printNodes(head);
    }
    
    private static Node reversePairs(Node head) {
        if(head == null || head.next == null) {
            return head;
        }
        Node headNextNext = head.next.next;
        head.next.next = head;
        Node newHeadNext = reversePairs(headNextNext);
        Node newHead = head.next;
        head.next = newHeadNext;
        return newHead;
    }

    private static void printNodes(Node head) {
        while(head != null) {
            System.out.print(head.data + " -> ");
            head = head.next;
        }
        System.out.print("null");
        System.out.println();
    }
}

Output:
1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 -> 11 -> 12 -> 13 -> 14 -> 15 -> 16 -> 17 -> 18 -> 19 -> 20 -> 21 -> 22 -> 23 -> 24 -> 25 -> 26 -> 27 -> 28 -> 29 -> 30 -> 31 -> 32 -> 33 -> 34 -> 35 -> 36 -> 37 -> 38 -> 39 -> 40 -> 41 -> 42 -> 43 -> 44 -> 45 -> 46 -> 47 -> 48 -> 49 -> 50 -> 51 -> 52 -> 53 -> 54 -> 55 -> 56 -> 57 -> 58 -> 59 -> 60 -> 61 -> 62 -> 63 -> 64 -> 65 -> 66 -> 67 -> 68 -> 69 -> 70 -> 71 -> 72 -> 73 -> 74 -> 75 -> 76 -> 77 -> 78 -> 79 -> 80 -> 81 -> 82 -> 83 -> 84 -> 85 -> 86 -> 87 -> 88 -> 89 -> 90 -> 91 -> 92 -> 93 -> 94 -> 95 -> 96 -> 97 -> 98 -> 99 -> 100 -> 101 -> null
2 -> 1 -> 4 -> 3 -> 6 -> 5 -> 8 -> 7 -> 10 -> 9 -> 12 -> 11 -> 14 -> 13 -> 16 -> 15 -> 18 -> 17 -> 20 -> 19 -> 22 -> 21 -> 24 -> 23 -> 26 -> 25 -> 28 -> 27 -> 30 -> 29 -> 32 -> 31 -> 34 -> 33 -> 36 -> 35 -> 38 -> 37 -> 40 -> 39 -> 42 -> 41 -> 44 -> 43 -> 46 -> 45 -> 48 -> 47 -> 50 -> 49 -> 52 -> 51 -> 54 -> 53 -> 56 -> 55 -> 58 -> 57 -> 60 -> 59 -> 62 -> 61 -> 64 -> 63 -> 66 -> 65 -> 68 -> 67 -> 70 -> 69 -> 72 -> 71 -> 74 -> 73 -> 76 -> 75 -> 78 -> 77 -> 80 -> 79 -> 82 -> 81 -> 84 -> 83 -> 86 -> 85 -> 88 -> 87 -> 90 -> 89 -> 92 -> 91 -> 94 -> 93 -> 96 -> 95 -> 98 -> 97 -> 100 -> 99 -> 101 -> null

- abhishek08aug April 29, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class ReversePairsLL {

	public static Node reversePairs(Node f) {
		if(f == null) return null;
		
		Node prev = null;
		Node cur = f;
		Node newHead = f.next;
		
		while(cur !=null && cur.next !=null) {
			Node temp = cur.next;
			cur.next = temp.next;
			
			if(prev == null) {
				temp.next = cur;
				prev = cur;
			} else {
				temp.next = cur;
				prev.next = temp;
				prev =cur;
			}
			cur = cur.next;
		}	
		return newHead;
	}
	
	public static void main(String[] args) {
		Node first = new Node(1);
		
		Node cur = first;
		for(int i=2;i <6;i++) {
			Node n = new Node(i);
			cur.next = n;
			cur = cur.next;
		}
		
		cur=first;
		while(cur != null) {
			System.out.print(" " + cur.info);
			cur = cur.next;
		}
		System.out.println();
		
		Node nhead = reversePairs(first);
		cur = nhead;
	
		while(cur != null) {
			System.out.print(" " + cur.info);
			cur = cur.next;
		}
		System.out.println();
	}
}

class Node {
	public int info;
	public Node next;
	Node(int data) {
		info = data;
		next = null;
	}
}

- Guru May 03, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Using recursion in Java.

public static Node reversePairsRec(Node n1){
		if(n1 == null) return null;
		if(n1.next == null) return n1;
			Node n2 = n1.next;
			n1.next = n2.next;
			n2.next = n1;
		n1.next = reversePairsRec(n1.next);
		return n2;

}

- King@Work May 05, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static void ReversePairs(int[] values)
        {
            var currentIndex = 0;
            var length = values.Length;
            int cache;

            while (currentIndex < length - 1)
            {
                cache = values[currentIndex + 1];
                values[currentIndex + 1] = values[currentIndex];
                values[currentIndex] = cache;

                currentIndex += 2;
            }
        }

- Mike May 07, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static Node Reverse(Node node)
        {
            var head = node;
            if (node == null)
            {
                return head;
            }
            
            var i1 = node;
            if (i1.Next == null)
            {
                return head;
            }
            var i2 = i1.Next;
            head = i2;

            Node previ1 = null;

            while (i2.Next != null)
            {
                i1.Next = i2.Next;
                i2.Next = i1;
                if (previ1 != null)
                {
                    previ1.Next = i2;
                }

                previ1 = i1;
                i1 = i1.Next;
                if (i1.Next == null)
                {
                    return head;
                }
                i2 = i1.Next;
            }
            return head;
        }
    }

- Roman A May 07, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This problem is not hard to solve but what Interviewer looking here is your skills to write Bug free program when it involves Pointers, Managing index and Boundary cases.
Think through for all boundary cases

- pc May 26, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <time.h>

typedef struct _list
{
	int data;
	_list *next;
} list;

int arr[11] = {78, 40, 25, 40, 17, 65, 3, 47, 54, 98, 23};

void preplist(list **head, int arr[], int size){
	list **cur = head;
	int i = size;
	while (i){
		list *temp = (list*)calloc(sizeof(list), 1);
		assert(temp != NULL);
		temp->data = arr[--i];
		temp->next = *cur;
		*cur = temp;
	}
}
void displist(list* head){
	printf("\nProduced List :: ");
	while (head){
		printf("%d->", head->data);
		head = head->next;
	}
	printf("NULL");
}
void smartReverse(list **head){
	if (*head == NULL || (*head)->next == NULL) 
		return;
	list *first = *head;
	list *second = first->next;
	smartReverse(&(second->next));
	first->next = second->next;
	second->next = first;
	*head = second;
}

int main(){
	list *head = NULL;
	preplist(&head, arr, SIZE);
	displist(head);
	smartReverse(&head);
	displist(head);
	getchar();
	return 0;
}

OutPut

Produced List :: 78->40->25->40->17->65->3->47->54->98->23->NULL
Produced List :: 40->78->40->25->65->17->47->3->98->54->23->NULL

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

#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <time.h>

typedef struct _list
{
	int data;
	_list *next;
} list;

int arr[11] = {78, 40, 25, 40, 17, 65, 3, 47, 54, 98, 23};

void preplist(list **head, int arr[], int size){
	list **cur = head;
	int i = size;
	while (i){
		list *temp = (list*)calloc(sizeof(list), 1);
		assert(temp != NULL);
		temp->data = arr[--i];
		temp->next = *cur;
		*cur = temp;
	}
}
void displist(list* head){
	printf("\nProduced List :: ");
	while (head){
		printf("%d->", head->data);
		head = head->next;
	}
	printf("NULL");
}
void smartReverse(list **head){
	if (*head == NULL || (*head)->next == NULL) 
		return;
	list *first = *head;
	list *second = first->next;
	smartReverse(&(second->next));
	first->next = second->next;
	second->next = first;
	*head = second;
}

int main(){
	list *head = NULL;
	preplist(&head, arr, SIZE);
	displist(head);
	smartReverse(&head);
	displist(head);
	getchar();
	return 0;
}

OutPut

Produced List :: 78->40->25->40->17->65->3->47->54->98->23->NULL
Produced List :: 40->78->40->25->65->17->47->3->98->54->23->NULL

- praveen pandit March 01, 2016 | 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