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;
	}
}

- shukad333 October 05, 2014 | Flag Reply
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.

- Anonymous October 03, 2014 | Flag Reply
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;
}

- Abhijeet October 06, 2014 | Flag Reply
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 :-)

- MVVSK October 10, 2014 | Flag Reply
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; }
            }

}

- C# implementation October 18, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use Modified Binary Search method to construct the BST.

- algo2code January 01, 2015 | Flag Reply
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;
		Node head = new Node(arr[middle]);
		head.left = createBSTAux(arr, front, middle-1);
		head.right = createBSTAux(arr, middle+1, end);
		return head;
	}

- Anonymous January 27, 2015 | Flag Reply


Add a Comment
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.

Learn More

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.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More