Amazon Interview Question for Software Engineer / Developers


Team: Development
Country: India
Interview Type: In-Person




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

We can store the tree in follwoing format
(Node, leftSubtrre, rightSubtree) where subtree will be again in format of (Node, leftSubtree, rightSubtree).

e.g.
(10,(5, null,null),(15,(12, null, null),null))

- Santosh Diwase March 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

You can store it as JSON :).

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

We have to store two traversal one of the following
1> inorder, preorder
2> inorder, postorder

- NaiveCoder February 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

What happens if all nodes in the tree have the same value?
Guess it works only for trees with no duplicates.

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

File is sequential storage of data, So we have to check how we could implement a binary tree in array.
If my parent is at location n then left child = 2n + 1 and left child = 2n+2

0 1 2 3 4 5 6 7 8 9
0 is root of the tree,
left child of 0 is at location : 2*0 + 1 = 1
right child of 0 is at location : 2*0 + 2 = 2 and so on.

For reconstruction we need to remember one more thing parent = n/2 location if child is at n.

- ahmad February 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Note that if the tree is very sparse, this will be space inefficient, since you require 2^h items in the array where h is the height of the tree.

- Leigh March 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Storing the tree nodes in preorder is sufficient. Given the node values (in preorder), the first value is root of subtree, value next to root is left child and the next value greater than root is right child. Do this recursively and you'll create the tree.

- Mukesh Kumar February 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Storing the tree nodes in preorder is sufficient. Given the node values (in preorder), the first value is root of subtree, value next to root is left child and the next value greater than root is right child. Do this recursively and you'll create the tree.

- Mukesh Kumar February 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Its not a BST, so cant follow the approach above

- bytecode March 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

We can serialize the tree object and save it to file. When we deserialize, will get the same tree

- Hussain March 15, 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