Microsoft Interview Question for Software Engineer / Developers


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




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

void print_array_spiral(int mat[4][5])
{
   int j, i;
   int min_row, max_row;
   int min_col, max_col;
   
   min_row = 0;
   max_row = 3;
   min_col = 0;
   max_col = 4;

   while (min_col < max_col && min_row < max_row) {
      // Move rightwards and print
      for (i = min_col; i <= max_col; i++)
         printf("%2d ", mat[min_row][i]);
      min_row++;

      // Move downwards and print
      for (i = min_row; i <= max_row; i++)
         printf("%2d ", mat[i][max_col]);
      max_col--;
      
      // Move leftwards and print
      for (i = max_col; i >= min_col; i--)
         printf("%2d ", mat[max_row][i]);
      max_row--;

      // Move upwards and print
      for (i = max_row; i >= min_row; i--)
         printf("%2d ", mat[i][min_col]);
      min_col++;

      printf("\n");
   }
   
   printf("\n"); // one round is over, proceed to the next
}

- ashot madatyan March 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

#include<iostream.h>
#include<conio.h>

int matrix[8][8]=
{
{4,6,3,5,5,5},
{4,2,5,7,6,3},
{5,3,7,5,3,9},
{5,2,8,5,9,2},
{2,6,3,8,1,4},
{5,2,8,3,2,5}
};

//------- i=0 .....1,m-1 2,m-1 3,m 4,m-----------2,m-2 3,m-2
void spiral(int matrix[][8],int n,int m)
{ int i,j=0,x;
if(n%2==0) x=n/2;
else x=n/2+1;
for(i=0;i<x;i++)
{
cout<<"\nround 1-->";
for(j=i;j<m-i;j++)
cout<<matrix[i][j]<<" ";

cout<<"\nround 2-->";
for(j=i+1;j<n-i;j++)
cout<<matrix[j][(m-1)-i]<<" ";

cout<<"\nround 3-->";
for(j=m-i-2;j>=i;j--)
cout<<matrix[n-i-1][j]<<" ";

cout<<"\nround 4-->";
for(j=n-i-2;j>i;j--)
cout<<matrix[j][i]<<" ";


cout<<endl;
}




}//
void printMatrix(int matrix[][8],int n,int m)
{
for(int register i=0;i<n;i++)
{
for(int register j=0;j<m;j++)cout<<matrix[i][j]<<" ";
cout<<endl;}
}

int main()
{ int i=0;
clrscr();

cout<<endl<<"start "<<endl;

for(i=2;i<7;i++)
{
cout<<endl<<"matrix printing start "<<endl;
printMatrix(matrix,i,i); cout<<endl<<"outputt";
spiral(matrix,i,i);
getch();
clrscr();
}

getch();
return 0;
}

- Aniruddh Siddh March 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

Comment from OP

void spiraltraversal( int (*a)[], int r, int c, int startr = 0, int startc = 0 )
{
	
	if( r == 1 && c== 1 )
		print(a[sr][sc])
		return;
	
	for( i = sc; i <= sc+c-1; i++ )
		print(a[sr][i]);
	for( i = sr; i <= sr+r-1; i++ )
		print(a[i][sc+c-1]);
	if( r != 1 )
		for( i = sc+c-1; i >= sc; i-- )
			print(a[sr+r-1][i]);
	if( c != 1 )
		for( i = sr+r-1; i >= sr+1; i-- )
			print(a[i][sc]);
		
	if( r-2 <= 0 || c-2 <= 0 )
		return;
	else	
		spiraltraversal( a, r-2, c-2, startr+1, startc+1 );
	return;

}

- y2km11 March 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

struct Point {
	int x;
	int y;
};

void spOnce(char **a, Point p0, Point p1) {
	if(p0.x==p1.x) {
		if(p0.y<=p1.y) {
			for(int i=p0.y; i<=p1.y; i++) {
				cout<<a[p0.x][i];
			}
		} else {
			for(int i=p0.y; i>=p1.y; i--) {
				cout<<a[p0.x][i];
			}
		}
	} else {
		if(p0.x<=p1.x) {
			for(int i=p0.x; i<=p1.x; i++) {
				cout<<a[i][p0.y];
			}	
		} else {
			for(int i=p0.x; i>=p1.x; i--) {
				cout<<a[i][p0.y];
			}	
		}
	}
}

void spiralTrav(char **a, int m, int n) {
	Point pt[4];
	pt[0].x=0; pt[0].y=0;
	pt[1].x=0; pt[1].y=n-1;
	pt[2].x=m-1; pt[2].y=n-1;
	pt[3].x=m-1; pt[3].y=0;
	int pos=0;
	while (pt[0].x<=pt[3].x) {
		spOnce(a, pt[pos], pt[(pos+1)%4]);
		switch(pos) {
		case 0:
			pt[0].x++; pt[1].x++;
			break;
		case 1:
			pt[1].y--; pt[2].y--;
			break;
		case 2:
			pt[2].x--; pt[3].x--;
			break;
		case 3:
			pt[3].y++; pt[0].y++;
			break;
		default:
			break;
		}
		pos=(pos+1)%4;
	}
}

- Soba March 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

We can use recursion to solve this problem:

basicalgos.blogspot.com/2012/03/spiral-print-of-matrix.html

- Andy March 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int a[4][4] = { {12, 23, 15, 17}, {34, 22, 176, 23}, {345, 62, 11, 19}, {18, 223, 112, 152}};

	int l=0, k=0, N = 4, M = 4;
	bool ltr = true;
	while (l < N && k < M)
	{
		std::cout << a[k][l] << " ";
		if(l == N-1 && ltr)
		{
			ltr = false; ++k;
			std::cout << std::endl;
		}
		else if(l == 0 && !ltr)
		{
			ltr = true; ++k;
			std::cout << std::endl;
		}
		else
		{
			if(ltr)
				++l;
			else
				--l;
		}
	}

- wally.azab March 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

for(i = 0;i < row; i++)
    {
       printf("\n");
      count = 10;
      if(i%2) // odd
      {
           end = 0;
           p = -1;
           j = col - 1;

      }
      else // even
      {
           end = col - 1;
           p = 1;
           j = 0;
      }
      while(count > 0 )
      {
        printf("%d ", twoDarr[i][j]);
        j = j + p;
        count--;
      }
    }

- RN March 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

yoursandmyideas.wordpress.com/2012/04/01/spiraltraversalofmatrix/

Remember that the same question was asked in MS Student Rockstar 2007 contest.

- Sunil Singhal April 01, 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