Amazon Interview Question for Software Engineer / Developers






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

First do a n = size+1...
Take binary representation of n.
Ignore MSB. Start from the MSB + 1th bit.
If 0 go left else go right.

Eg.

Node 1 --> Root
Node 2 --> 0010 --> Ignore leading one --> 0 -> L
Node 3 --> 0011 --> Ignore leading one --> 1 -> R

Node 10 --> 1010 --> Ignore leading one --> 010 --> L R L
Node 15 --> 1111 --> Ignore leading one --> 111 --> R R R
Node 9 --> 1001 --> Ignore leading one --> 001 --> L L R

- King_K May 15, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Excellent technique!

- Anonymous November 03, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nice Solution.

- Aalok July 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hats off!!..

- godzilaa October 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 0 vote

@maryfan
it is not given as a complete binary tree..
only complete tree is given.
and size is also given
first of all find height h=[(log base 2)(size)]
now we have to find its pos in its level from the left
pos=(size-2^(h-1))
represent pos in binary appending zeros to the left to make it h digits
like something as 0100010..0110101
now start traversing from the root reading this binary num from the left using convension
0=left
1=right
you will reach your destination in O(log n) time

- Anonymous July 23, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yo,

your pos=(size-2^(h-1)) is wrong, it should be pos =(size-(2^h-1)) try it with size = 8 and you will know

- Anon September 18, 2007 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

I am sorry...

1. The tree has no structure apart from being balanced
2. you have only access to those given structures
3. You have to position your insertion properly.


solution: Find no of levels, no of nodes in leaf level. Use a simple algorithm to position your insertion in leaf level. do the insertion...

- sundar February 17, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

sicne it is a complete binary tree.you can do insertion based on the binary values of the size.
1- 1
2 - 10
3 - 11
4 - 100


0 - for left and 1 - for right

if size is 4 then we need to insert the next in 100 position . that is root->left->left

- Anonymous December 28, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Assumption:
1> Its a Complete Binary Tree (Binary is not mentioned and A node with two
pointers can be used for n-ary tree.So binary is not obvious from the
question)
2> Gneral convention of forming Complete tree from left to right is assumed.
This means when adding a child to a node, we'll add left child prior to
its right.

Algorithm with O(LogN) : (I've used Java style)

addNode(Tree t, TreeNode newNode) {
int n= t.size;
TreeNode p=t.root;
int ncomplete=0; //Stores the size of the Binary tree with root=p
//if it would have been completely full
int nPrevComplete=0; //stores size of the completely full BT with
//hight one less to ncomplete
int Lr=0; //Stores the no of nodes required to have
//ncomplete from nPrevComplete
int lr=0; //Actual nodes supplied to nPrevComplete

nPrevComplete=func(n); // func(X) returns an integer by changing all
//bits to the left of most significant bit of
//X to 1. Example func(5)=7
//This is to be done. Looking for O(1) time
//solution. Any HELP!!!!

while(n>2){
nComplete=nPrevComplete;
nPrevComplete=nPrevComplete>>>1 // Logical Right shift

LR=nComplete - nPrevComplete;
// As an enhancement this => LR=nComplete XOR nPrevComplete
lr=n - nPrevComplete;

if( (lr>= (LR/2)) && (lr<LR) ){ //Move to right of p
p=p.rChild;
n=(nPrevComplete -1)/2 + lr - (LR/2);
}else{ //Move to left of p
p=p.rChild;
n=(nPrevComplete -1)/2 + lr - (LR/2);
}
}

//p is the node whose child will be new node
//Giving preference to left child as a convention
if(p.lChild == null){
p.lChild=newNode;
}else{
p.rChild=newNode;
}
}//End of method

//Note, variables 'LR' and 'lr' are redandant and are used for ease of understanding

- kg January 12, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

kq,
There is a cleaner way to do this. We assume we are dealing with a complete binary tree of length n and we are looking for the first empty node.

Let j be such that 2**j <= n < 2**(j+1). Express k=n-2**j in binary notation. Walk the tree to that first empty node by looking at k digit by digit. If the digit is 0 take the left tree, if it is 1 take the right tree.

- maryfran March 25, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The solution is neat but slightly wrong.2**j - 1 <=n<2**(j+1). k=n-(2**j-1).
For eg: If n=6 then j=2; Hence we have k= 6-2**2-1=3. This points to the location of the new node. Of course the no of digits to be considered is equal to j.

- aadi June 11, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

aadi,

Your solution is correct. BUT to make it more cleaner, instead of
2**j - 1 <=n<2**(j+1).

j = Math.ceil(lg(size));

and ...
k=n-(2**j-1).

- TheGreatOne September 18, 2007 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

If each node has a only left and right nodes, it is a binary tree

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

logn - it's the height of the balanced tree. So to have insertion time O(logn) we should always have balanced tree.

- zdmytriv January 28, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The main idea behind the question is that,

1. the tree is not balanced.
2. you have only access to those given structures

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

How about doing a breath first traversal and inserting elements where there is no child?

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

let suppose tree size is n // where n is number of node.
2*i + 1 = n ( if n is odd )
2*i = n ( if n is even )
according to ...
i = n / 2 or n - 1 /2.

- Deepak Garg October 03, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

But can any one explain how this binary representation works for tree traversals?
Their relationship

- Anonymous May 26, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

doctorinterview.com/A/9A/9A23.html

- Anonymous July 11, 2010 | 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