## Amazon Interview Question for Software Engineer / Developers

• 0

Team: SDE
Country: India
Interview Type: In-Person

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

Step1. Convert the BST in a sorted Array
Step2. Find the index of given node (say i)
Setp3 print all elements from i-k to i+k

Comment hidden because of low score. Click to expand.
0
{{{ 50 40 75 30 45 10 35 44 46 }} If the node is 45, and k=1 answer should be 44,46 and 40 Your answer - 44 and 46 Sort - 10,30,35,40,44,45,46,50,75
Comment hidden because of low score. Click to expand.
0
of 0 vote

Find The given node in this process you will identify the top elements which are above the node and which are k distance from a given node , next do a BFS from that node and read until K levels and then sort it (or) and do an inorder traversal for the just below k levels

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

Find the depth of the given node (say d). find the children from the root upto k-d levels leaving the branch that is towards the given node. traverse one step ahead towards the given node and now find the children upto k-d+1 levels leaving the branch that is towards the given node.
Do it until the given node is reached. Then find children at k levels below the given node.
Note: If k<d, traverse until d-k towards the given node and do the same
Complexity: O(k.d)

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

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

struct node
{
int data;
struct node *left;
struct node *right;
};

int max(int a, int b)
{
return a > b ? a : b;
}

int height(struct node *node)
{
if(node == NULL)
return 0;

return 1 + max(height(node -> left), height(node -> right));
}

void printkdistantdown(struct node *root, int k)
{
if(root == NULL)
return;

if(k == 0)
{
printf("%d ", root -> data);
return;
}

printkdistantdown(root -> left, k-1);
printkdistantdown(root -> right, k-1);
}

void printkdistantup(struct node *node, int k, struct node *root)
{
int h1 = height(node);
int h2 = height(root);

printkdistantdown(root, h2-h1-k);
}

struct node* newNode(int data)
{
struct node* node = (struct node*)
malloc(sizeof(struct node));
node->data = data;
node->left = NULL;
node->right = NULL;

return(node);
}

/* Driver program to test above functions*/
int main()
{

/* Constructed binary tree is
1
/ \
2 3
/ \ /
4 5 8
*/
struct node *root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
root->right->left = newNode(8);

printkdistantdown(root, 2);
printf("\n");
printkdistantup(root -> left -> left, 1, root);
}

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.

### 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.