Amazon Interview | Set 17
Online Written round :
5 programming questions. You have to answer within 2 hours.
1. Swap two nodes in a linked list
2. Find kth smallest element in a binary search tree
3. Longest increasing subsequence in an array
4. One DP program
Face to face interviews:
1st round :
1. Find whether given tree is BST or not
2. Boundary traversal of a tree
3. Print the border nodes of the tree
2nd round:
1. There are n number of points in a two dimensional plane. Find two nearest points
2. There are n number of points in a two dimensional plane . Given a point find k nearest points to it.
3rd round:
1. Given a matrix with random numbers in it , If a location has 1, make all the elements of that row and column as 1
2. Given a matrix, find whether you can form the given number in
4th round:
1. Write a program to list all the possible words from the given set of data in the same order. ( eg : given word : nokiamobile O/P : nokia mobile : given word : samsung O/P : 1. SAMSUNG 2.SAM SUNG(considering sam as a word) )
2. Given two trees , find whether they are from same set of dataset or not.
3. Thread pool implementation.
This article is compiled by Yogesh. Many Many congratulations to Yogesh for his selection. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
How many questions needs to be correctly coded to qualify the interview round. Any idea?
I was able to correctly code 2 out of 4 in 2 hours !
Any ideas about how many questions should be attempted correctly so as to get to the interview call.
I got 4 questions, and was able to correctly complete only 2!
K-TH smallest element in tree
int size(node *root)
{
if(root==NULL)
return 0;
else
return (size(root->left)+1+sizeof(root->right));
}
int smallest(int k,node *root)
return smallest(k,root->left);
{
int count;
count=size(root->left)+1;
if(k==count)
return (root->data);
else if(k
else
return smallest(k-count,root->right);
}
in online written round second question is just inorder traversal and count kth element
Swapping two nodes of linked lists by swapping the pointers of the nodes.
CODE:
#include
#include
#include
struct node{
int data;
node *link;
}*start;
void insert(int data)
{
node *temp=(struct node*)malloc(sizeof(struct node));
temp->data=data;
if(start==NULL)
{
temp->link=NULL;
start=temp;
}
else
{
node *p;
p=start;
while(p->link!=NULL)
p=p->link;
p->link=temp;
temp->link=NULL;
}
}
int lengthoflist(node* root)
{
int count=1;
node *p=start;
while(p!=NULL)
{
count++;
p=p->link;
}
return count;
}
void swap(int pos1,int pos2)
{
int length_list=lengthoflist(start);
node *p,*r,*p1,*r1,*temp;
p=r=p1=r1=start;
if(pos1<length_list && pos2<length_list && pos1link;
}
temp=r->link;
r->link=p->link;
p->link=temp;
r1->link=p;
p1->link=r;
delete temp;
return;
}
else
{
while(pos1!=1)
{
--pos1;
p1=p;
p=p->link;
}
while(pos2!=1)
{
--pos2;
r1=r;
r=r->link;
}
temp=r->link;
r->link=p->link;
p->link=temp;
p1->link=r;
r1->link=p;
delete temp;
return;
}
}
else
{
printf("Enter valid positions to swap\n");
return;
}
}
void display(node* root)
{
node *p=start;
while(p!=NULL)
{
printf("%d----->",p->data);
p=p->link;
}
printf("\n");
return;
}
int main()
{
start=NULL;
int no,pos1,pos2,ch=0;
while(ch!=4)
{
printf("------------------------MENU-----------------------\n1.INSERT\n2.SWAP\n3.DISPLAY\n4.EXIT\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("enter the data to be inserted\n");
scanf("%d",&no);
insert(no);
break;
case 2:
printf("enter the positions that you wanna swap\n");
scanf("%d %d",&pos1,&pos2);
swap(pos1,pos2);
break;
case 3:
display(start);
break;
case 4:
break;
}
}
return 0;
}
SWAPPING OF TWO NODES OF LINKED LISTS
Approach:Basically there are two approaches to solve the problem:to swap the values of the nodes or to swap the pointers.In this SET i have just swapped the values of the nodes without swapping the pointers which will be shown in the later posts.
STEPS:1.Calculate the size of the linked lists and pass the positions of the nodes which you want to swap.
2.If the positions are more than the size of the list then print enter the correct positions and check if the second position is more than the first one.
3.Now take two pointers and take them to the desired positions of the nodes of whose values you want to swap and swap the values.
CODE:
#include
#include
#include
struct node{
int data;
node *link;
}*start;
void insert(int data)
{
node *temp=(struct node*)malloc(sizeof(struct node));
temp->data=data;
if(start==NULL)
{
temp->link=NULL;
start=temp;
}
else
{
node *p;
p=start;
while(p->link!=NULL)
p=p->link;
p->link=temp;
temp->link=NULL;
}
}
int lengthoflist(node* root)
{
int count=1;
node *p=start;
while(p!=NULL)
{
count++;
p=p->link;
}
return count;
}
void swap(int pos1,int pos2)
{
{
int temp,length_list=lengthoflist(start);
node *p,*r;
p=r=start;
if(pos1
if(pos1==1)
{
while(pos2!=1)
{
--pos2;
r=r->link;
}
temp=p->data;
p->data=r->data;
r->data=temp;
return;
}
else
{
while(pos1!=1)
{
--pos1;
p=p->link;
}
while(pos2!=1)
{
--pos2;
r=r->link;
}
temp=p->data;
p->data=r->data;
r->data=temp;
return;
}
}
else
{
printf("Enter valid positions to swap\n");
return;
}
}
void display(node* root)
{
node *p=start;
while(p!=NULL)
{
printf("%d----->",p->data);
p=p->link;
}
printf("\n");
return;
}
int main()
{
start=NULL;
int no,pos1,pos2,ch=0;
while(ch!=4)
{
printf("------------------------MENU-----------------------\n1.INSERT\n2.SWAP\n3.DISPLAY\n4.EXIT\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("enter the data to be inserted\n");
scanf("%d",&no);
insert(no);
break;
case 2:
printf("enter the positions that you wanna swap\n");
scanf("%d %d",&pos1,&pos2);
swap(pos1,pos2);
break;
case 3:
display(start);
break;
case 4:
break;
}
}
return 0;
}
Q:Swapping the two nodes of a linked list
SOLUTION:http://sahilalipuria.wordpress.com/2013/01/20/swapping-two-nodes-of-a-singly-linked-lists-set-1/
1. Swap two nodes in a linked list
Standard procedure. swap(struct node** a, struct node** b)
2. Find kth smallest element in a binary search tree
class NodeNumPair {
int numNodes;
int kthElement;
}
NodeNumPair findKth(Node root, int k) {
if(k==1 || root.left == null)
return new numNodes(-1,root.value};
left = findKth(root.left, k);
if(left.numNodes == -1)return left;
if(left.numNodes == k-1) return new numNodes(1,root.value};
return findKth(root.right, k-left.numNodes-1);
}
3. Longest increasing subsequence in an array
DP Problem. Available here on geeksforgeeks
4. One DP program
Face to face interviews:
1st round :
1. Find whether given tree is BST or not
Check recursively if left.value <= cur.value <= right.value
2. Boundary traversal of a tree
Perform partial inorder, while passing a flag that says whether to go left or right
void boundary(Node node, boolean left) {
if(node == null) {
return(node;
if(left) {
if(node.left != null)
boundary(node.left, left);
else boundary(node.right, !left);
}
System.out.println(node.value);
if(!left) {
if(node.right != null)
boundary(node.right, !left);
else boundary(node.left, left);
}
}
3. Print the border nodes of the tree
Not sure of the difference between this and the boundary traversal above.
2nd round:
1. There are n number of points in a two dimensional plane. Find two nearest points
Compute the distance between two points, while storing the lowest.
2. There are n number of points in a two dimensional plane . Given a point find k nearest points to it.
Compute the distance between two points while storing each value. Perform a fast k-th element selection using partition algo.
3rd round:
1. Given a matrix with random numbers in it , If a location has 1, make all the elements of that row and column as 1
2. Given a matrix, find whether you can form the given number in
4th round:
1. Write a program to list all the possible words from the given set of data in the same order. ( eg : given word : nokiamobile O/P : nokia mobile : given word : samsung O/P : 1. SAMSUNG 2.SAM SUNG(considering sam as a word) )
2. Given two trees , find whether they are from same set of dataset or not.
3. Thread pool implementation.