Microsoft Interview Question
Software Engineer / DevelopersTeam: Bing
Country: India
Interview Type: In-Person
Now define a function
bool Distance(Node currNode,
Node lookupNode,
int distance,
int lookupDistance,
List<Node> results)
{
if (currNode == null)
{
return false;
}
if (currNode == lookupNode)
{
if (lookupDistance == 0)
{
results.Add(currNode);
}
else
{
Distance(currNode.Left, lookupNode, 1, lookupDistance, results);
Distance(currNode.Right, lookupNode, 1, lookupDistance, results);
}
return true;
}
if (distance > lookupDistance)
{
if (!Distance(currNode.Left, distance - 1, lookupDistance, results))
{
Distance(currNode.Right, distance - 1, lookupDistance, results);
}
}
if (distance < lookupDistance)
{
Distance(currNode.Left, lookupNode, distance + 1, lookupDistance, results);
Distance(currNode.Right, lookupNode, distance + 1, lookupDistance, results);
}
if (distance == lookupDistance)
{
results.Add(currNode);
}
return false;
}
Now determine the distance of the node from root (d) and call this method on root:
Distance(root, lookupNode, d, k, results);
<pre lang="" line="1" title="CodeMonkey73295" class="run-this">#include "stdio.h"
int a[5][4] =
{
{1 ,2 ,3 ,4 },
{14,15,16,5 },
{13,20,17,6 },
{12,19,18,7 },
{11,10,9 ,8 }
};
int k;
void print(int x)
{
printf("%d ",x);
k++;
}
int main()
{
int rows=5;
int cols=4;
int i=0,is=0,ie=0;
int j=0,js=0,je=0;
k=0;
ie=rows-1;
je=cols-1;
while(k<rows*cols)
{
for(j=is;j<=je;j++)
{
print(a[i][j]);
}
/*revert extra*/
j--;
/*limit*/
is++;
for(i=is;i<=ie;i++)
{
print(a[i][j]);
}
/*revert extra*/
i--;
/*limit*/
je--;
for(j=je;j>=js;j--)
{
print(a[i][j]);
}
/*revert extra*/
j++;
/*limit*/
ie--;
for(i=ie;i>=is;i--)
{
print(a[i][j]);
}
/*revert extra*/
i++;
/*limit*/
js++;
}
return 0;
}
</pre><pre title="CodeMonkey73295" input="yes">
</pre>
Edit distance code
#include <iostream>
using namespace std;
typedef struct {
int cost;
int parent;
}Cell;
enum {
UNKNOWN = 0,
MATCH,
SUBSTITUTION,
INSERT,
DELETE
};
int EditDistance(char s[], int m, char t[], int n)
{
Cell **matrix;
int ed = 0;
matrix = new Cell*[n+1];
for(int i = 0; i <= n; i++)
matrix[i] = new Cell[m+1];
for(int i = 0; i <= m; i++)
{
matrix[0][i].cost = i;
matrix[0][i].parent = UNKNOWN;
}
for(int j = 0; j <= n; j++)
{
matrix[j][0].cost = j;
matrix[j][0].parent = UNKNOWN;
}
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
if(t[i-1] == s[j-1])
{
matrix[i][j].cost = matrix[i-1][j-1].cost;
matrix[i][j].cost = MATCH;
}
else
{
int minCost;
int parent;
if( (matrix[i-1][j-1].cost+1) < (matrix[i][j-1].cost+1))
{
minCost = matrix[i-1][j-1].cost+1;
parent = SUBSTITUTION;
}
else
{
minCost = matrix[i][j-1].cost+1;
parent = DELETE;
}
if( minCost > (matrix[i-1][j].cost+1))
{
minCost = matrix[i-1][j].cost+1;
parent = INSERT;
}
matrix[i][j].cost = minCost;
matrix[i][j].parent = parent;
}
}
}
ed = matrix[n][m].cost;
for(int i = 0; i <= n; i++)
delete(matrix[i]);
delete(matrix);
return ed;
}
"K Nearest Neighbours"
class Tree {
private class Node {
int key;
int x;
int y;
Node left;
Node right;
int distance(Node node) {
return Math.abs(x - node.x) + Math.abs(y - node.y);
}
}
private Node head = null;
private void setCoordinates(Node node, int x, int y) {
if (node == null) {
return;
}
node.x = x;
node.y = y;
setCoordinates(node.left, x - 1, y - 1);
setCoordinates(node.left, x + 1, y - 1);
}
private void findKNearestNeighbours(Node pivot, int k, Node node, List<Node> list) {
if (node == null) {
return;
}
if (node.distance(pivot) == k) {
list.add(node);
}
findKNearestNeighbours(pivot, k, node.left, list);
findKNearestNeighbours(pivot, k, node.right, list);
}
public List<Node> findKNearestNeighbours(Node pivot, int k) {
setCoordinates(root, 0, 0);
List<Node> list = new LinkedList<Node>();
findKNearestNeighbours(pivot, k, root, list);
return list;
}
}
K Distance Nodes:
void mod_inorder_traversal(node *root, int n, int dist)
{
static int dist_start = -1;
if(root == NULL)
return;
else
{
if(dist_start != -1)
{
if((dist - dist_start) == k)
{
cout << root->data << endl;
return;
}
}
if(root->data == n)
dist_start = dist;
dist += 1;
mod_inorder_traversal(root->left, n, dist);
if(dist > dist_start)
{
mod_inorder_traversal(root->right, n, dist);
}
else
{
if(dist_start == -1)
mod_inorder_traversal(root->right, n, dist);
if(dist_start != -1)
{
if(((dist - 1) - dist_start) == k)
{
cout << root->data << endl;
return;
}
}
}
}
}
int main()
- vj December 08, 2011{
int arr[][4] = { {1,2,3,4},
{5,6,7,8},
{9,10,11,12},
{13, 14, 15, 16}
};
int i, j, k,middle,size;
printf("\n\n");
size = 4;
for(i=size-1, j=0; i>0; i--, j++)
{
for(k=j; k<i; k++) printf("%d ", arr[j][k]);
for(k=j; k<i; k++) printf("%d ", arr[k][i]);
for(k=i; k>j; k--) printf("%d ", arr[i][k]);
for(k=i; k>j; k--) printf("%d ", arr[k][j]);
}
middle = (size-1)/2;
if (size % 2 == 1) printf("%d", arr[middle][middle]);
printf("\n\n");
return 1;
}---------------------------------------