Adobe Interview Question for Software Engineer / Developers






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

Do level order traversal of the tree and check for the searching element:
int binarytree_search(node *root,int x)
{
int level=0;
node *temp;
queue<node*>q;
q.enqueue(root);
while(!q.empty())
{
temp=q.dequeue();
if(temp->data==x)
return level;
if(temp->left)
q.enqueue(temp->left);
if(temp->right)
q.enqueue(temp->right);
}
//if control reaches till here return -1
return -1;
}
TC: O(n)
SC:O(n)

- gnu July 21, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

where you updating level??

- technicalChaos July 21, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

U should do

int binarytree_search(node *root,int x) 
{ 
int level=0; 
node *temp; 
queue<node*>q; 
q.enqueue(root,level); 
while(!q.empty()) 
{ 
temp=q.dequeue(); //temp is an arrow which contains node addr,level
// i am assuming the appropriate typecasts are done in the code
if(temp[0]->data==x) 
return temp[1]; 
if(temp[0]->left) 
q.enqueue(temp[0]->left,++temp[1]); 
if(temp[0]->right) 
q.enqueue(temp[0]->right,++temp[1]); 
} 
//if control reaches till here return -1 
return -1; 
}

- vsohail09 January 11, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

as such there is no preference to using BFT/DFT etc...anyone of them will do the job as long as you are checking for the given value...that apart the code seems to assume that there is some other function for getting the level of a node, is it? then that function will just take a node data and return its level, right?

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

int findLevel(TREE * t,int levelCount,int data)
{

int left,right;
if(!t)
return -1;
if(t->data == data )
{
return 1;

}
else
{
left=findLevel(t->left,data);
right= findLevel(t->right,data);

return (left== -1) ? right: left;

}





}

- Idea July 22, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry no need of that level count argument

- Idea July 22, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Perfectly Working Code!!!!

int findLevel(TREE * t,int& levelCount,int data)
{

int left,right,temp;
if(!t)
return -1;
if(t->data == data )
{
levelCount=0;
return 1;

}
else
{

left=findLevel(t->left,levelCount,data);
right= findLevel(t->right,levelCount,data);
temp=(left == -1 ) ? right: left;
levelCount++;
return temp ;

}





}

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

BFS.

int getDepthOfItem(node* root, int x) {
queue<node*> q;
int i = 0;

q.push(root);

do {
if (q.front()->value == x) return (int) log2(i + 1);
if (q.front()->left) q.push(q.front()->left);
if (q.front()->right) q.push(q.front()->right);
q.pop();
++i;
} while (!q.empty());

return -1;
}

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

u r assuming tree is complete..plus its enq deq, not push,pop
as in ln2(i+1) works only if tree is complete

- vsohail09 January 11, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Reposting, whitespace preserved this time:

BFS.

int getDepthOfItem(node* root, int x) {
queue<node*> q;
int i = 0;

q.push(root);

do {
if (q.front()->value == x) return (int) log2(i + 1);
if (q.front()->left) q.push(q.front()->left);
if (q.front()->right) q.push(q.front()->right);
q.pop();
++i;
} while (!q.empty());

return -1;
}

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

int searchbtree(node *root, int value){

int left, right;

if(root == NULL) return -1;

if(root->data == value) return 0;

left = searchbtree(root->left, value);

right = searchbtree(root->right, value);

if(left >= 0) return (1 + left);

if(right >= 0) return (1 + right);

return -1;

}

- likie July 24, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

struct node
{
int data;

struct node*left;
struct node*right;
};

int getDepthOfItem(node* root, int x)
{
node t,w;

queue<node*> Q;

Q.push(root);
int level=0;

while(!Q.empty())
{
t=*Q.front();
Q.pop();

if(t.data==x)
return level;

Q.push(&t.right);
Q.push(&t.left);

level++;

}

return -1;

}

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

this is wrong..

- Anonymous July 29, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

the code is wrong @ Anonymous
else prove why its not wrong?

- lickie August 20, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
3
of 3 votes

think more..its bfs..u will be incrementing level even for same level nodes

- vsohail09 January 11, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

A simple DFS is sufficient for this

int findlevel(tree* root, int key , int level = 0)
{
if(!root) return -1;
if(root->data == key) return level;
if( findlevel(root->left,key,level+1) > 0 || findlevel(root->right,key,level+1))return level;
return -1;
}

I have not ran the code on system though.

- Addy September 12, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

opps


if( findlevel(root->left,key,level+1) > 0 || findlevel(root->right,key,level+1)>0)return level;

- Addy September 12, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

as we do not have any standard way to searching for elements in a binary tree, unlike a BST, we've to follow the standard method of searching the element by traversing the whole tree and one of the option would be to use level-order-traversal[BFS]

- paddy September 29, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

typedef struct tree
{
tree *right;
tree *left;
int data;
}node;

int findLevel(node *start)
{

}

- Manish Agarwal October 04, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Apply BFS and store a null pointer in a queue at the start. Now Whenever you find out a NULL pointer in queue push a NULL pointer again and increase the count. The final count will be our level.

typedef struct tree
{
tree *right;
tree *left;
int data;
}node;

int findLevel(node *start, int key)
{
   queue<node*> level;
   level.push(start);
   level.push(NULL);
   int count = 0; 
   while(level.size()>0)
   {
      start = level.front();
      level.pop();
      if(start == NULL)
      {
         count++;
         if(level.front()!= NULL)
             level.push(NULL);
         continue;
      } 
      if (start->data == key) return count;
      if(start->left)level.push(start->left);
      if(start->right)level.push(start->right);  
   }
   return -1;   
}

- Manish Agarwal October 04, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

if will be an infinite loop , if no element is found in the tree. Because the queue will always contains NULL so .we have to check
if one element is present and is NULL then should break the while loop .

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

Tree Traversl algo

leveller(node *x, int toSearch, int level)
{
if (x==NULL)
return;
if (x->key==toSearch)
    cout<<"Match at level "<<level;
leveller(x->left,toSearch,level+1);
leveller(x->right,toSearch,level+1)
}

- genie December 01, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

call---> search_depth(my_t, val, 0);

int search_depth(struct bin_t * my_t, int val, int dp)
{
if (!my_tr) {
return -1;
}
if (my_tr->val == val) {
printf("\n val : %d depth %d\n", my_tr->val, dp);
} else if (my_tr->val > val) {
pr_t(my_tr->l_ch, val, ++dp);
} else {
pr_t(my_tr->r_ch, val, ++dp);
}
return 0;
}

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

code tested ...
int level_find(Node *cur, int value, int level)
{
   if(!cur)
        return -1;

   if(cur->data == value)
        return level;

   int left = level_find(cur->left, value, level+1);
   int right = level_find(cur->right, value, level+1);

   if(left!=-1)
       return left;
   else if(right!=-1)
       return right;
   else
       return -1;
}

int level_find_wrap(Node *cur, int value)
{
   return(level_find(cur, value, 0));
}

- prep4Inter February 07, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int level_node(Tree *node, int level, int data)
{
if(node != NULL)
{
if(node->data == data)
return level;
else if(data < node->data)
level_node(node->left,level+1,data);
else
level_node(node->right,level+1,data);
}
else
return -1;
}

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

int levelSearch (Node *node, int data, int level = 1)
{
    if (!node)
        return -1;
    if (node->info == data)
        return level;
    return (MAX (levelSearch (node->left, data, level + 1), levelSearch (node->right data, level + 1))
}

- Siraj December 25, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

you should return level-1 instead of level to make root at level 0 as specified in the ques

- fabregas March 06, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

What about using preorder traversal??? It should work...If not found, it returns -1 else it returns the level of the node containing key.

int search(Tree *node, int key)
{
if(node)
{
if(node->data == key) return 0;
return (1 + search(root->left, key);
return (1 + search(root->right, key);
return -1;
}

return -1;
}

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

Hope its readable enough. Don't know why i didn't get the indentations i used while writing. sad

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

oops...the correct solution is...

int search(Tree *node, int key)
{
if(!node) return -1
else
{
if(node->data == key) return 0;
if(node->left) return (1 + search(root->left, key);
if(node->right) return (1 + search(root->right, key);
return -1;
}
}

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

really sorry for putting the code before testing. Have learned my lesson. finally the code after removing all the bugs is as follows:

int search(tree *node, char key)
{
int l=0;
static char ch= 'n';
if(!node)return -1;
else
{
if(node->data == key) ch='y';
else
{
l=search(node->left, key);
if(ch=='y')return l+1;

l=search(node->right, key);
if(ch=='y')return l+1;
}
return l;
}
}

- Ansh2rock March 20, 2011 | 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