Microsoft Interview Question for Software Engineer / Developers






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

int recursive(int x, int y){
if(x == 1 || y == 1) return 1;
return recursive(x-1, y) + recursive(x, y-1);
}

as for NxN, calculate recursive(N, N)

- guest April 14, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Ok, this question is extremely easy if you look at it in the right light... For an NxN square it takes (2N - 2) steps to move from the upper left corner to bottom right corner. Of these steps, half must be right and half must be down (N - 1 in each direction). Knowing this, we can look at it as a string permutation or how many ways can you write "RRRDDD", etc. (this would be for N = 4, "RRDD" for N = 3 and so on). Now we just have to count the number of permutations for this string which in terms of N will be (2N - 2)!/((N - 1)!(N - 1)!). And there you have it...these numbers match up with Godot's post of:
2, 2
3, 6
4, 20
5, 70
6, 252
7, 924
8, 3432

- Nathan May 05, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Right, Nathan's analysis is right.

- Anonymous May 05, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

yup!

- T November 26, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The first technique that comes to my mind is to employ a depth first search until the entire tree is exlored, and count how many times the goal (ie. the right-bottom cell) is reached.

- graphicalguy April 10, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I guess that's the solution...

- gauravk.18 April 10, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

pin

- sidd April 11, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

N * N+1

- guest April 13, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is a dynamic programming problem.
Use an NxN array A where A[i,j] is the number of paths into cell i,j.
Initialize
A[0,0] = 1
and iterate through the array calculating
A[i,j] = A[i-1,j] + A[i,j-1]
The number of paths into a cell is the sum of the number of paths into the cells to the left and up.

- EG April 14, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is right to compute the number of path

- Anonymous February 12, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

It takes 2N-1 steps to reach the destination. At each step, you have two choices, except the last step where you have only one choice to make. So, it will be 2*2*..(2n-2 times)*1 = 2^(2n-2)

But for the right most column and bottom row, there is only one choice. how to subtract it from the above result ?

- KK April 15, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Guys, this is a catalan number, mathematically there are 2n!/(n!(n+1)!) paths.
Search Catalan_numbers in Wikipedia.

- Nandish April 16, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

its not applicable. read the wiki article fully. the soln is applicable for the problem which does not cross the diagonal. its not the case here

- Anonymous April 17, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

No dynamic programming , no catalan. Its a simple counting problem. Answer is C( 2(n-1), n-1 )
Explanation: If we assume the initial location is [1,1], the robot has to move n-1 steps to right and n-1 down. This problem can be seen as making 2 groups of size n-1 out of 2n-2.

- Mohit Garg April 20, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Mohit ur solution doesnot work, check it properly.... say for n=2, ur ans is 2 but the actual answer is 4,,,just check it out...

- JH April 21, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

hi jh how is it 4 for n = 2?

- hj April 21, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

C'mon, guys the ans is C(2n,n) , check it out! see the following explanation.
Lets encode the movements as below
down = 0
right = 1
now the total path lenght is 2n, in this 2n steps the robot has to take n steps down and n steps right. now our job is to to a selection of n steps form 2n..
or in our encoding we can say that we want to put n no of 1s in n no no 0s(or put n 0s in n 1s, both are same as the matrix is nxn, otherwise it wud have been different) so just select n out of 2n and thats the answer. hope it makse sense...

- desiNerd April 23, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

it needs (n-1)Down and (n-1)Right steps..

so, d ans is C(2n-2,n-1)..
try it with n=3 or so..

- nupur April 26, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

call it with

count(N, 1, &c);

void count(int m, int n, int *c)
{
  printf("m = %d , n = %d \n" , m, n );
  if ( m == 1 && n == N )
    (*c)++;
  else
  {
    if ( m > 1)
      count(m-1,  n, c);
    if ( n < N )
      count(m,  n+1, c);
  }
}

- DareDevil April 28, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

should be C(2n-2,n-1)


int N;

int count (int x, int y)
{
if ((x==N-1)&&(y==N-1))
return 1;
else if (x==N-1)
return count(x,y+1);
else if (y==N-1)
return count(x+1,y);
else
return (count(x+1,y)+count(x,y+1));
}

initially call count(1,1)

- PP June 12, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

it can be recursively generated.
Assume the path is expressed as "RRDD", meaning that path is R->R->D->D.
The following code works.

void genePath(int i, int j, int index, char arr[], int n) {
if (index == 2*n) {
int i =0;
for (i =0; i< 2*n; i++)
printf("%c,",arr[i]);
printf("\n");
}

if (i < n) {
arr[index] = 'R';
genePath(i+1,j,index+1,arr,n);
}

if (j < n) {
arr[index] = 'D';
genePath(i,j+1,index+1,arr,n);
}
}

int main(void) {
int n = 3;
char* arr = (char*) malloc(n*(sizeof(char)));
genePath(0,0,0,arr,n);
return 0;
}

- lensbo June 17, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

arr is out of allocated memory

- pg June 30, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

a minor correction:
if((x==(n-1))&&(y==(n-1)) return 0;
if((x==(n-1))||(y==(n-1)) return 1;
return (count(x+1,y)+count(x,y+1));

- @PP September 24, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Dont be a cunt

- Stranger October 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

only nathan is correct. it's surprising how stupid people are... (DFS? WTF?)

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

Totally (N-1) steps down and (N-1) steps right. Use 1 to represent up and 0 to represent down. We need to find all the strings having 2(N-1) characters with (N-1) '1' and (N-1) '0'. A recursive solution is as following:

#include <stdio.h>
#include <malloc.h>
#define N 3

void Assign( char *start, char *current, int One, int Zero){
if(!One && !Zero){
printf("%s\n", start);
return;
}
if(One){
*current='1';
Assign(start, current+1, One-1, Zero);
}
if(Zero){
*current='0';
Assign(start, current+1, One, Zero-1);
}
}

void main(){
char *path=(char*)malloc((2*(N-1)+1)*sizeof(char));
path[2*(N-1)]='\0';
Assign(path, path, N-1, N-1);
}

- Siwen November 19, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is my code:

#define N 4

void Walk(char *out,int *used,int i, int recurlen)
{
if(recurlen==(2*N-2))
{
int k=0;
while(k<recurlen)
{
printf("%c",out[k]);
k++;
}
printf("\n");
return;
}

if(used[i]==1)
{
out[recurlen]='D';
used[i]=0;
Walk(out,used,i+N,recurlen+1);
used[i]=1;
}

if(used[i]==2)
{
out[recurlen]='R';
used[i]=0;
Walk(out,used,i+1,recurlen+1);
used[i]=2;
}

if(used[i]==3)
{
out[recurlen]='R';
used[i]=1;
Walk(out,used,i+1,recurlen+1);
used[i]=3;

out[recurlen]='D';
used[i]=2;
Walk(out,used,i+N,recurlen+1);
used[i]=3;
}

}

int main()
{

char * out = (char *)malloc((2*N-2)*sizeof(char));
int used[] = {3,3,3,1,3,3,3,1,3,3,3,1,2,2,2,0};

Walk(out,used,0,0);

return 0;
}

- DJ January 24, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I am treating the NxN board as a single dimension array and have modified the indexing appropriately.

A few comments. The used array basically gives me the moves that I can make from that square i.e. R, D or both R and D.

I know I could have named the array better, permissions might have been a better name but whatever :).

if(used[i]) = 1 it means you can move Down (D) from there, if it is 2 you can move Right (R) from there. If used[i]=3, you can move both right and down from there.

Also, I have assumed that at every square you can make some move. If a square is completely off limits the code will still work with minor modifications.

- DJ January 24, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is a typical dynamic programming question.

- Jeff February 19, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

for(i=0;i<N;i++){
for(j=0;j<N;j++){
if(i==0 || j==0)
a[i][j]=1;
else
a[i][j]=a[i-1][j]+a[i][j-1];
}
}
The number of paths to any square is the sum of the no. of paths to the above square and no. of paths to the left square.

This code can find the number of paths to each square in the board.

- Balaji March 01, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

yea dynamic

- pseudo April 03, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

To calculate C(2N,N) or (maybe off by 1 C(2N-2, N-1)), if you say dynamic programming, then fine.


--
Half knowledge is dangerous. Most posters visiting this site are armed!

- T April 03, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

I think DFS may not work. It will ignore some paths which have back-edges. The total number of paths is C(2N, N).
length of every path = 2N
Given a set (1,2,3,...,2N), which represents the path (2N steps). we compute all the possible subsets that have exactly N elements and consider the numbers in the subset as the steps that go down.

- wei April 11, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes I think it is the solution.

- crazystone April 11, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yeh C(2N,N) is correct answer.
Another way to look at problem is you are given N white balls(step right) and N black balls(step down) in how many ways you can arrange them in one raw(of 2N size). The answer of this problem is 2N!/(N!*N!) which is C(2N,N).

- teki July 26, 2008 | Flag
Comment hidden because of low score. Click to expand.
-1
of 0 vote

i think mathematically we can say that there will be (n+1)! paths.
please check my proof.
proof:
since to reach the destination.....in any case the robot will take have to take n steps in the vertical direction to reach ..
i represent those n steps as a1,a2,a3,...
a1 a2 a3 a4 a5 a6 a7 ........an(these are all vertical steps taken at some point of time)
between these steps there can be many horizontal steps(have be to n to reach destination)
since there are n+1 spaces between thses steps....so we choose n spaces out of these n+1 and permute them...
n+1Cn * n!
Any comments?

- tom April 12, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Couple of things with your solution

a) its n-1 steps and the spaces are n-1 too.
b) order matters so taking the combination won't work

As for a sample set of solutions, I coded this up
{size of grid, num ways )
2, 2
3, 6
4, 20
5, 70
6, 252
7, 924
8, 3432

Way its growing suggests a factorial, but your solution doesn't hold water imo.

FWIW, the data structure I am reminded of is a threaded binary tree

- Godot April 13, 2008 | Flag


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