## Amazon Interview Question for SDE-2s

Country: India
Interview Type: Phone Interview

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

``````package tree;

public class BalancedTreeFromSortedArray {

public static void main(String[] args) {

int[] arr = { 1,2,3,4};

TreeBST tree = arrayToBST(arr, 0, arr.length-1);

preorder(tree);

}

static void preorder(TreeBST root) {

if(root!=null) {

System.out.println(root.data+" ");
preorder(root.left);
preorder(root.right);
}
}
static TreeBST arrayToBST(int[] arr , int start , int end ) {

if(start>end)
return null;

int mid = (start+end)/2;

TreeBST root = new TreeBST();
root = root.TreeBST(arr[mid]);

root.left = arrayToBST(arr, start, mid-1);
root.right = arrayToBST(arr, mid+1, end);

return root;

}

}

class TreeBST {

TreeBST left;
TreeBST right;
int data;

public TreeBST TreeBST(int data) {

TreeBST tree = new TreeBST();
tree.left = null;
tree.right = null;
tree.data = data;

return tree;
}
}``````

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

1. Get the middle item in the array and create a node.
2. Call this method again with the array on the left side of the middle element - assign the node to left.
3. Call this method again with the array on the right side of the middle element - assign the returned node to right.
4. return the node.

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

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

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

struct TNode* newNode(int data);

struct TNode* sortedArrayToBST(int arr[], int start, int end)
{
if (start > end)
return NULL;

int mid = (start + end)/2;
struct TNode *root = newNode(arr[mid]);

root->left =  sortedArrayToBST(arr, start, mid-1);

root->right = sortedArrayToBST(arr, mid+1, end);

return root;
}

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

return node;
}

void preOrder(struct TNode* node)
{
if (node == NULL)
return;
printf("%d ", node->data);
preOrder(node->left);
preOrder(node->right);
}

int main()
{
int arr[] = {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};
int n = sizeof(arr)/sizeof(arr[0]);

struct TNode *root = sortedArrayToBST(arr, 0, n-1);
printf("\n PreOrder Traversal of constructed BST ");
preOrder(root);

return 0;
}``````

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

The solution is simple as they didn't say abt the "BST" is balanced or not ...

so let us take the array is sorted in ascending order

Now keep adding all the elements to the right of the nodes

eg : {1,2,3,4,5}

node(1).right->node(2).right->node(3).right->node(4).right->node(5)
all node(x).left->null

this is also a BST :-)

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

``````private static void PreOrder(TreeClass tree)
{
if (tree != null)
{
Console.Write(tree.Data);
PreOrder(tree.Left);
PreOrder(tree.Right);
}
}

private static TreeClass ArrayToTree(int[] sortedArr, int start, int end)
{
if (start > end)
return null;

int mid = (start + end)/2;
return new TreeClass
{
Data = sortedArr[mid],
Left = ArrayToTree(sortedArr, start, mid - 1),
Right = ArrayToTree(sortedArr, mid + 1, end)
};
}

private class TreeClass
{
private TreeClass _left;
private TreeClass _right;
private int _data;

public TreeClass Left
{
get { return _left; }
set { _left = value; }
}
public TreeClass Right
{
get { return _right; }
set { _right = value; }
}

public int Data
{
get { return _data; }
set { _data = value; }
}``````

}

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

Use Modified Binary Search method to construct the BST.

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

Assuming there is Node class ready

``````public static Node createBSTFromSorted(int[] arr) {
if (arr.length > 0) return createBSTAux(arr, 0, arr.length-1);
else return null;
}

private static Node createBSTAux(int[] arr, int front, int end) {
if (front > end) return null;
int middle = front + (end - front) / 2;
}``````

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.