Microsoft Interview Question for Software Engineer / Developers






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

This is a typical combinatorial search algorithm. Find all possible subsets and only print out subset with length k.

- vodangkhoa November 13, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Khoa can you please give us the algorithm?

- Java Coder November 23, 2007 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

#include <iostream>

using namespace std;

void combination(int combindex);

const int arysize = 5;
const int outsize = 3;

int ary[] = {1,2,3,4,5};
bool used[arysize];
int comb[outsize];


int main(void){
	
	for(int i = 0;i<arysize;i++){
		used[i] = false;
	}
	
	combination(0);
}

void combination(int combindex){

	for(int i=0;i<arysize;i++){
		if(used[i] == false){
			comb[combindex] = ary[i];
			used[i] = true;

		}
		else
			continue;
		if(combindex == outsize-1){
				for(int j = 0;j<outsize;j++){
					cout <<comb[j]<<",";
				}
				cout << endl;
		}
		else{			
			combination(combindex+1);
		}
		used[i] = false;
	}
}

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

many duplicate subset

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

Can the following be a solution?

if the array is 1234 and say k is 3.

then we have to output 123,234,341,412.

#include<stdio.h>

int main()
{
	int a[10];
	int i,j,k=3;
	for(i=0;i<10;i++)
		a[i]=i;
printf("\n");
printf("\nThe array elements are");
for(i=0;i<10;i++)
	{
		printf("%d->",a[i]);
	}
printf("\n");
for(i=0;i<10;i++)
{
	for(j=i;j<i+k;j++)
	{
		printf("%d ",(a[j]%10));
	}
	printf("\n");
}
return 0;
}

- Java Coder November 23, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You are wrong:. IF there are 10 elements you have 120 3 element subsets.

- Mike November 27, 2007 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

k=3 should be an input, unknown.

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

void PrintAllSubsets(int A[], int nALen, int B[], int nBLen, int k)
{
if (nALen <=0 || nBLen <= 0)
return ;

if (nBLen == 1)
{
for (int i=0; i<nALen; ++i)
{
B[0] = A[i];
printf("%s", "sub-array : ");
for (int j=0; j<k; ++j)
printf("%d ", B[j-k+1]);
printf("%s\n", " ");
}
}

for (int i=0; i<nALen-nBLen+1; ++i)
{
B[0] = A[i];
PrintAllSubsets(&A[i+1], nALen-(i+1), &B[1], nBLen-1, k);
}

}


// Sample
const int ALen = 10;
const int BLen = 3;
int A[ALen], B[BLen];
for (int i=0; i<ALen; ++i)
A[i] = i;

PrintAllSubsets(A, ALen, B, BLen, BLen);

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

bool Combination(int *arr, int n, int k)
{

if(arr == NULL || n =< 0 || k =< 0 || k>n)
return false;

int *result = new int[k];
DoCombination(arr,n,result,k,0,0);
delete[] result;
}

void DoCombination(int *arr, int n, int* result, int k, int pos, int count)
{
if(arr == NULL || result == NULL || n <= 0 || k <= 0 || k>n || pos >= k || count >= n)
return;

for(int i=count; i<=(n-k-pos), i++)
{
result[pos] = a[i];
if(pos == k-1)
{
PrintBuffer(result,k);
continue;
}
DoCombine(arr,n,result,k,pos+1,count+1);
}
}

- Swati February 18, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is my version

#include <stdio.h>
void combinations(char * str, int start, int * result, int len, int k, int count);
void main()
{
	int i=0,k=0, count = 0;
	char str[100];
	printf("Enter the string \n");
	gets(str);
	printf("Enter the size of the combinations to be produced \n");
	scanf("%d", &k);
	while(str[i] != '\0')
		i++;
	int result[100];
	combinations(str,0, result, i, k, count);
	printf("\n");
}

void combinations(char * str, int start, int * result, int len, int k, int count)
{
	if(start == len)
	{
		if(k==count)
		{
			printf("\n");
			for(int j=0;j<len;j++)
				if(result[j] == 1)
					printf("%c", str[j]);
		}
		return;
	}
	for(int i=0;i<2;i++)
	{
		if(i && count <= k)
		{
			result[start] = 1;
			count++;			
		}
		else
			result[start] = 0;
		combinations(str,start+1,result,len,k,count);				
	}
}

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

Isn't it similar to dynamic programming coin sum problem?

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

No
It is just a recursive function.
not difficult.

- sunbow.xs@hotmail.com October 10, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Subset(char arr[], int k, int count, int size){
if count == size print ith element with X[i] = 1; return;
if k> length(arr) print ith element with X[i] = 1; return; // subset with size less than 'size'
x[k] =1: subset ( arr, k+1, count +1 ,size);
x[k] =0; subset ( arr, k+1, count, size);
}

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