You are on page 1of 13

Amazons most asked interview questions

May 15, 2014

1) K largest elements from a big file or array.


2) Find a triplet a, b, c such that a2 = b2 + c2. Variations of this problem
like find a triplet with sum equal to 0. Find a pair with given sum. All such
questions are efficiently solved using hashing.
3) Binary tree traversal questions like left view, right view, top view,
bottom view, maximum of a level, minimum of a level, children sum
property, diameter etc.
4) Convert a BST into a DLL and DLL to BST in place.
5) Vertical traversal of a Binary Tree.
6) Lowest Common ancestor in a Bianry Search Tree and Binary Tree.
7) Implement a stack with push(), pop() and min() in O(1) time.
8) Reverse a linked list in groups of size k.
9) Given two numbers represented by two linked lists, write a function
that returns sum list.
10) Rotate a matrix by 90 degree.
11) Some stack based questions like stock span problem, next greater
element.
12) Some Dynamic Programming problems like maximum sum subarray,
maximum sum subarray such that no elements are consecutive, edit
distance, assembly line scheduling.
13) Why Amazon?
14) Questions about projects done in previous company or final year.

Q1- Given an array of positive and negative numbers, arrange them in an alternate fashion such that
every positive number is followed by negative and vice versa maintaining the order of appearance.If the
count of negative numbers is more keep the extra at last in array
constraint : Space complexity should be O(1).
Q2- Given an array of random numbers, Push all the zeros of a given array to the right end of the array in
minimum possible swaps. Order of appearance doesnt matter. Print the total nonzero numbers and
minimum swaps needed to do so.
input : {1, 9, 8, 0, 0, -2, 0, 1, 6}.
output :
nonzero : 6
swaps : 2 (-2 as it is and swap 1 and 6 from first two zeros. )
18 were selected out of 55 for f2f round.
Round 1 F2F :
Q1- Two linked lists merge at one point, return the converging node. Constraint- O(1) space and O(m+n)
,where m and n are lengths of lists.
Link: http://www.geeksforgeeks.org/write-a-function-to-get-the-intersection-point-of-two-linked-lists/
Q2- Rotate the alternate levels of a binary tree.

Input:
1
/

2
/

\
5

4
/

3
/

\
7
/ \
12 13

/ \
11 10

8
Output:

1
/

3
/

2
\
7

6
/
13

/
4
\

\
5

\ /
\
12 8 11 10

First he asked to do it without recursion and then with recursion. O(n) time complexity.

Q3 Write an efficient function that takes two strings as arguments and removes the second string from
first string (in place). (Shifting not allowed)
input:
str1: aabcabcb
str2: abc
output: ab
Q4 Insert an element into a sorted link list which is having loop somewhere and duplicate elements as
well.
Q5 Make your own data structure. which inserts, deletes and gives a random number in O(1) time.
Hint : Use hash table and array.
Round 2 F2F :
Q1 You have n pencils, each having l length. Each can write 4 kilometers. After writing 4 kilometers it
has l/4 length. Then you can join 4 pencils which are having l/4 length and can make 1 pencil. You cant
make pencil of pieces if remaining pieces are 3 or 2 or 1 in number. And you can include these remaining
pieces whenever you need. Write a recursive relation independent of l,length of given pencil, for how
much one can write from n pencils. Write mathematical equation also.
Q2 Find

the largest sum subtree in a given Binary Tree.

Q3 Reverse level order traversal.


time complexity : O(n)

Input:
1
/

2
/
4
/
8

3
\
5

6
/ \
11 10

\
7
/ \
12 13

output:
13 12 11 10 8 7 6 5 4 3 2 1
You are permitted to use extra space and now print them in separate levels too.
Output:
13 12 11 10 8
7654

32
1

Given only a pointer to a node to be deleted in a


singly linked list, how do you delete it?
A simple solution is to traverse the linked list until you find the node
you want to delete. But this solution requires pointer to the head node
which contradicts the problem statement.
Fast solution is to copy the data from the next node to the node to be
deleted and delete the next node. Something like following.

struct node *temp = node_ptr->next;


node_ptr->data = temp->data;
node_ptr->next = temp->next;
free(temp);
Program:
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
/* Link list node */
struct node
{
int data;
struct node* next;
};
/* Given a reference (pointer to pointer) to the head
of a list and an int, push a new node on the front
of the list. */
void push(struct node** head_ref, int new_data)
{
/* allocate node */
struct node* new_node =
(struct node*) malloc(sizeof(struct node));
/* put in the data */
new_node->data = new_data;
/* link the old list off the new node */
new_node->next = (*head_ref);

/* move the head to point to the new node */


(*head_ref)
= new_node;
}
void printList(struct node *head)
{
struct node *temp = head;
while(temp != NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}
}
void deleteNode(struct node *node_ptr)
{
struct node *temp = node_ptr->next;
node_ptr->data
= temp->data;
node_ptr->next
= temp->next;
free(temp);
}
/* Drier program to test above function*/
int main()
{
/* Start with the empty list */
struct node* head = NULL;
/* Use push() to construct below list
1->12->1->4->1 */
push(&head, 1);
push(&head, 4);
push(&head, 1);
push(&head, 12);
push(&head, 1);
printf("\n Before deleting \n");
printList(head);
/* I m deleting the head itself.
You can check for more cases */
deleteNode(head);
printf("\n After deleting \n");
printList(head);
getchar();
return 0;
}

Move last element to front of a given Linked List

Write a C function that moves last element to front in a given Singly


Linked List. For example, if the given Linked List is 1->2->3->4->5, then
the function should change the list to 5->1->2->3->4.
Algorithm:
Traverse the list till last node. Use two pointers: one to store the address
of last node and other for address of second last node. After the end of
loop do following operations.
i) Make second last as last (secLast->next = NULL).
ii) Set next of last as head (last->next = *head_ref).
iii) Make last as head ( *head_ref = last)
/* Program to move last element to front in a given linked list */
#include<stdio.h>
#include<stdlib.h>
/* A linked list node */
struct node
{
int data;
struct node *next;
};
/* We are using a double pointer head_ref here because we change
head of the linked list inside this function.*/
void moveToFront(struct node **head_ref)
{
/* If linked list is empty, or it contains only one node,
then nothing needs to be done, simply return */
if(*head_ref == NULL || (*head_ref)->next == NULL)
return;
/* Initialize second last and last pointers */
struct node *secLast = NULL;
struct node *last = *head_ref;
/*After this loop secLast contains address of second last
node and last contains address of last node in Linked List */
while(last->next != NULL)
{
secLast = last;
last = last->next;
}
/* Set the next of second last as NULL */
secLast->next = NULL;
/* Set next of last as head node */

last->next = *head_ref;
/* Change the head pointer to point to last node now */
*head_ref = last;
}
/* UTILITY FUNCTIONS */
/* Function to add a node at the begining of Linked List */
void push(struct node** head_ref, int new_data)
{
/* allocate node */
struct node* new_node =
(struct node*) malloc(sizeof(struct node));
/* put in the data */
new_node->data = new_data;
/* link the old list off the new node */
new_node->next = (*head_ref);
/* move the head to point to the new node */
(*head_ref)
= new_node;
}

/* Function to print nodes in a given linked list */


void printList(struct node *node)
{
while(node != NULL)
{
printf("%d ", node->data);
node = node->next;
}
}
/* Druver program to test above function */
int main()
{
struct node *start = NULL;
/* The constructed linked list is:
1->2->3->4->5 */
push(&start, 5);
push(&start, 4);
push(&start, 3);
push(&start, 2);
push(&start, 1);
printf("\n Linked list before moving last to front ");
printList(start);
moveToFront(&start);
printf("\n Linked list after removing last to front ");

printList(start);
getchar();
}

Time Complexity: O(n) where n is the number of nodes in the given


Linked List.
1) Add a node at the front: (A 5 steps process)
The new node is always added before the head of the given Linked List. And newly added node becomes
the new head of DLL. For example if the given Linked List is 10<->15<->20<->25 and we add an item 5 at
the front, then the Linked List becomes 5<->10<->15<->20<->25. Let us call the function that adds at the
front of the list is push(). The push() must receive a pointer to the head pointer, because push must
change the head pointer to point to the new node (See this)
Following are the 5 steps to add node at the front.
/* Given a reference (pointer to pointer) to the head of a list
and an int, inserts a new node on the front of the list. */
void push(struct node** head_ref, int new_data)
{
/* 1. allocate node */
struct node* new_node = (struct node*) malloc(sizeof(struct
node));
/* 2. put in the data */
new_node->data = new_data;
/* 3. Make next of new node as head and previous as NULL */
new_node->next = (*head_ref);
new_node->prev = NULL;
/* 4. change prev of head node to new node */
if((*head_ref) != NULL)
(*head_ref)->prev = new_node ;
/* 5. move the head to point to the new node */
(*head_ref)
= new_node;
}
Four steps of the above five steps are same as the 4 steps used for inserting at the front in singly linked
list. The only extra step is to change previous of head.
2) Add a node after a given node.: (A 7 steps process)
We are given pointer to a node as prev_node, and the new node is inserted after the given node.
/* Given a node as prev_node, insert a new node after the given node
*/
void insertAfter(struct node* prev_node, int new_data)

{
/*1. check if the given prev_node is NULL */
if (prev_node == NULL)
{
printf("the given previous node cannot be NULL");
return;
}
/* 2. allocate new node */
struct node* new_node =(struct node*) malloc(sizeof(struct node));
/* 3. put in the data */
new_node->data = new_data;
/* 4. Make next of new node as next of prev_node */
new_node->next = prev_node->next;
/* 5. Make the next of prev_node as new_node */
prev_node->next = new_node;
/* 6. Make prev_node as previous of new_node */
new_node->prev = prev_node;
/* 7. Change previous of new_node's next node */
if (new_node->next != NULL)
new_node->next->prev = new_node;
}
Five of the above steps step process are same as the 5 steps used for inserting after a given node in
singly linked list. The two extra steps are needed to change previous pointer of new node and previous
pointer of new nodes next node.
3) Add a node at the end: (7 steps process)
The new node is always added after the last node of the given Linked List. For example if the given DLL
is 5<->10<->15<->20<->25 and we add an item 30 at the end, then the DLL becomes 5<->10<->15<>20<->25<->30.
Since a Linked List is typically represented by the head of it, we have to traverse the list till end and then
change the next of last node to new node.
Following are the 7 steps to add node at the end.
/* Given a reference (pointer to pointer) to the head
of a DLL and an int, appends a new node at the end */
void append(struct node** head_ref, int new_data)
{
/* 1. allocate node */
struct node* new_node = (struct node*) malloc(sizeof(struct
node));
struct node *last = *head_ref;

/* used in step 5*/

/* 2. put in the data */


new_node->data = new_data;
/* 3. This new node is going to be the last node, so
make next of it as NULL*/
new_node->next = NULL;
/* 4. If the Linked List is empty, then make the new
node as head */
if (*head_ref == NULL)
{
new_node->prev = NULL;
*head_ref = new_node;
return;
}
/* 5. Else traverse till the last node */
while (last->next != NULL)
last = last->next;
/* 6. Change the next of last node */
last->next = new_node;
/* 7. Make last node as previous of new node */
new_node->prev = last;
return;
}
Six of the above 7 steps are same as the 6 steps used for inserting after a given node in singly linked list.
The one extra step is needed to change previous pointer of new node.
4) Add a node before a given node
This is left as an exercise for the readers.
A complete working program to test above functions.
Following is complete C program to test above functions.
// A complete working C program to demonstrate all insertion methods
#include <stdio.h>
#include <stdlib.h>
// A linked list node
struct node
{
int data;
struct node *next;
struct node *prev;
};
/* Given a reference (pointer to pointer) to the head of a list
and an int, inserts a new node on the front of the list. */

void push(struct node** head_ref, int new_data)


{
/* 1. allocate node */
struct node* new_node = (struct node*) malloc(sizeof(struct node));
/* 2. put in the data */
new_node->data = new_data;
/* 3. Make next of new node as head and previous as NULL */
new_node->next = (*head_ref);
new_node->prev = NULL;
/* 4. change prev of head node to new node */
if((*head_ref) != NULL)
(*head_ref)->prev = new_node ;
/* 5. move the head to point to the new node */
(*head_ref)
= new_node;
}
/* Given a node as prev_node, insert a new node after the given node
*/
void insertAfter(struct node* prev_node, int new_data)
{
/*1. check if the given prev_node is NULL */
if (prev_node == NULL)
{
printf("the given previous node cannot be NULL");
return;
}
/* 2. allocate new node */
struct node* new_node =(struct node*) malloc(sizeof(struct node));
/* 3. put in the data */
new_node->data = new_data;
/* 4. Make next of new node as next of prev_node */
new_node->next = prev_node->next;
/* 5. Make the next of prev_node as new_node */
prev_node->next = new_node;
/* 6. Make prev_node as previous of new_node */
new_node->prev = prev_node;
/* 7. Change previous of new_node's next node */
if (new_node->next != NULL)
new_node->next->prev = new_node;
}
/* Given a reference (pointer to pointer) to the head
of a DLL and an int, appends a new node at the end
void append(struct node** head_ref, int new_data)

*/

{
/* 1. allocate node */
struct node* new_node = (struct node*) malloc(sizeof(struct node));
struct node *last = *head_ref;

/* used in step 5*/

/* 2. put in the data */


new_node->data = new_data;
/* 3. This new node is going to be the last node, so
make next of it as NULL*/
new_node->next = NULL;
/* 4. If the Linked List is empty, then make the new
node as head */
if (*head_ref == NULL)
{
new_node->prev = NULL;
*head_ref = new_node;
return;
}
/* 5. Else traverse till the last node */
while (last->next != NULL)
last = last->next;
/* 6. Change the next of last node */
last->next = new_node;
/* 7. Make last node as previous of new node */
new_node->prev = last;
return;
}
// This function prints contents of linked list starting from the
given node
void printList(struct node *node)
{
struct node *last;
printf("\nTraversal in forward direction \n");
while (node != NULL)
{
printf(" %d ", node->data);
last = node;
node = node->next;
}
printf("\nTraversal in reverse direction \n");
while (last != NULL)
{
printf(" %d ", last->data);
last = last->prev;
}
}

/* Drier program to test above functions*/


int main()
{
/* Start with the empty list */
struct node* head = NULL;
// Insert 6. So linked list becomes 6->NULL
append(&head, 6);
// Insert 7 at the beginning. So linked list becomes 7->6->NULL
push(&head, 7);
// Insert 1 at the beginning. So linked list becomes 1->7->6>NULL
push(&head, 1);
// Insert 4 at the end. So linked list becomes 1->7->6->4->NULL
append(&head, 4);
// Insert 8, after 7. So linked list becomes 1->7->8->6->4->NULL
insertAfter(head->next, 8);
printf("\n Created DLL is: ");
printList(head);
getchar();
return 0;

You might also like