Microsoft Interview Question for Software Engineer in Tests






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

the result would be: {1,-2,4, -2, 6}? You miss 4. The sum is 7 which is equal to the given MaxSum.

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

Is it fine that the subarray elements are not continous?

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

The question reads less or equal to the given MaxSum.If this is the case then we can have both {1,-2,4,5,-2} or {1,-2,4,-2,6} so how do you decide what to choose?

- any October 05, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Could anybody retype or word the question a little more clearly.

- any October 05, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

reduce problem to 0-1 knapsack problem with the value of each item is one.

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

I believe subarray implies continuous, so for array = {1, -2, 4, 5, -2, 6, 7} and max=7, answer should be {1, -2, 4, 5, -2}

- Marucho October 05, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I agree with you.

- cctv October 05, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I agree with you too :)

- Lahori December 08, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Actually no, the above answer would be 6, so the maxSubArray sum would be 7 (it gotta be <= maxSum)

- Lahore December 26, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Actually no, the above answer would be 6, so the maxSubArray sum would be 7 (it gotta be <= maxSum)

- Lahori December 26, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I believe it can be solved even if you don't consider it to be a contiguous block. In such a case you would have to consider dynamic programming. I solved the problem using lisp. Here is the code

(defun maxsum(list maxsum &optional sublist)
  (cond ((= (length list) 1) (if (<= (apply #'+ (append sublist list))
				     maxsum)
				 (reverse (append sublist list))
				 (reverse sublist)))
	(t (let ((list1 (if (<= (apply #'+ (cons (first list) sublist))
				maxsum)
			    (cons (first list) sublist)
			    sublist))
		 (list2 (maxsum (rest list)
				maxsum
				(if (<= (apply #'+ (cons (first list) sublist))
					maxsum)
				    (cons (first list) sublist)
				    sublist))))
						    
	     (if (> (length list1)
		    (length list2))
		 list1
		 list2)))))

The output is as follows

maxsum = 7
(maxsum '(1 -2 4 5 -2 6 7) 7)
(6 -2 4 -2 1)

Maxsum = 10;
(maxsum '(1 -2 4 5 -2 6 7) 10)
(1 -2 4 5 -2)

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

someone is trying to get homework done again...

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

Sorry for the confusing, if there are more results, you will only return one result set, the sub array elements are not necessary continually.
Anonymous, can you come up a c code? thanks

- rmod October 06, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Its better to sort the array in assending order, then add each element one-by-one till sum <= maxsum

Complexity N(log N)

- Ashu October 06, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This won't work, for example:
{-5, -4, -3, -2, -2, 0, 3, 5, 8}
The given sum is 8, the anwser will be:
{-4, -2, -2, 0, 3, 5, 8}
so, sorting won't work

- rmod October 06, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I guess u have taken the wrong soln.
In the case which u gave the entire sequence will be the answer even if we sort in ascending order.
{-5, -4, -3, -2, -2, 0, 3, 5, 8}
{-5, -9, -12, -14, -16, -13, -8, 0}

Can u come up with a better example in which greedy doesnt work coz i cant figure out one

- game October 07, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

@Anonymous on October 05, 2009
Would you like to post your solution is some other language, say C/C++/Java etc or at least put the pseudo-code so that it'll be a bit easier for all of us to follow your solution. Thank you.

- dabur October 07, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Assumptions:

- subarray MUST be continuous, else we'd just return all positive integers, or if the largest int should they all be <= 0.

- The FIRST subarray is returned should there be multiple subarrays with the same max sum

- Should the resulting max subarray end in a zero, it will not be picked up. For instance: { 1, 0, 1, 0 } will return { 1, 0 , 1 }. This goes for { 1, 0, 1, 0, 0, 0 ...} as well.

Pseudo Code:

- You will need at least 6 counters to keep track of values.  These will all be initialized as zero at the start, except for max value being set to the smallest possible integer (Integer.MIN_VALUE in Java).
   - Max Sum
   - Current Sum
   - Start Index
   - End Index
   - Cursor
   - Max Value

for( 0 to array length ) {
     add current value (array[i]) to your current sum counter.

   if( current sum > max sum ) {  // Continue your max subarray since it is larger than previous
        max sum is now = current sum
        set start position of your subarray to cursor
        set end position of your subarray to current position
   }
   if ( current sum < 0 ) { // Break your max subarray because it is less than 0
        set current sum back to 0
        update cursor to current position + 1
   }
   if ( current value > max value ) { // Keep track of max value if all values are <= 0
        set max value to current value
   }
}

if( end != 0 ) { // You know a max subarray containing at least 1 positive integer exists
   print max sub array
}
else { // Array was filled with integers <= 0
   print max value
}

The runtime order of this algorithm is O(n) as it only uses a for loop to traverse from beginning to end.

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

If the question is abt contiguous subarray , then here is the soln :
for(i=0;i<n;i++)
{
curr = curr + a[i];
if( curr > max )
{
max = curr;
}
else if(curr < 0)
{
curr = 0;
}
}

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

this is the solution to a different problem..

- nobody October 19, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

agrees with nobody ! :)

- everybody November 06, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here would be the recursive solution:

Opt(j, w)=Max(a[j]+Opt(j-1, MAXSUM-a[j]), Opt(j-1, MAXSUM));

- Aats! November 11, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This dynamic programming approach may answer the question when it finds the exact match with the expected Sum. can you argue how it would work in case it has to find the closest(but lesser and not equal) one ? (i.e. when exact match doesnt exist).

- X November 13, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

more over you are maximizing the sum! shouldnt you be maximizing the number of elements considered in contributing to the SUM ? (i.e. Max(1 + Opt(j-1,..), Opt(..)) instead of Max(a[j]+...)) Correct me if am wrong.

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

u r right it should maximize number of elements considered... but ur earlier comment is wrong about this code nt working for less than maxsum condition, to make it work for lesse or equal to maxsum u have to just put this in
Opt(j,sum){
if(j<0 || a[j]>sum) return 0;
return max(1+Opt(j-1,sum-a[j]), Opt(j-1,sum));
}

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

how about multiply each element by -1 and then solve it as maximum sum problem?

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

If we fix the beginning of the sub array to be x then finding the longest sub-array would be straightforward (by moving the ending of the sub-array y one position at a time until the sum is larger than maxSum). Now let's consider what happens when we move x one position to the left: the ending point of the longest sub-array staring at x+1 will either be less than y (in which case this sub-array can be ruled out from being a candidate for the longest because it is shorter than the previous one) or greater than y. If the new ending point is greater than the current y we can find the new ending point by moving the current ending point one at a time until the sum exceeds maxSum. If we continue this process and keep track of the length, beginning and end, of the longest sub array seen so far then after whole array is scanned we arrive at the longest sub-array.

Pseudo code:

for (int x = 0; x < n; x++)
{
sum = sum - a[x];
while (sum < maxSum)
{
sum += a[y];
y++;
}
if (currentMaxLen < y - x)
{
currentMaxLen = y - x;
currentMaxBegining = x;
currentMaxEnding = y;
}
}

- Anonymous October 18, 2010 | 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