## Amazon Interview Question for Software Engineer / Developers

• 1
of 1 vote

Country: India
Interview Type: Phone Interview

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

If a[] is the inorder traverse of BST, which means a[] is sorted, and arr[][] gives the root of each subtree, we may rebuild the BST by divide and conquer:
arr[0][a.length-1] gives the root of the BST, locate it in a[] as a[k],
we divide the problem into two smaller ones:
(1)rebuild left BST with a[0, k-1] and arr[][]
(2)rebuild right BST with a[k+1, n-1] and arr[][]
Below is code in Java:

``````public TreeNode rebuild(int[] a, int[][] arr){
return rebuild(a, 0, a.length - 1, arr);
}
private TreeNode rebuild(int[] a, int s, int e, int[][] arr){
if(s == e) return new TreeNode(a[s]);

int rootVal = arr[s][e];
TreeNode root = new TreeNode(rootVal);
int pos = Arrays.binarySearch(a, rootVal);
if(pos > s) root.left = rebuild(a, s, pos - 1, arr);
if(pos < e) root.right = rebuild(a, pos + 1, e, arr);
return root;
}``````

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

if the 1d array is inorder traversal of tree then it will be ok but what if the array given is either preorder or postorder traversal of the tree , how to handle them ???

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

Here you presume that a[] is sorted. if it is not, you cannot simply take the quarters of the matrix. but the logic remains the same:
find the treeroot in a[1,n]
then look for elements that have the same value. id a[i,j]==a[1,n], it means that ai and aj are in different subtrees. one of them is smaller than a[1,n], it will be in the left subtree, the other is bigger and will be in the right subtree. this way you will have the nodesets of the two subtrees.
solve recursively for the two subtree sets.

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

Can you please post an example to understand the question

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

simple one,

lets say size of a[] is 'm'
just get the element number arr[0][m] , this element is the root of the BST.
now read the a[] one by one and keep on adding the elements in BST in normal format.

basic rule for BST is , left elemt < root element < right element.

~Rohan

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

this is not good. a BST containing the same values can have many different structures. it all depends on the order in which you add the elements to the BST. even when you have a fixed root. noone said that a[] was ordered.

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

I presume a[] is sorted. Is it possible to solve this problem if a[] is not sorted?

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

Here is my sample data:
int a[] = {1,2,3,4,5,6,7,8,9,10};
int arr[10][10] =
{{0,0,0,2,0,0,0,0,0,4},
{0,1,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,3,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,5,0,0,0,6},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,7},
{0,0,0,0,0,0,0,0,0,8},
{0,0,0,0,0,0,0,0,0,9}};

Which should built a BST like this:

``````5
/		\
3		7
/		\	/	\
1		4	6	8
\				\
2				9
\
10``````

And here is my c++ code:

``````#include <iostream>

const int SIZE = 10;
int a[] = {1,2,3,4,5,6,7,8,9,10};
int arr[SIZE][SIZE] =
{{0,0,0,2,0,0,0,0,0,4},
{0,1,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,3,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,5,0,0,0,6},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,7},
{0,0,0,0,0,0,0,0,0,8},
{0,0,0,0,0,0,0,0,0,9}};

class BST
{
private:
struct node {
node *right;
node *left;
int data;
};
node* root;

public:
BST()
{
root = NULL;
}

void printInOrder();
void inorder(node *);
void insert(int);
void buildBST(int, int);
};

void BST::insert(int i)
{
node *n = new node;
n->left = n->right = NULL;
n->data = i;

if( root == NULL ) {
root = n;
}
else {
node *parent = NULL;
node *curr = root;
while( curr ) {
parent = curr;
if( n->data > curr->data ) {
curr = curr->right;
}else if( n->data <= curr->data ) {
curr = curr->left;
}else {
break;
}
}

if( n->data < parent->data ) {
parent->left = n;
}else if( n->data > parent->data ) {
parent->right = n;
}
}
}

void BST::printInOrder()
{
if( root == NULL ) {
return;
}

inorder(root);
}

void BST::inorder(node *n)
{
if( n == NULL ) {
return;
}
if( n->left != NULL ) {
inorder(n->left);
}
std::cout << n->data << " ";
if( n->right != NULL ) {
inorder(n->right);
}
}

void BST::buildBST( int s, int e ) {
int i = arr[s][e];
insert( a[i] );
if( s <= i-1 ) {
buildBST( s, i-1 );
}
if( i+1 <= e ) {
buildBST( i+1, e );
}
}

int main()
{
BST b;
b.buildBST( 0, SIZE - 1 );
b.printInOrder();
return 0;
}``````

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

-- A[] from i to j all values in the tree rooted at a[i][j] INCLUDING a[i][j] itself.
--A[] is sorted.

Steps:
1) Find a[i][j] in A[]--this index will be called idx
2) Everything from i to idx-1 is the left subtree of a tree rooted at a[i][j].
3)Everything from idx+1 to j is the right subtree of a tree rooted at a[i][j]

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

public TreeNode getTree(int[] a, int[][] arr,int i, int j){
return buildTree(i,j,a);
}
private TreeNode buildTree(int start,int end, int[] values){
if(start>end){
return null;
}
int mid=(start+end)/2;
Node n=new Node(values[mid]);
n.left=buildTree(start,mid-1,values);
n.right=buildTree(mid+1,end,values);
return n;

}

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.