Amazon Interview Question for Software Engineer / Developers


Country: -
Interview Type: In-Person




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

go thru Split in splay trees

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

yeah, i think this would work..

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

As the tree was BST earlier, the parent of node would be greater than the rightmost child of node. So rearrange it like this ;

7
/ \
5 10
/ \ /\
2 6 8 11
/\
1 3

Suppose node is 2

then break 2 -> 5
Arrange 2 -> 3 -> 7.

New tree is

2
/\
1 3
\
7
/\
5 10
\ /\
6 8 11

- Srikant Aggarwal November 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

good solution

- nmc January 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@srikant Aggarwal Plzzz explain more what r u doing?????

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

Use rotation (as we use Left and Right rotation in AVL tree or Red Black tree)

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

Make the Node's parent as its right child.Rotation.

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

1. Delete the node just as you would in a normal BST (trivial if node is leaf; splice out the node if it has only one child i.e. replace with child, replace with successor if node has both children).

2. Now compare value of node with value in root. If node value > root value then original root becomes node's left child else it becomes its right child. Finally update root to node.

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

FAIL

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

What's wrong with the solution? Why FAIL?

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

Compare node with root, if greater than root, replace root with node and put root as leftmost child of right subtree.If less than root, replace root with node and put root as rightmost child in left subtree. Now rearrange the tree in bottom-up fashion.

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

What if we just find that node and swap values of root and node ??Its a Binary Tree and not a BST . DO we also have to take care that the left and right childs wud be same in the new Tree ?

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

How about this ?
1.Do a DFS from root to the node you are looking for
2. Reverse the direction of the path from Root to the found node and you will get a new binary tree with the newly found node as the root....

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

All Solution is fail...Correct solution is to delete the main node and insert the new node twice..twice becaue first will make the binary tree and second insertion will force it to convert it to bST (duplicate matching corollary)

- correct May 31, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

the answer is rotate to root:

template<typename T>
void insert_root(node*& h, T x) {
   if (!h) { h = new node{x}; return }
   if (x.key() < h->item.key()
      { insert_root(h->l, x); rotate_right(h); }
   else { insert_root(h->r, x); rotate_left(h); }
}

using the usual left or right rotations. ref: algorithms in c++, sedgewick and van wyk.

- ang October 31, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

exchange the root's value and the node's value

- Anonymous October 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

FAIL

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

why FAIL?
we are given a binary tree and not a BST.

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

can you pls explain what do you mean by transform ?

- Anonymous October 18, 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