IBM Interview Question for Software Engineer / Developers






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

just add a condition

(if(root->value)>vmin && root->value<vmax){
inorder(root->left);
print(root->value);
inorder(root->right);
}

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

In the above code it wont enter the loop in the first place if vmin is say 5 and the root has value say 3..

so i would reccommend not printing any values in inorder till root->value > vmin and return when we encounter a value root->value < vmax. This could be the optimal solution!

- vk August 25, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

If vmin < min inside tree and vmax > max in tree, you have to print them all => O(n)
But, vmax - vmin = constant (does not depend on the input size) => O(log n + ct) = O(log n).
Therefore, I don't think it is possible to print all values greater than vmin and lower than vmax in O(log n).
But if the nodes are consecutive numbers, you can find the start node and the end node in O(log n) and print the inner values in O(vmax - vmin) => O(log n + vmax - vmin).

- S August 25, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

pseudo code:

findrange(bst,min,max){
   if (bst==null) return ;
   if(min<= bst->data <= max) {
         findrange(bst->left,min,max);
         print bst->data;
         findrange(bst->right,min,max);
   }
   elseif (bst->data <min)
         findrange(bst->right,min,max);
   elseif (bst->data <max)
         findrange(bst->left,min,max);
   else
          return;
}

- Rocco August 25, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

inorder(root)
if(root == null) return;
if(root->value > min) inorder(root->left)
if(root->value > min && root->value < max) print(root->value)
if(root->value < max) inorder(root->right)

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

inorder(root)
if(root == null) return;
if(root->value > min) inorder(root->left)
if(root->value > min && root->value < max) print(root->value)
if(root->value < max) inorder(root->right)

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

A short psuedo code

Find the Least common ancestor of (a,b)O(max(depth(a), depth(b))). The subtree rooted at this node is the smallest subtree that has to be explored. Run a dfs on the nodes, and print only those that are in the range.

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

A short psuedo code

Find the Least common ancestor of (a,b)O(max(depth(a), depth(b))). The subtree rooted at this node is the smallest subtree that has to be explored. Run a dfs on the nodes, and print only those that are in the range.

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

if(p != NULL)
{
if(p->data > min)
BSTsearch(p->left,max,min);

if(p->data <=max && p->data >=min)
cout<<" "<<p->data;

if(p->data <max)
BSTsearch(p->right,max,min);


}

- helen May 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{
asdf
}

- Anonymous October 20, 2014 | 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