Vizury Interview Question for Software Engineer / Developers






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

Divide the elements(except the root) of each array into two sets-one that contains the elements that come in the left sub-tree in the order in which they occur in the array and the other that contains the elements that come in the right sub-tree again in the order in which they occur in the array.
Make left and right arrays for both the arrays. Then the left arrays of both as well as the right arrays of both should be exactly same.
For example:
Array1:10 5 20 15 30
Array2:10 20 15 30 5
left1 : 5
right1 : 20 15 30
left2 : 5
right2 : 20 15 30
therefore the arrays would produce the same BSTs

But for:
Array1:10 5 20 15 30
Array2:10 15 20 30 5
left1 : 5
right1 : 20 15 30
left2 : 5
right2 : 15 20 30
As right1 and right2 are not same, therefore different BSTs are produced.
Here is the code:

#include<stdio.h>
#define SIZE 1000

int checkMakeSameTree(int A[],int B[],int n)
{
    int root;
    int left[SIZE];
    int right[SIZE];
    int i,j,k;
    int leftsize = 0;
    int rightsize = 0;
    if(A[0] == B[0])
        root = A[0];
    else
        return 0;
    for(i=1;i<n;i++)
    {
        if(A[i] > root)
            right[rightsize++] = A[i];
        else
            left[leftsize++] = A[i];
    }
    j=k=0;
    for(i=1;i<n;i++)
    {
        if(B[i] > root)
        {
            if(k < rightsize && right[k] == B[i])
                k++;
            else
                return 0;
        }
        else
        {
            if(j < leftsize && left[j] == B[i])
                j++;
            else
                return 0;
        }
    }
    if(j == leftsize && k == rightsize)
        return 1;
    else
        return 0;
}

- Vipul June 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

nice

- @Vipul June 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The above suggest method doesnot work for the following condition
A: 8,3,6,1,4,7,10,14,13
B: 8,10,14,3,6,4,1,7,13

A left : 3,6,1,4,7
A right : 10,14,13

b left : 3,6,4,1 ,7
B right : 10,14,13

A left != B left
according to condition this should not for BST
which is wrong

- DNS June 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The above suggest method doesnot work for the following condition
A: 8,3,6,1,4,7,10,14,13
B: 8,10,14,3,6,4,1,7,13

A left : 3,6,1,4,7
A right : 10,14,13

b left : 3,6,4,1 ,7
B right : 10,14,13

A left != B left
according to condition this should not for BST
which is wrong

- DNS June 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The above suggest method doesnot work for the following condition
A: 8,3,6,1,4,7,10,14,13
B: 8,10,14,3,6,4,1,7,13

A left : 3,6,1,4,7
A right : 10,14,13

b left : 3,6,4,1 ,7
B right : 10,14,13

A left != B left
according to condition this should not for BST
which is wrong

- DNS June 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Vipuls method will work with slight modification,
Instead of being arrays being identical, they should form identical BST. And to test that, same function can be called recursively.
Let my try a psudocode.

check_identical(Array A, Array B){
if(A[0] != B[0]) {
return false;
}
if(A.size()!=B.size()){
return false;
}
if(A.size()==1){
return true; //only one elem in array and thats same in both
}
split(A, A_left, A_right);
split(B, B_left, B_right);
return check_identical(A_left, B_left)
&&
check_identical(A_right, B_right);
}

- Old Monk June 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@DNS, thanx for pointing out the mistake. I didn't check for enough test cases.
@Old Monk, I think your solution will work fine.

- vipul June 17, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Vipul

If u sort ur left and right arrays before comaprision with second Array ur solution will work fine ut then complexity will change in worst case to (n-1)log(n-1) i.e. nlogn..

Old Monk solution doesnt work to me.coz what if elements in right array are in this orer
A_Right 15 25 30
B_Right 30 15 25

I think his method will return false while answer shud br true

- Antiquity Blue November 22, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

wht abt this

f any one of the following conditions are false, then the BSTs are not identical else they are identical.

1: The first element of both arrays are same.
2: The relative ordering of all elements greater than the first element(root) is same in both the arrays.
3: The relative ordering of all elements lesser than the first element(root) is same in both the arrays.

Time Complexity O(n)
Space Complexity O(1)

- Anonymous February 18, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

How BST is represented by array? Preorder?

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

by using this array as input and insert the elements in this
order you can construct one BST. Find out these 2 arrays
construct the same type of BST or not

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

after you create the BST for both the sequences .. one option is we need to find out the INORDER,PREORDER or POSTORDER traversal , and compare them for the 2 BST's that you get from the given sequence .. exact replica of BST should have same in,pre,post order sequences .

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

Create BST with one array. For second array using insert / traverse function check second array element is matching with the stored data.

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

@vipul
The above suggest method doesnot work for the following condition
A: 8,3,6,1,4,7,10,14,13
B: 8,10,14,3,6,4,1,7,13

A left : 3,6,1,4,7
A right : 10,14,13

b left : 3,6,4,1 ,7
B right : 10,14,13

A left != B left
according to condition this should not for BST
which is wrong

- DNS June 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

this logic will work great for all the test cases if u take it a bit further

A: 8,3,6,1,4,7,10,14,13
B: 8,10,14,3,6,4,1,7,13

A left : 3,6,1,4,7
A right : 10,14,13
b left : 3,6,4,1 ,7
B right : 10,14,13
While(aleft<=3&&aright<=3 )
if(aleft ==3) // do the same for aright==3
{compare a left and bleft
if not = then NOT BST
}


If(A left >3!! A right > 3)
A left : 1
A right : 6,4,7

similarly for B
b left :1
b right: 6,4,7

- Shivam August 31, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Wht abt this

f any one of the following conditions are false, then the BSTs are not identical else they are identical.

1: The first element of both arrays are same.
2: The relative ordering of all elements greater than the first element(root) is same in both the arrays.
3: The relative ordering of all elements lesser than the first element(root) is same in both the arrays.

Time Complexity O(n)
Space Complexity O(1)

- Anonymous February 18, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

It's simple and can be done in O(n).

Create from first array a BST say aBST..
Once done when you are trying to create second BST, say bBST compare the value of the node (being inserted) with the aBST current node value, if equal insert it and move down else return false and you are done.

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

Creating a BST would take O(nlgn) not O(n).

- Vipul June 22, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

i thot of a similar approach but with a twist:

1. Create a BST with first input list say aBST, but each node should have an extra bool variable (say IsReached) set to false.

2. Now, start with the first element in second input list. The first element has to be the same as the root node of aBST, (otherwise we are done already), mark it as IsReached = true and goto step 3.

3. for each subsequent element in second input list, do a search in aBST. But condition is that you can test search only till the immediate children of any node where IsReached = true. If found, set the isReached flag = 1 & move on. Otherwise conclude & exit.

howzat?

- harleen June 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

One Approach is Pretty Clear by creating BST O(nlogn) then checking two tree for identical O(N) overall O(nlogn) ..i wonder if there exist O(N) Time & O(1) Space also without extra space .??

- WgpShashank June 27, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Build Two separate BST and Do the level Order traversal on both.If Two sequence wants to generate similar tree then Level Order traversal must be same. Correct if I am wrong?As I am beginner thats why not thinking much about space and time complexity.

- Rex July 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This will do

static boolean preorder(Node r1, Node r2){

if( r1.l == null && r1.r == null && r2.l == null && r2.r == null)
return r1.d == r2.d;
else if(
(r1.l == null && r2.l != null) || (r1.r == null && r2.r != null) ||
(r1.l != null && r2.l == null) || (r1.r != null && r2.r == null ))
return false;
else
return preorder(r1.l, r2.l) && r1.d == r2.d && preorder(r1.r, r2.r);
}

- Khan July 22, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

what about this

for (int i = 0; i < length - 1; i++)
{
if (arr1[i] < arr1[i+1] && arr2[i] < arr2[i+1])
return false;
if (arr1[i] > arr1[i+1] && arr2[i] > arr2[i+1])
return false;
}

return true;

- Alex August 05, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

nevermind...doesn't work =)

- Alex August 05, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

take first element as pivot of the quick sort.
for both the arrays position this element such that left elements are all smaller and right elements are all bigger.
Now compare the array: if they are equal then same BST else not.

eg:
1) Array1:10 5 20 15 30
Array2:10 20 15 30 5

after processing
Array1: 5 10 20 15 30
Array2: 5 10 20 15 30
Result: True


Array1:10 5 20 15 30
Array2:10 15 20 30 5
after processing
Array1: 5 10 20 15 30
Array1: 5 10 15 20 30
Result: false


Comments people. If this approach is correct

- Aditya June 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

if first root satisfies the condition then its always true.
else take second element and do the same thing recursively
for both arrays

- Aditya June 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think We can do it by using the array splitting in two part. one is smaller then first nbr and one is bigger the first nbr and seq should not be break for ex: (first element is common as it is root so ignore it)
array A = 57926813
then make two array from it 213 and 7968
Array B = 52713968
then make two array from it 213 and 7968
here both array are same so tree should be ideantical..

- Amit Singh Gaurav July 31, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If any one of the following conditions are false, then the BSTs are not identical else they are identical.

1: The first element of both arrays are same.
2: The relative ordering of all elements greater than the first element(root) is same in both the arrays.
3: The relative ordering of all elements lesser than the first element(root) is same in both the arrays.

Time Complexity O(n)
Space Complexity O(1)

- Anonymous February 18, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

For the two arrays to give same BST, the two arrays should contain the same elements. (much like anagrams).
sort the two and compare O(n lg n)
I hope their is a better solution to check for anagrams..

- Ayush June 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Are u drunk???
array A = 123456789
array B = 198765432

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

Probably he didn't understand "equality" here. Two BSTs have to be identical (not just containing the same elements).

- Anonymous May 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Probably he didn't understand "equality" here. Two BSTs have to be identical (not just containing the same elements).

- Anonymous May 01, 2012 | Flag


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