Amazon Interview Question for Software Engineer / Developers






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

1) create a doubly linked list from binary tree - inplace
2) sort the list
3) create a binary search tree from the sorted list

- cougar_Cs October 17, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

do heapsort

- Anonymous September 03, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

psudo code:
bool sortTree(Tree* node)
{
static bool swapped=0;
Tree* left=node->left;bool sortTree(Tree* node)
{
bool swapped=0;
Tree* left=node->left;
Tree* right=node->right;

if(!node) return 0;

if(left)
{ if(left->data>node->data)
{swap(left->data,node->data);
swapped=1;}

}

if(right)
{ if(right->data<node->data)
{swap(right->data,node->data);
swapped=1;}
}

if(left&&right)
{ if(left->data>right->data)
{swap(left->data,right->data);
swapped=1;}
}


swapped=swapped||sort(left)||sort(right);

return swapped;

}

void doTreeSort(Tree* root)
{
while(sortTree(root)){}

}


Tree* right=node->right;

if(!node) return 0;
while(swapped)
{

if(left)
{ if(left->data>node->data)
{swap(left->data,node->data);
swapped=1;}

}

if(right)
{ if(right->data<node->data)
{swap(right->data,node->data);
swapped=1;}
}

if(left&&right)
{ if(left->data>right->data)
{swap(left->data,right->data);
swapped=1;}
}

swapped=swapped||sort(left)||sort(right);


}

}

- Jackie September 05, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

corrected:

bool sortTree(Tree* node)
{
bool swapped=0;
Tree* left=node->left;
Tree* right=node->right;

if(!node) return 0;

if(left)
{ if(left->data>node->data)
{swap(left->data,node->data);
swapped=1;}

}

if(right)
{ if(right->data<node->data)
{swap(right->data,node->data);
swapped=1;}
}

if(left&&right)
{ if(left->data>right->data)
{swap(left->data,right->data);
swapped=1;}
}


swapped=swapped||sort(left)||sort(right);

return swapped;

}

void doTreeSort(Tree* root)
{
while(sortTree(root)){}

}

- Jackie September 05, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

totally wrong
1, this space complexity is O(n)
2, it does not change to bst.

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

heap sort is the best answer as it's "in place"...

- ssn September 21, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

A heap is not a BST

- R November 18, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

the above solution keeps call sortTree once if he root, its left child, & right child are not in heap order, then will quit. Becayse || operator is used sort(left), sort(right) will not occur as swapped == 1.

Also, seems like this is intended to produce a heap, not a BST. The question is to create a BST.

Is there an algorithm that converts a given tree into BST inplace ?

- acoder September 22, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

hi ,

I think the following is the high level logic recursive for inplace conversion

func( Node node)
{
if(node==null)
return;
func(node->left);
func(node->right);
if(node->right==null && node->left==null)
return;
else if(node->right==null)
{
int k= larger (node.data and node->left.data)
if(k!=node.data)
{
node->left.data =node.data;
node.data=k;
}
}
else if(node->left==null)
{
int k= smaller(node.data and node->right.data)
if(k!=node.data)
{
node->right.data =node.data;
node.data=k;
}
}
else
{
int sm=smaller(node.data,node->left.data,node->right.data);
int max=larger(node.data,node->left.data,node->right.data);
int third=The element that is between the sm and max;
node.data=third;
node->left.data=sm;
node->right.data=max;
}
}

- chinni October 14, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

no proper answer so far !
any one out there with a logically correct and working answer ???

- klpd September 25, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

We don't need to sort the tree because we just need a Binary search tree.
Hence for each node, make sure that the left child is less than the current node and right child is greater than current node. Repeat for every node.

ConvertBinarySearchTree(NodeStruct *Node)
{
....temp1 = Node->left
....temp2 = Node->right
....if(Node->value < temp1->value) exchange(Node->value, temp1->value)
....if(Node->value > temp2->value) exchange(Node->value, temp2->value)
....ConvertBinarySearchTree(Node->left)
....ConvertBinarySearchTree(Node->right)
}

- Avinash October 05, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

That wouldnt be a BST, since it doesnt guarantee that all elements in left sub-tree is less than or equal to the root element.

- omri October 13, 2009 | 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