Microsoft Interview Question for Software Engineer in Tests






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

Supose S is the array of integers, N the length and sum the current sum.
Let best be the solution at current step.We keep in x and y the limit of the best array
Initally,best = -INF and sum = 0.
Then

for(i = 0 ;i < N ; ++i)
  {
   if(sum <= 0) {sum = S[i] ; idx = i; } 
   else sum += S[i];
   if(best < sum) {best = sum;x = idx ; y = i;}

The solution will be the subarray [x,y]. Complexity O(N) time O(1) memory and one loop.

- bogdan.cebere October 24, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

gud job bogden....

- Anonymous September 03, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
using namespace std;
int main()
{
    int max_so_far = 0,max_ending_here =0;
    int A[] = {14,-15,6,-3,2,3,4,5,-1};
    int numOfLoops = 0;
    int size = sizeof(A)/sizeof(int);
    for(int i=0;i<size;i++){
	++numOfLoops;
        max_ending_here = (max_ending_here + A[i])>0?max_ending_here + A[i]:0;
        max_so_far = max_ending_here>max_so_far?max_ending_here:max_so_far;
    }
    cout<<"number of loopings:"<<numOfLoops<<endl;
    cout<<"Maximum sum::"<<max_so_far<<endl;
}

- chennavarri October 24, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I don't think this is correct. Say your array is:
-5, -1, -2...

- Anonymous October 24, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Ya, I guess kadane's algorithm gives 0 sum for negative sums. The code above is kadane's algorithm

- chennavarri October 24, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think we can add one more variable, which finds the highest number after being initialized with the first element in the array.
And while returning we can check, and return the max.


int main()
{
int max_so_far = 0,max_ending_here =0;
int A[] = {14,-15,6,-3,2,3,4,5,-1};
int max_element = A[0];
int numOfLoops = 0;
int size = sizeof(A)/sizeof(int);
for(int i=0;i<size;i++){
++numOfLoops;
max_element = A[i] > max_element? A[i]:max_element;
max_ending_here = (max_ending_here + A[i])>0?max_ending_here + A[i]:0;
max_so_far = max_ending_here>max_so_far?max_ending_here:max_so_far;
}
cout<<"number of loopings:"<<numOfLoops<<endl;
cout<<"Maximum sum::"<<max_so_far > max_element? max_so_far:max_element<<endl;
}

- mahi October 27, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I can't believe interviewers still actually ask this question....

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

After trying out this question, it looks so simple to write the code for that.

int main()
{
	int arr[]={14,-15,6,-3,2,3,4,5,-1};
	int size = sizeof(arr)/sizeof(int);
	int i,maxsum=arr[0];
	for(i=1;i<size;i++)
	{
		maxsum = ( (maxsum+arr[i])<maxsum) ? ( maxsum>arr[i] ? maxsum:arr[i] ) : (maxsum+arr[i]) ;
	}
	printf("Maximum sum:%d",maxsum);
}

- Arun November 01, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think the subarray should be continuous. Your solution only add all the positive numbers together...

- Anonymous November 02, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

If the answer needs to be from contiguous subarray, soln is(derived from kadane alg)

int main()
{
int a[]={-2, -3, 4, -4, 50, 5, -3};
printf("\nMax Sum-%d",maxSubArraySum(a,sizeof(a)/sizeof(a[0])));
return;
}


int maxSubArraySum(int a[], int size)
{
int max_so_far = 0, max_ending_here = 0;
int i;
int negative=a[0];
for(i = 0; i < size; i++)
{
max_ending_here = max_ending_here + a[i];
if(max_ending_here < 0)
max_ending_here = 0;
else if (max_so_far < max_ending_here)
max_so_far = max_ending_here;
if(max_so_far==0 && negative<a[i])
{
negative=a[i];
}
}
return (max_so_far!=0?max_so_far:negative);
}

- Arun November 03, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

int main()
{
	int a[]={-2, -3, 4, -4, 50, 5, -3};
	printf("\nMax Sum-%d",maxSubArraySum(a,sizeof(a)/sizeof(a[0])));
	return;
}


int maxSubArraySum(int a[], int size)
{
   int max_so_far = 0, max_ending_here = 0;
   int i;
   int negative=a[0];
   for(i = 0; i < size; i++)
   {
     max_ending_here = max_ending_here + a[i];
     if(max_ending_here < 0)
         max_ending_here = 0;
      else if (max_so_far < max_ending_here)
         max_so_far = max_ending_here;
     if(max_so_far==0 && negative<a[i])
     {
	negative=a[i];
     }
   }
   return (max_so_far!=0?max_so_far:negative);
}

- Arun November 03, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int FindMaxLength(int []inputArray)
{
int prevValue=inputArray[0];
int sum=0;
for(int i=1;i<inputArray.Length;i++)
{
if((sum+inputArray[i])>inputArray[i])
sum+=inputArray[i];
else
sum=inputArray[i];
}
return sum;
}

- Biruk Solomon November 03, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

ideone.com/WmT0k

- Anonymous November 08, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

i don't think this solution is correct,

because that solution returns the largest continues positive subarray. however a result of this question can contain negative numbers.

for example:

-5, 45,-1,76 -7

the answer should be 45,-1,76

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

Void findSubArray(int[] array){
int best=0;
int worst=0;
int worstIndex=0;
int start=0;
int end=0;
int sum=0;
for (int i=0; i<array.length;i++){
sum+=array[i];
if((sum-worst)>best){
start=worstIndex;
end=i;
best=sum-worst;
}
if(sum<worst){
worstIndex=i;
worst=sum;
}
}
}

- Anonymous November 11, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int GetMaxSubArray(int[] Numbers, out int start, out int end)
        {
            int sum = 0;
            int maxsum = int.MinValue;
            start = 0;
            end = 0;

            for (int i = 0, j = 0; j < Numbers.Length; j++)
            {
                sum += Numbers[j];
                if (sum > maxsum)
                {
                    maxsum = sum;
                    start = i;
                    end = j;
                }

                if (sum < 0)
                {
                    sum = 0;
                    i = j + 1;
                }
            }

            return maxsum;
        }

- Anonymous November 11, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class MaxSubArray {
	
	public static void main(String[] args){
		
		int[] arr = new int[]{-2,4,-3,6,8,-3,2,-4,-1};
		MaxSubArray max = new MaxSubArray();
		max.subArray(arr);
	}

	public int subArray(int[] array){
		int maxsum =0;
		int sum =0;
	        int start=0;
	        int end=0;
		
		for(int i=0; i<array.length; i++){
			sum = sum + array[i];
			if(maxsum < sum){
				maxsum=sum;
				end=i;
			}
			if(sum<0){
				start=i+1;
				sum=0;
			}
		}
		System.out.println("SUM: "+maxsum+", Start Index: "+start+", End Index: "+end);
		return maxsum;
	}
}

- girishv.84 December 14, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

THIS ONE IS SIMPLE AND BEST OF ALL ALTHOUGH ITS NOT MY CODE

sum and maxsum = 0;

for i=0 to i<n
sum += a[i];
if(sum<0)
sum = 0;
else if(maxsum < sum)
sum = maxsum;

- hi December 28, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int findMaxSub(int [] input ){
		int max=Integer.MIN_VALUE;
		int current=0;
		for(int i=0;i<input.length;i++){
			current+=input[i];
			if(current>max)
				max=current;
			if(current<=0)
				current=0;
		}
		return max;
	}

this code should work.

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

all of you are retarded

- i hate nigger April 21, 2011 | 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