Microsoft Interview Question for Software Engineer / Developers


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




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

int main()
{
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;
}---------------------------------------

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

just Awesome

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

The matrix being mXn you would need to take care of that while deciding limits. It should be min(ceil(m/2), ceil(n/2))

- Srikant Aggarwal January 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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);

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

int main()
{
int a[m][n]; //Assume data are already present
int i, j,count=1;
for(i=0;i<n;i++)
{
if(!count%2)
{
for(j=0;j<m;j++)
{
printf("%d",arr[i][j];
}
}
else
{
for(j=m-1;j>=0;j++)
{
printf("%d",arr[i][j]);
}
}
count++;
}

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

<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>

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

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;
}

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

"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;
    }
}

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

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;
                }
            }
        }
    }
}

- Srikant Aggarwal January 12, 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