Microsoft Interview Question
Software Engineer / DevelopersTeam: Bing
Country: India
Interview Type: In-Person
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);
}
}
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);
Algo:
1. First find the min
2. Go for inorder
3. Print the nodes between n(smaller) and m(larger)
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?
if you can also tell which college did you go to in your engineering that might help :)
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)
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.
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);
}
#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;
}
#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;
}
here is the correct code , i just forget to include the print statemnt
- getjar.com/todotasklist my android app December 07, 2011