Amazon Interview Question for Software Engineer / Developers


Team: RCX
Country: United States
Interview Type: In-Person




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

struct node * constructBST(int a[],int low,int high){
            if(low > high) return NULL;
            int mid = (low+high)/2;
            struct node * root = newNode(a[mid]);
            root->left = constructBST(a,low,mid-1);
            root->right = constructBST(a,mid+1,high);
            return root;
}

newNode is the helper function which creates a new node and returns the struct node pointer.

- rkt March 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

C#:

public class BSTNode
{
	public int data;
	public BSTNode left;
	public BSTNode right;
}
public BSTNode root;

public void Wrapper(int[] arr)
{
	root=BuildBST(arr, 0, arr.Length-1);	
}
public BSTNode BuildBST(int[] arr, int lo, int hi)
{
	if(lo==hi)
	{
		return new BSTNode() { data=arr[lo] };	// Using automatic intializer C#		
	}
	int mid=lo + ((hi-lo)/2);
	BSTNode node= new BSTNode() { data=arr[mid] };
	
	node.left=BuildBST(arr, lo, mid-1);
	node.right=BuildBST(arr, mid+1, hi);

	return node;
	
}

- Anonymous January 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Small correction, for checking edge case:

C#:

public class BSTNode
{
	public int data;
	public BSTNode left;
	public BSTNode right;
}
public BSTNode root;

public void Wrapper(int[] arr)
{
	root=BuildBST(arr, 0, arr.Length-1);	
}
public BSTNode BuildBST(int[] arr, int lo, int hi)
{
	if(lo==hi)
	{
		return new BSTNode() { data=arr[lo] };	// Using automatic intializer C#		
	}
	int mid=lo + ((hi-lo)/2);
	BSTNode node= new BSTNode() { data=arr[mid] };
	
	if(mid!=lo) node.left=BuildBST(arr, lo, mid-1);
	node.right=BuildBST(arr, mid+1, hi);

	return node;
	
}

- Anonymous January 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Divide and conquer strategy for each subarray.

- Learner January 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Node *Convert( int *a , int low , int high)
{
if( low == high )
{
Insert( a[low]);
}
if( low < high )
{
int mid = (low + high )/2;
Insert( a[mid] );
Convert( a , low , mid-1 );
Convert( a , mid+1 , high );
return root;
}

- Anonymous February 24, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Node *Convert( int *a , int low , int high)
{
if( low == high )
{
Insert( a[low]);
}
if( low < high )
{
int mid = (low + high )/2;
Insert( a[mid] );
Convert( a , low , mid-1 );
Convert( a , mid+1 , high );
return root;
}

- sasi February 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void arraytotree(struct node **root, int array[], int left, int right) {
        if(left <= right) {
                int mid = (left + right)/2;
                insert(root,array[mid]);
                arraytotree(root, array, left, mid - 1);
                arraytotree(root, array, mid + 1, right);
        }
}


void insert(struct node **root, int data) {
        if(*root == NULL) {
                *root = (struct node *)malloc(sizeof(struct node));
                (*root)->left = NULL;
                (*root)->right = NULL;
                (*root)->data = data;
        }
        else {
                if(data <= (*root)->data)
                        insert(&((*root)->left),data);
                else
                        insert(&((*root)->right),data);
        }
}

If you find any problem with the solution let me know ..

- Ashwin March 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static Tree createBalancedTreeFromSortedArray(int[] array) {
		if(array == null || array.length == 0) {
			return null;
		}
		
		Node node = createNode(array, 0	, array.length-1);
		Tree tree = new Tree();
		tree.root = node ;
		return tree;
	}

private static Node createNode(int[] array, int start, int end) {
		int middleElement = (start+end)/2;
		System.out.println(start + " : " + end);
		int middle = array[middleElement];
		Node node = new Node(middle, null, null);
		
		if(start != end && start <= end) {
			Node leftnode = createNode(array, start, middleElement-1);
			Node rightnode = createNode(array, middleElement+1, end);
			
			node.leftChild = leftnode;
			node.rightChild = rightnode;
			
			System.out.println(node + " 's left chick is " + leftnode);
			System.out.println(node + " 's right chick is " + rightnode);
			
		} else if(start == end) {
			return node ;
		} else {
			return null;
		}

		return node;
	}

public static void main(String[] args) {
Tree tree = Tree.createBalancedTreeFromSortedArray(new int[]{0, 25, 50, 75, 85, 100, 125, 150});

- Tuco March 27, 2012 | 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