Microsoft Interview Question for Software Engineer / Developers


Team: Bing
Country: India
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
7
of 9 vote

here is the correct code , i just forget to include the print statemnt

printNodes(Node root,int m,int n)
{
    if(root==null)
     return;
    if(root.data>=m&&root.data<=n)
    print(root.data);
    if(m<root.data)
    printNode(root.left,m,n);
    if(n>root.data)
    printNode(root.right,m,n);
}

- getjar.com/todotasklist my android app December 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

What is the complexity ? O(total # of nodes in the tree) .. ?

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

How are u taking care of case when root.data>m and root.data<n?

- anony December 07, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

see the code carefully , it is easy to understand.

- getjar.com/todotasklist my android app December 07, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

However this isn't an ordered result.

- Rio December 09, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Isnt the complexity O(log n)

- pachunuri January 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

In best case its O(logn). In worst case it is O(n).

I can say its complexity is O(h) where h is height of Btree

- Anonymous March 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

How about this? Its short and sweet :P

public static void printRange( Node node, int r1, int r2 ) {
     if(node == null) return;
     if(node.data > r2)
       printRange(node.left, r1, r2);
     if(node.data < r1)
       printRange(node.right, r1, r2);
 
     if(r1 <= node.data && node.data <= r2) {
       System.out.println("Node data: " + node.data);
       printRange(node.right, r1, r2);
       printRange(node.left, r1, r2);
     }
   }

- mihirk February 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Good. If we also change an order of checks in the algo as following, the algo outputs data in increasing order:

if(node.data > r2)
       printRange(node.left, r1, r2);

     if(r1 <= node.data && node.data <= r2) {
       System.out.println("Node data: " + node.data);
       printRange(node.left, r1, r2);
       printRange(node.right, r1, r2);
      }
     if(node.data < r1)
       printRange(node.right, r1, r2);

- Aleksey.M November 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

Algo:
1. First find the min
2. Go for inorder
3. Print the nodes between n(smaller) and m(larger)

- Atul December 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Why inorder? Why not preorder or postorder. It takes O(n) time in all cases as the best and worst case both involve going through each element. Instead follow techcoderonwork's algo . the best case would take O(log n) if i am not wrong with the analysis

- pachunuri January 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi raiprince001. How did you get the interview with MS bing? Employee Referral? Consultant? or somthing else ? pls reply.

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

we can do it in O(k + log(z)) time , where k = number of printed nodes (between n and m) and z = total number of nodes in the BST.
say n<m
Like in order
if currval of the node <= n go right
if(currval of the node is in between n and m print)
if currval of the node > m go left

- Doctor.Sai December 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I went through the Employee Referral ..!!

- raiprince001 December 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thanks for the reply .. :)

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

Did you go in as a experienced candidate or as a fresh grad (entry level). If you went as a exp person. can you please tell me how much exp you had?

- Ash December 09, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

No I have just an experience of 6 months .!!

- raiprince001 December 09, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

if you can also tell which college did you go to in your engineering that might help :)

- Ashu December 13, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

perhaps if you can tell which college you went to in your engineering , that might help

- ashu December 13, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about this ?

bool ReturnBtw(curr, m, n) {
if(!curr)
return false;

else // find m
{
while(curr->val == m){
if(curr-val > m)
curr = curr->left;
else
curr = curr->right;
}
//found m, stored in m
int flag = true;
Print(curr, flag, n);
}

Print(curr, flag, n){

if(!curr || curr->val == n)
return;

else{
if(flag) {
cout<<curr->right->val;
Print(curr->right, false, n);
}
else{
Print(curr->left, false,n);
cout<<curr->val;
Print (curr->right, false, n);
}
}
}

Idea is, if found m, we need to traverse the immediate right subtree, and after that do the normal inporder traversal, until u reach destination node.

complexity: O(no. of nodes btw m & n)

- P December 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

TO call:
ReturnBtw(root, sourceNode, destNode);

- P December 06, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This code may not work. reason i see is,
the code prints only the right subtree of the node m.
The usecase it fails is, if 'm' is in the left subtree of the head and 'n' is somewhere in the right subtree of the head.

- Somisetty December 08, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

So, Intent is to print left most node in the right subtree, that is, the in-order succesor of node m. Use case , it will fail is, if m doesn't have a right subtree. So, we should rather find the in-order succesor of the m, and start from thr.

- P December 08, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

here is the code

printNodes(Node root,int m,int n)
{
    if(root==null)
     return;
    if(n<root.data)
    printNode(root.left,m,n);
    if(m>root.data)
    printNode(root.right,m,n);
}

- getjar.com/todotasklist my android app December 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

where is the print?

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

Will it be if(n<root.data) or if(root.data>n)

- techgeek December 11, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

@techcoderonwork

Will this really work.....
Check this out for this example...
18
/ \
15 20
/ \ / \
13 16 19 24
The value of m and n are 16 and 30...I think it wont satisfy any if statement

- coding December 11, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
#include"BinaryTree.h"

using namespace std;

#define NELEMS(x) (sizeof(arr)/sizeof(arr[0]))

void findNodesInRange(btree *p, int m, int n)
{
        if(n < m)
                return;

        if(p == NULL)
                return;

        if(p->data > n)
                findNodesInRange(p->left, m, n);
        else if(p->data < m)
                findNodesInRange(p->right, m ,n);
        else
        {
                cout << p->data << endl;
                findNodesInRange(p->left, m, p->data);
                findNodesInRange(p->right, p->data, n);
        }
}

int main(int argc, char *argv[])
{
        btree *root = NULL;
        int arr[] = {12,7,4,2,6,10,8,11,9,20,16,14,19,18,24,22,26};

        if(argc != 3)
        {
                cout << "invalid command line usage" << endl;
                return 1;
        }

        for(int i = 0; i < NELEMS(arr); i++)
        {
                root = BinaryTree_insert(root, arr[i]);
        }

        findNodesInRange(root, atoi(argv[1]), atoi(argv[2]));
        return 0;
}

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

1) Find LCA { Easy to do in O(lg n ) if you have a visited bit with each node in the tree }
2) from LCA print all nodes until we reach m.
3) Print right subtree of m.
4) From LCA print all nodes, until we reach n.
5) Print Left subtree of n.

- subramanian.ganapathy86 January 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<conio.h>
#include<stdio.h>
#include<malloc.h>
struct node
{
int data;
struct node*left;
struct node*right;
};


void insert(struct node**q,int num)
{
struct node *temp=NULL;
temp=(struct node*)malloc(sizeof(struct node));
if(*q==NULL)
{
temp->data=num;
temp->left=NULL;
temp->right=NULL;
*q=temp;
// printf("%d\n",temp->data);
}
else
{
if(((*q)->data)>num)
insert(&((*q)->left),num);
else
insert(&((*q)->right),num);
}
}

void range(struct node *p,int m,int n)
{
if(p==NULL)
return;
else
{
if(m>p->data)
range(p->right,m,n);
else
{
if(n<p->data)
range(p->left,m,n);
else
{
printf("%d\n",p->data);
range(p->left,m,p->data-1);
range(p->right,p->data+1,n);
}
}}
}
int main()
{
struct node*p;
p=NULL;

insert(&p,17);
insert(&p,13);
insert(&p,21);
insert(&p,27);
insert(&p,19);
insert(&p,8);
insert(&p,15);
range(p,10,20);
getch();
return 0;
}

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

fun(node*n,int m int n))
{ if(n)
{ if(n->data>=m&& n->data<=n)
{ cout<<n->data;
if(n->left)
fun(n->left);
if(n->rite)
fun(n->rite);
}

if(n->data>m)
fun(n->left);
if(n->data<m)
fun(n->rite);
}
}

- Anonymous October 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

oops forgot to login.pasting code again.

fun(node*n,int m int n))
{ if(n)
{ if(n->data>=m&& n->data<=n)
{ cout<<n->data;
if(n->left)
fun(n->left);
if(n->rite)
fun(n->rite);
}

if(n->data>m)
fun(n->left);
if(n->data<m)
fun(n->rite);
}
}

- aaman.singh85 October 14, 2012 | 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