Microsoft Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

Python:
Can be improved though

def split_into_four(a):
    sub_parts = []
    for (left, right) in [(0, len(a) / 2), (len(a) / 2, len(a))]:
        sub_parts.append([v[0:len(v) / 2] for v in a[left:right]])
        sub_parts.append([v[len(v) / 2:len(v)] for v in a[left:right]])
    return sub_parts

- td.abinesh February 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
using namespace std;

void MatrixToArrayFun(int arr[][4],int iCol,int iRow){
if(iCol!=iRow){
cout<<"sorry row and column must be same"<<endl;
return;
}
int lNumber = iRow/2;

int lRound =0;
while(lNumber){

for(int i=lRound;i<lRound+2;i++)
for(int j=lRound;j<lRound+2;j++)
cout<<arr[i][j]<<"..";

cout<<endl;

for(int i=lRound;i<lRound+2;i++)
for(int j=(2-lRound);j<(4-lRound);j++)
cout<<arr[i][j]<<"..";
cout<<endl;

lNumber --;
lRound+=2;
}
}

int main(){
cout<<"hello"<<endl;
int arr[4][4]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
cout<<sizeof(arr)<<endl;
MatrixToArrayFun(arr,4,4);

}

- Raj February 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Pseudocode -

print(Array A, int n){
 int count = 0;
 for(int i=0; i<n-1;i+=2){ // n-1 to prevent Array out of bounds.
  for(int j=0; j<n-1;j+=2){
   OutputArr[count] = {A[i][j], A[i][j+1], A[i+1][j], A[i+1][j+1]};
   count++;
  }
 }
}

- Biru February 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Generic code when no of independent arrays = (ROW*COL/row*col) is an integer.

#include<stdio.h>
#include<stdlib.h>

void matrixToArray(int arr[][6],int ROW,int COL,int row,int col)
{
if((ROW*COL)%(row*col)!=0)
{
printf("No splitting possible\n");
return;
}

int noArrays=ROW/row;
int rowBound=0;
int colBound=0;
while(noArrays--)
{
int i=0,j=0;
for(i=rowBound;i<rowBound+row;i++)
for(j=colBound;j<colBound+col;j++)
printf("%2d ",arr[i][j]);

printf("\n");

for(i=rowBound;i<rowBound+row;i++)
for(j=(col-colBound);j<(COL-colBound);j++)
printf("%2d ",arr[i][j]);

printf("\n");

rowBound+=row;
}
}

int main()
{
int A[4][6]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24};
matrixToArray(A,4,6,2,3);
getchar();
return 0;
}

- gautam714 February 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#define R 4
#define C 4
void CreateArr(int arr[R][C],int start1,int end1,int start2,int end2,int output[])
{
int local=0;
for(int i=start1;i<end1;i++)
for(int j=start2;j<end2;j++)
output[local++]=arr[i][j];
}
int main()
{
int arr[R][C]={{1,2,3,4},
{5,6,7,8},
{9,10,11,12},
{13,14,15,16}};
int size=2;
int *out=(int *)malloc(sizeof(int)*size*size);
for(int i=0;(i+size)<=R;i=i+size)
{
for(int j=0;(j+size)<=C;j=j+size)
{
memset(out,0,sizeof(int)*size*size);
CreateArr(arr,i,i+size,j,j+size,out);
for(int k=0;k<(size*size);k++)
printf("%d ",out[k]);
printf("\n");
}
}
getchar();
return 0;
};

- Anonymous February 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

all this algos have o(n^2) complexity. Is there any algorithm with reduced complexity?

- Ashley February 03, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can anyone please provide the code in matlab...though for loops in matlab have an advantage..i cant figure out how to create the arrays...please help me..

- Anonymous February 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Test {
public static void main(String args[]) {
int arr[][] = new int[][]{{1,2,3,4,5,6},{7,8,9,10,11,12},{13,14,15,16,17,18},{19,20,21,22,23,24},{25,26,27,28,29,30},{31,32,33,34,35,36}};
int resetpos = 0, matrixsize = 6, submatrix = 2;
boolean terminate = false;


for (int i=0;i<=matrixsize && !terminate;) {
for (int j=0;j<=matrixsize && !terminate;j++)
{
if (j!=0 && j%submatrix == 0) {
i++;
j=resetpos;
}

if (i!=0 && i%matrixsize == 0) {
i=0;
resetpos += submatrix;
j=resetpos;
}

System.out.println(arr[i][j]);

if (i+1 == matrixsize && j+1 == matrixsize)
terminate = true;

}
}

}
}

- Joy Java Coder February 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

int N=8; // N*N matrix

int main() {
    int i,j;
    int b[4][(N*N)/4];
    int c[2*N][4];
    int a[N][N];
    int dev=N/2;
    srand(time(NULL));

//creating and printing input
    for(i=0;i<N;i++)
	{
	    for(j=0;j<N;j++)
	    {
	        a[i][j]=rand()%100;
	        printf("%2d ",a[i][j]);
	    }
	    printf("\n");
	}

/* actaul copying done here */
	for(i=0;i<N;i++)
	{
	    for(j=0;j<N;j++)
	    {
	        b[((i/dev)*2)+(j/dev)][(j%dev) + dev*(i%dev)]=a[i][j];
	        c[((i/2)*dev)+j/2][(i%2)*2 + j%2]=a[i][j];
	    }
	}

//printing output1
	for(i=0;i<4;i++)
	{
	    for(j=0;j<(N*N)/4;j++)
	    {
	        printf("%2d ",b[i][j]);
	    }
	    printf("\n");
	}

//printing out other output
    for(i=0;i<2*N;i++)
	{
	    for(j=0;j<4;j++)
	    {
	        printf("%2d ",c[i][j]);
	    }
	    printf("\n");
	}
	return 0;
}

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

{{
const int SIZE=4;
int matrix[SIZE][SIZE];
void print(int , int , int ,int);
int main()
{
int size;
printf("Entering %d*%d matrix using rand() \n",SIZE,SIZE);
for(int i=0;i<SIZE;i++)
{
for(int j=0;j<SIZE;j++)
{
matrix[i][j]=rand();
}
}

printf("Enter the sub matrix size you want to print:");
scanf_s("%d",&size);

if(SIZE%size==0)
{
print(0,0,size,SIZE);
}
return 0;
}

void print(int i, int j, int size, int SIZE)
{
int row=i, column=j;
for(;i<size+row;i++)
{
for(j=column;j<size+column;j++)
{
printf("%-7d",matrix[i][j]);
}
}
printf("\n\n");

if(j<SIZE)
{
i-=size;
print(i,j,size,SIZE);
}
else
{
if(i<SIZE)
{
j=0;
print(i,j,size,SIZE);
}
}
}
}}

- Anonymous February 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/*1 2 3 4
5 6 7 8
3 5 0 8
9 2 1 5*/
/*the output must be
1 2 5 6
3 4 7 8
3 5 9 2
0 8 1 5*/
#include<stdio.h>
int a[10][10];
int main()
{
int m,i,j;

printf("enter the value of m:\n");
scanf("%d",&m);
printf("enter the matrix elements:\n");
for(i=0;i<m;i++)
for(j=0;j<m;j++)
scanf("%d",&a[i][j]);
print(m);
return 0;
}
int print(int m)
{
int aa[(m*m)/2][4],k=0,i=0,j=0;
i=0;j=0;
while(i<m&&j<m)
{
aa[k][0]=a[i][j];
aa[k][1]=a[i][j+1];
aa[k][2]=a[i+1][j];
aa[k][3]=a[i+1][j+1];
k++;
if(j+2<m-1)
j+=2;
else{
j=0;i+=2;}
}
for(i=0;i<k;i++){
printf("\nthe elements of array%d is:",i+1);
for(j=0;j<4;j++)
printf("\n%d:",aa[i][j]);}
return 0;
}

- Ananymous February 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.ArrayList;

public class SubMatrix {
	int[][] matrix;
	int M;

	public SubMatrix(int m) {
		super();
		M = m;
		matrix = new int[M][M];
		for (int i = 0; i < M; i++) {
			for (int j = 0; j < M; j++) {
				matrix[i][j] = (int) (Math.random() * 10 + 1);
			}
		}
	}

	public void subMatrix(int subNum) {
		if (subNum <= 0 || M % subNum != 0) {
			System.out.println("cannot extract individal blocks");
			return;
		}
		int blockSize = M / subNum;
		ArrayList<Integer>[] lists = new ArrayList[blockSize * blockSize];
		for (int i = 0; i < blockSize * blockSize; i++) {
			lists[i] = new ArrayList<Integer>();
		}

		for (int index = 0; index < blockSize * blockSize; index++) {
			for (int k = 0; k < subNum * subNum; k++) {
				int row = index / blockSize * subNum + k / subNum;
				int column = index % blockSize * subNum + k % subNum;
				lists[index].add(k, matrix[row][column]);
			}
		}

		for (int index = 0; index < blockSize * blockSize; index++) {
			System.out.println(lists[index]);
		}
	}

	public void print() {
		for (int i = 0; i < M; i++) {
			for (int j = 0; j < M; j++) {
				System.out.print(matrix[i][j] + "\t");
			}
			System.out.println();
		}
	}

	public static void main(String[] args) {
		SubMatrix sm = new SubMatrix(6);
		sm.print();
		sm.subMatrix(3);

	}

}

- Kevin February 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I supposed R and C be the number rows and columns respectively of the Array from where data is to be extracted. r and c be the number rows and columns respectively of the new array;

if(R*C%(r*c)!==0)
{
	printf("Extraction Not Possible");
	return;
}
int arr[R*C/(r*c)][r][c];

for(i=0;i<R*C;i++)
{
	arr[i/(r*c)][i%r][i%c]=a[i/R][i%C];
}

- Kaushal Kumar Singh June 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Or We can do this

if(R*C%(r*c)!=0)
{
	printf("Extraction Not Possible");
	return;
}

for(i=0;i<R*C;i++)
{
	if(i%(r*c)==0)
		printf("\n");

	printf("%d",a[i/R][i%C]);
}

- Kaushal Kumar Singh June 17, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
#include <stdlib.h>

int main()
{
int arry[4][4] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
int i,j,m,n;
int count = 1;

for(i=0;i<3;i+=2)
{
for(j=0;j<3;j+=2)
{
printf("Array %d = ",count);
for(m=i;m<i+2;m++)
{
for(n=j;n<j+2;n++)
{
printf("%d\t",arry[m][n]);
}
}
count++;
printf("\n\n");
}
}
}

- Prashanth August 29, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void exactindividualblock()
{
int a[][4]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
int size=4;
for(int i=0;i<size;i=i+2)
for(int j=0;j<size;j=j+2)
{
for(int m=i;m<i+size/2;m++)
{
for(int k=j;k<j+size/2;k++) printf("%d-",a[m][k]);

}
}
}

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

//code to extract individual blocks from a given matrix
#include<stdio.h>
#include<conio.h>
#define MAX 4
int main()
{
int a[MAX][MAX],i,j;
printf("enter array elements:");
for(i=0;i<MAX;i++)
{
for(j=0;j<MAX;j++)
scanf("%d",&a[i][j]);
}
for(i=0;i<MAX/2;i++)
{
for(j=0;j<MAX/2;j++)
printf("%d\t",a[i][j]);
//printf("\n\n");
}
printf("\n\n");
for(i=0;i<MAX/2;i++)
{
for(j=MAX/2;j<MAX;j++)
printf("%d\t",a[i][j]);
//printf("\n\n");

}
printf("\n\n");
for(i=MAX/2;i<MAX;i++)
{
for(j=0;j<MAX/2;j++)
printf("%d\t",a[i][j]);
//printf("\n\n");
}
printf("\n\n");
for(i=MAX/2;i<MAX;i++)
{
for(j=MAX/2;j<MAX;j++)
printf("%d\t",a[i][j]);
// printf("\n\n");
}
getch();
}

- apoorva September 19, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<conio.h>
#define MAX 6

int main()
{
int a[MAX][MAX],i,j;
printf("enter array elements:");
for(i=0;i<MAX;i++)
{
for(j=0;j<MAX;j++)
scanf("%d",&a[i][j]);
}
for(i=0;i<MAX/2;i++)
{
for(j=0;j<MAX/2;j++)
printf("%d\t",a[i][j]);
//printf("\n\n");
}
printf("\n\n");
for(i=0;i<MAX/2;i++)
{
for(j=MAX/2;j<MAX;j++)
printf("%d\t",a[i][j]);
//printf("\n\n");

}
printf("\n\n");
for(i=MAX/2;i<MAX;i++)
{
for(j=0;j<MAX/2;j++)
printf("%d\t",a[i][j]);
//printf("\n\n");
}
printf("\n\n");
for(i=MAX/2;i<MAX;i++)
{
for(j=MAX/2;j<MAX;j++)
printf("%d\t",a[i][j]);
// printf("\n\n");
}
getch();
}

- apoorva September 19, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//code to extract individual blocks from a given matrix
#include<stdio.h>
#include<conio.h>
#define MAX 6
int main()
{
	int a[MAX][MAX],i,j;
	printf("enter array elements:");
	for(i=0;i<MAX;i++)
	{
		for(j=0;j<MAX;j++)
			scanf("%d",&a[i][j]);
	}
	for(i=0;i<MAX/2;i++)
	{
		for(j=0;j<MAX/2;j++)
		printf("%d\t",a[i][j]);
	//printf("\n\n");
	}
	printf("\n\n");
	for(i=0;i<MAX/2;i++)
	{
		for(j=MAX/2;j<MAX;j++)
		printf("%d\t",a[i][j]);
	//printf("\n\n");
	
	}
	printf("\n\n");
	for(i=MAX/2;i<MAX;i++)
	{
		for(j=0;j<MAX/2;j++)
		printf("%d\t",a[i][j]);
	//printf("\n\n");
	}
	printf("\n\n");
	for(i=MAX/2;i<MAX;i++)
	{
		for(j=MAX/2;j<MAX;j++)
		printf("%d\t",a[i][j]);
//	printf("\n\n");
	}
	getch();
}

- apoorva September 19, 2015 | 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