Adobe Interview Question






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

How are elements inserted in an array?
Is it in postorder, preorder, inorder or levelorder?

- DashDash July 23, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The next largest number is giong to be the smallst element of the tree rooted at its right child. Say the given number is at index i, get the right child from arr[2i+2]. New index is j = 2i+2;
keep looking for leftmost child arr[2j+1] untill arr[2j+1] is invalid. Next largest number would be arr[j];
But this is not in constant time :(

- endeav.rd July 24, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

next largest no: is the successor of given number..

if n is leaf or if it has only left child..then its parent is the successor..
else
move to right node and take left till v reach leaf..then this leaf node is its successor

- xclamation July 24, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

if ((n is leaf or n has only left child) {
if (n is the left child of parent){
then its parent is the successor..
} else { // n is the right child of parent
then go to its parent recursively.. till v reach a node which is a left node of its parent. then the parent of such a node is the successor.
}
}
else {
move to right node and take left till v reach leaf node. then this leaf node is its successor
}

- Anonymous July 24, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is not in constant time.

- fiddler.g July 24, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I am still confused as to how the elements are stored in the array?

- DashDash July 24, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I suppose that the BST is a complete tree and the indices are 0-based.
Say the given number is at index i. There are several cases we need to check:

1) If this node has a right child, so the sought node should be the leftmost node of the right subtree.
2) If this node hasn't a right child and it is a left child, so the parent node is the sought node.
3) If this node hasn't a right child and it is a right child, so we need to find the first parent which is a left child, and it's parent should be the sought node.

In all cases we can do some calculations (O(1)) in order to find the sought node.

1) The existence of the right child could be done with the following condition:
bool isRightChildExists = (2*i+2 < N), where N is the number of node in the tree. Now we must find the leftmost nodes index. this node should be located on the last or previous level. The hight of the tree is [log(N+1)] (say root is located on the 0 level) and the i-th node's level is [log(i+1)]. The root of the right subtree is 2*i+2 so we need to find the (d = [log(N+1)]-[log(i+1)]-1) d-th left child. It is also easy to see that the n-th left node's index could be found as follows: (2^(n+1))*(i+1)-1. So the sought node's index should be (2^(d+1))*((2*i+2)+1)-1. If this value is > N, so the sought node is located on the previous level and it's index is calculated in the same way, just by replacing d with d-1.

2) All left child's indices are odd, so if (i&1 != 0) then the sought node's index is [(i-1)/2].

3) The n-th (n>1) right child of the i-th node could be found as follows: (2^n)*(i+2)-2. So we need to find the maximum n for which there is some x (the first left child parent of i-th node) when (2^n)*(x+2)-2=i. That is i-th node is the n-th right child.
Therefore x=(i+2)/(2^n)-2. Easy to see that n is the greatest for which i+2 could be divided to 2^n. If we write the i+2 in the binary format, then the number of the last 0-s should the n. So 2^n=((i+2)xor(i+1))+1. Then x=(i+2)/(((i+2)xor(i+1))-1). The sought node is the parent of x: [(x-1)/2].

Any comments are welcome.

- fiddler.g July 24, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

the solution works but how did u get 2^n=((i+2)xor(i+1))+1 in last 2 lines???

- ankit September 09, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

OK, one mistake, it should be 2^(n+1). To solve this, just shift right by 1 bit.

We have equation like this: 2^(n+1) = (x xor (x-1)) + 1,
where n is the number of last consequent 0-s of x written
in binary format.

x                 = [binary data][1][n 0-s]
x-1               = [binary data][0][n 1-s]
x xor (x-1)       = [    0-s    ][1][n 1-s]
(x xor (x-1)) + 1 = [ 0-s and 1 ][n+1  0-s] = 2^(n+1).

- fiddler.g September 09, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

i have a doubt in last line, point1. When we say if sought node value comes > N, we replace d by (d-1) and do it again till we get value <=N.
looks like
while value > N
d = d-1
find sought node value
and here i am confused with time complexity ?
Can smbody explain where i m missing ?

- Anubhav January 21, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

correction :
do{
value = (2^(d+1))*((2*i+2)+1)-1
if value < N
{ break;
}
else
{ d = d-1;//find value for d = d-1
}
}while(1)

- Anubhav January 21, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi All

When an BST is implemented in an array. It is stored as level order. So on this condsideration. If we are given the index of the element. Say we have an array of node objest. Which has a data in it.

Now every elements child will be @ 2i and 2i+1st element in the array.
given an index i. find if the 2i+i th indexed element is null or not.
Case1 it is not null// has a right child
i = 2i+1;
while(arr[i]!=null)// geting the leftmost child of right sub tree.
i = 2*i;

return arr[tmp];

Case2 2i+1 is null // no child

while((i%2) == 1 )// checking if current index is right child or not
i=i/2;

return arr[i];

- kaustubh August 13, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

while(arr[i]!=null)// geting the leftmost child of right sub tree.
i = 2*i;
not an o(n) solution...

- ankit September 09, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

the solution has to be O(1) and not O(n)

- AK September 29, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

this is a inorder successor finding question i think

- sweetest September 15, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i dont get how they will store a BST in a array....normally array is used for heaps which are complete binary trees....for BST some nodes may be missing left/right childs....so if that is the case we have to assume that there is a value for node does not exist....can think of a constant time solution....O(n) can be like this

find_next_greater(given_number)
{
index=find_index(given_number);
if (array[2*index+1])==NAN //for null node 
   { return error("number has no larger number");}
else if(array[2*(2*index+1)]!=NAN)
   {find_next_greater(array[2*index+1]);}//assuming 2*i+1 is rchild and 2*i is lchild
else
   {print("next number is",array[2*index+1] ); exit;}
}

- sweetest September 15, 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