Amazon Interview Question for Software Engineer / Developers


Country: India




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

u may also have a parent pointer

- kap October 09, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

smth along these lines should work:

// A[n] - array containing preorder traversal of a BST
root = new node;
t = root, t->parent = 0;
t->val = A[0], split = t;

for(i = 1; i < n; i++) {
    if(A[i] < t->val) {
        add_left_child(t, A[i]);
        split = t;
        t = t->left;
        continue;
    } 
    // if split  == 0 or A[i] < split->val then just add to the right
    // child of the current node 't' otherwise find another 'split'
    if(split != 0 && A[i] > split->val) {
        t = split;
        while(t->parent != 0) {
            if(A[i] < t->parent->val)
                break;
            t = t->parent;
        }
        split = t;
    }
    add_right_child(t, A[i]);
    t = t->right;
}

- pavel.em October 10, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

guys i have a doubt...
in preorder the first node will be the root right ?
so with first node as root we can construct BST considering other nodes in order using normal BST algo right ????

- mgr October 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

oops.... we need O(n) :P sorry

- mgr October 15, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

One can start with first element as root and just follow the BST rules(i.e. go to left if element is less than root, go to right otherwise) for rest of the elements.

- nagaraju October 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

CN(v)
	sn* n = new sn()
	n->d = v
	n->l = NULL
	n->r = NULL
	
BT(lo, hi)
	if(lo == hi)
		return CN(a[lo])
	v  = a[lo]
	for each lo +1 to hi
		if(a[i] > v)
			break
	n = CN(lo)
	n->l = BT(lo + 1, i-1)
	n->r = BT(i, hi)

- Prateek Caire October 24, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

But the problem with a pre-order traversal is that you wont get the exact tree because the order isn't stored in pre-order traversal

For example, consider the BST as follows

Root 6
Lchild of 6: 3
Rchild of 6:9
Lchild of 3: 1
Rchild of 3:4
Lchild of 9:7
Rchild of 9:10

Pre-order Traversal: 6,3,1,4,9,7,10

If the tree is modified such that 4 is the Rchild of 1and rchild of 3 is null then we get the same Pre-order traversal. How do you overcome such problems

- addidacodes March 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

public Node tree (char [] ary, int begin, int end) {
	
	if (begin > end || begin < 0) return null;
	
	Node n = new Node(ary[begin]);
	
	int halfIndex = getRightIndex (ary, ary[begin], begin + 1, end);
	n.left = tree (begin + 1, halfIndex - 1);
	n.right = tree (halfIndex, end);
	
	return n;
}



public int getRightIndex (char [] ary, char root, int start, int end) {
	
	for (int i = start; i <= end; i++) {
		if (ary[i] > root) {
			return i;
		}
	}
	return -1;
}

- Jeffrey October 11, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

this solution needs extra space for recursion stack ?

- Anonymous October 11, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Quadratic.

- Anonymous October 11, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

ary was not passed to tree function...

correction :-

n.left = tree (ary,begin + 1, halfIndex - 1);
n.right = tree (ary,halfIndex, end);

- atul October 13, 2011 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

If i did not misunderstand this problem,
With only preorder traversal one can not reconstruct tree.
e.g if for two node traversal is 1,2 there are two trees possible
1 root, 2 r. child,
2 root, 1 left child.

- Buch. October 11, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

if 1, 2 is preorder, then you know the root must come first. 1 is the root and 2 is the right. 2 must be the right child as this is a BST

- dc October 12, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Buch,
Since its given BST preorder(not necessarily BT), you can construct back (unique) BST.

- novice October 12, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Buch is right.

We can have many trees for 1 2 3 4 5 6 7 8

regardless whether you are given preorder, inorder or postorder.

You need inorder and one of post order, pre order

- Anonymous December 13, 2011 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

We can construct the BST using preorder and without using any extra space.


typedef vector<int> Array;


Tree* ConstructBST(Array preOrder)
{

int i = 0;

Tree* root = new Tree(preOrder[i]);
i++;

Tree* parent = NULL;

int min = INT_MIN;
int max = INT_MAX;

Tree* cur = root

while(i < preOrder.size() && cur)
{
//left branch
if(preOrder[i] > min && preOrder[i] < cur->val)
{
Tree* left= new Tree(preOrder[i]);
i++;

cur->lptr = left;

//set as parent
left->rptr = cur;

//set the max value
max = cur->val;

// set the current
cur = left;
}
//right branch
else if( preOrder[i] > cur->val && preOrder[i] < max)
{
Tree* right= new Tree(preOrder[i]);
i++;

// take the rptr node as there may be a chance that this may be pointing to it's parent
//node
Tree* parent = cur->rptr;

cur->rptr = right;


//set the min value
min = cur->val;


// set the parent node as current node's parent as we have to go back to that parent node
// once done with this node
if(parent)
{
right->rptr = parent;
}

// set the current
cur = right;



}
// move back to parent
else
{
if(cur)
{
//go back to parent node as we had set right node as parent for left branch

Tree* parent = cur->rptr;

cur->rptr = NULL;

cur = parent;

// set the min value
if(cur->lptr)
{
min = cur->lptr->val;
}
else
{
min = INT_MIN;
}

// set the max value
if(cur->rptr)
{
max = cur->rptr->val;
}
else
{
max = INT_MAX;
}

}
}

}


// set the parent nodes to NULL

while(true)
{
Tree* parent = cur->rptr;

if(parent != NULL && parent->lptr == cur)
{
cur->rptr = NULL;
cur = parent;
}
else
{
break;
}

}


return root;

}

- mp October 12, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

oops.. perserving white spaces

let's use right node as parent node and construct BST


typedef vector<int> Array;


Tree* ConstructBST(Array preOrder)
{
	
	int i = 0;
	
	Tree* root = new Tree(preOrder[i]);
	i++;
	
	Tree* parent = NULL;
	
	int min = INT_MIN;
	int max = INT_MAX;
	
	Tree* cur = root
	
	while(i < preOrder.size() && cur)
	{		
		//left branch
		if(preOrder[i] > min && preOrder[i] < cur->val)
		{
			Tree* left= new Tree(preOrder[i]);
			i++;
			
			cur->lptr = left;
			
			//set as parent	
			left->rptr = cur;
			
			//set the max value
			max = cur->val;
			
			// set the current 
			cur = left;
		}
		//right branch
		else if( preOrder[i] > cur->val && preOrder[i] < max)
		{
			Tree* right= new Tree(preOrder[i]);
			i++;
			
			// take the rptr node as there may be a chance that this may be pointing to it's parent 
			//node
			Tree* parent = cur->rptr;
			
			cur->rptr = right;
		
		
			//set the min value
			min = cur->val;
						
			
			// set the parent node as current node's parent as we have to go back to that parent node
			// once done with this node
			if(parent)
			{
				right->rptr = parent;
			}
			
			// set the current 
			cur = right;
			
			
			
		}
		// move back to parent
		else
		{
			if(cur)
			{	
				//go back to parent node as we had set right node as parent for left branch
				
				Tree* parent = cur->rptr;
				
				cur->rptr = NULL;
				
				cur = parent;	
				
				// set the min value
				if(cur->lptr)
				{
					min = cur->lptr->val;
				}
				else
				{
					min = INT_MIN;
				}
				
				// set the max value
				if(cur->rptr)
				{
					max = cur->rptr->val;
				}
				else
				{
					max = INT_MAX;
				}
				
			}
		}
	
	}

	
	// set the parent nodes to NULL 
	
	while(true)
	{
		Tree* parent = cur->rptr;
		
		if(parent != NULL && parent->lptr == cur)
		{
			cur->rptr = NULL;
			cur = parent;
		}
		else
		{
			break;
		}
		
	}
	
	
	return root;

}

- mp October 12, 2011 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

<pre lang="" line="1" title="CodeMonkey56755" class="run-this">//Create a BST of n elements
for(int i=0;i<n;i++){
node *node1 = new node;
node1->left=node1->right=NULL;
cin>>node1->data;

if(!root) { root=node1; continue; }
curr=temp=root;


while(curr){
temp=curr;
if(curr->data >= node1->data)
curr=curr->left;
else if(curr->data < node1->data)
curr = curr->right;
}

if(temp->data > node1->data) temp->left=node1; else temp->right=node1;

} </pre><pre title="CodeMonkey56755" input="yes">
</pre>

- Anonymous October 13, 2011 | 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