Microsoft Interview Question for Software Engineer / Developers






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

//Use Recursion to solve it...runtime depends on array size and group size
//Brute force solution, generates nCg combinations
//where n - no. of elements & g - groupsize
//

#include<iostream>

int sum_number(int sum,int *arr,int index,int size,int gsize,int total);

int group[20],count=0;

void main()
{
	int arr[] = {3,4,16,2,13,8,5,7,2,9};

	sum_number(0,arr,0,sizeof(arr)/4,4,28);
}

int sum_number(int sum,int *arr,int index,int size,int gsize,int total)
{
	if(gsize>0)
	{
		for(int i=index;i<size;i++)
		{
			sum = sum + arr[i];
			group[count++] = arr[i];
			sum_number(sum,arr,i+1,size,gsize-1,total);
			count--;
			sum = sum - arr[i];
		}
	}
	else
	{
		if(sum==total)
		{
			for(int i=0;i<count;i++)
			{
				printf("%d ",group[i]);
			}
			printf("Sum %d\n",sum);
		}
	}
	return sum;
}

- Anonymous June 16, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

oh and my name is Arun...goodluck :)

- fedex June 16, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I came up with a N^2 algorithm. Can we improve it ? Any comments ?

- Daniel Johnson February 17, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think N^2 is impossible.

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

Iterate over each element A - then iterate A+1 to N for O(n^2), then ping a hash table for the difference in O(1) in a typical case... Not really O(N^2), since a hashtable is O(N) but it's pretty safe to assume O(1) is reasonable for hashtable lookups.

Would you consider an algorithm that iterates over an array and pings a hashtable to be O(N^2)? I wouldn't, it would be misleading IMO.

- Matt March 16, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Since we have to return all possible groups, which could be well over n^3 , say for groupsize = 7, any O(N^2) algorithm will be wrong.

No need to look at any implementation details for that.

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

This is an instance of knapsack problem. So we can use dynamic programming. The 'total' given can be thought of as the total value. The individual numbers represent individual values. Thee group size can be thought of as the weight constraint. Each number adds a weight equals 1. Solve the knapsack now.

- Deepa March 02, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can you explain a bit more? I don't think your solution works.

- russoue February 09, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is a sum of sums problem - NP complete.

Using the knapsack algorithm will work (knapsacks are limited to size 3)

- dood July 01, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

knapsack alrite...but knapsack does not account for multiple solutions...need some tweak so we do not return as soon as we fill the sack once... need to keep continuing after printing the result to check for other possiblites...

- king_k July 05, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I agree with King_k. Brutal force solution would be O(N^2 * log(n)).

First, sort the array, takes O(nlogn). Then, iterate through first and second dimension with N^2, the last one would be found with binary search. Overall time complexity is O(N^2 * log(n)).

- Bill July 05, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

find the subset of the array of the size "groupsize" and test whether the total equals to the given total.

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

how can we use DP here?its not an optimization problem in terms of the sums...?

- Anonymous March 30, 2009 | 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