Microsoft Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
1
of 3 vote
here is an O(N)solution max contain the maximum sum and maxstart and maxend contains the indexes whose sum is maximum {{{ maxsum(int a[],int n) { int sum=0,maxsum=0,start=0,end=0,maxstart,maxend; for(int i=0;i<n;i++) { sum=sum+a[i]; if(sum<0) { sum=0; start=i+1; } if(sum>max) { max=sum; maxstart=start; maxend=end; } } - techcoder October 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

No need of end variable....your program is giving wrong end point....replace the last statement from maxend=end to maxend=i and you will have correct end point.

- Anshul December 15, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

No need of end variable....your program is giving wrong end point....replace the last statement from maxend=end to maxend=i and you will have correct end point.

- Anshul December 15, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

int main()
{
int a[] = {1,-1,2,3,4,-10,1,7,8,4,5,6,-6};
int i,j,n = 13;
int maxSum = a[0];
int sum = a[0];
int sumStartIndex = 0;
int sumEndIndex = 0;
int curIndex = 0;
int maxSeqSum = a[0];

for(i = 1;i <n;i++)
{
sum = sum + a[i];
if(sum > maxSum)
{
maxSum = sum;

}
else if(sum < 0)
{
if(maxSeqSum < maxSum)
{
maxSeqSum = maxSum;
sumStartIndex = curIndex;
sumEndIndex = i - 1;
}
curIndex = i + 1;
maxSum = 0;
sum = 0;
}
}
printf("maxSeqSum = %d \t maxSum = %d \n",maxSeqSum,maxSum);
if(maxSeqSum < maxSum)
{
sumStartIndex = curIndex;
sumEndIndex = n - 1;
}
for(i = sumStartIndex;i <= sumEndIndex;i++)
printf("%d \t",a[i]);

return 0;
}

- Syam March 31, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Look below the code in java. It will print the sequence which makes longest sequence.But first the array need to be sorted.

static void getMax(int[] a)
	{
		int len=a.length;
		int insq=0;
		int count=0;
		int maxcount=0;
		int startin=0;
		int endin=0;
		for (int i=0;i<len-2;i++)
		{
			if (a[i+1]==a[i]+1)
			{
				insq=1;
				count=0;
				int subcount=0;
				while(a[i+1]==a[i]+1)
				{
					count+=a[i];
					i++;
					subcount++;
				}
				count+=a[i];
				if(maxcount<count)
				{
					maxcount=count;
					startin=i-1;
					endin=i+subcount-1;
				}
			}
		}
		
		System.out.printf("Sequence= ");
		/*System.out.println(endin);*/
		for (int p=startin;p<=endin;p++)
		{
			System.out.print(a[p-1]);
			System.out.print(",");
		}
			System.out.printf("maximum size=%s", maxcount);
	}

- nagaraj_n@hotmail.com October 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream.h>
void main()
{
int a[20], i = -1, max = 0, n, sum, maxi, maxj;
cout<<" enter the elements of the array";
do {
i++;
cin>>a[i];
}while ( a[i] != -9999 && i <19);
n = i;
for ( i = 0;i<n; i++) {
sum = 0;
for ( j = i +1; j<n; j++) {
sum =sum +a[j];
if ( sum>max) {
max = sum;
maxi = i;
maxj = j;
}
}
}
cout<<" Maximum continous sum:";
for ( i = maxi; i< maxj; i++)
cout<<a[i]<<'\t';
cout<<"The maximum sum is :"<<max;
}

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

int sum= 0, maxsum=0;
for(int i= 0; i< n; i++) //n is length of array
{
sum += a[i]; //a is array name
if( sum > maxsum)
maxsum = sum;
else if(sum < 0)
maxsum = 0;
}

- PS November 04, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

the code is fine but in the "else if" when sum is less than 0 the "sum" should be set to 0 itself.

int maxSum(int a[])
{
        int n=a.length;
        int sum=0,maxSum=0;
        for(int i=0;i<n;i++)
        {
            sum=sum+a[i];
            if(sum>maxSum)
                maxSum=sum;
            else if(sum<0)
                sum=0;
        }
        return maxSum;
}

- KK November 07, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi KK the algorithm which ur using is kadanes algorithm..?

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

/* Solution # 1 */
int findLargestSumSubArray(int arr[], int len, int& startIndex, int& endIndex)
{
int sum = 0;
int minSum = 0;
int minIndex = -1;
int subArrLargeSum;

subArrLargeSum = 0;
startIndex = -1;
endIndex = -1;

//iterating through the given array
for (int begin = 0; begin<len; begin++)
{
sum = sum + arr[begin];
if (sum <= minSum)
{
//saving the minimum sum and starting index
minSum = sum;
minIndex = begin;
}

if (sum - minSum > subArrLargeSum)
{
//starting index of sub array
startIndex = minIndex + 1;

//ending index of sub array
endIndex = begin;

//largest sum of sub array so far
subArrLargeSum = sum - minSum;
}
}

//Printing the sub array elements
if( subArrLargeSum <=0 || ((startIndex < 0) && (endIndex < 0)) )
{
printf("Numbers: No sub array, because sum of any sub array is less than or equal to zero\n");
}
else
{
int i = startIndex;
int j = endIndex;

//printing the sub array
//Note: this steps can be moved to main() function also
printf("Numbers: ");
while( i <= j)
{
printf("%d ", arr[i++]);
}
}

printf("\n\nStarting Index = %d\n", startIndex);
printf("Ending Index = %d\n\n", endIndex);

//returning the largest sum of sub array
return subArrLargeSum;
}

This algorithm scans an array from 0 to N-1 elements. When the largest sum of an array found, the algorithm will remembers the start and end index of that sub-array. An array is scanned only once. Hence, the time complexity of this algorithm is O(N).

- Savi April 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

onestopinterviewprep.blogspot.com/2014/03/namespace-arrayproblem-write-function.html

- Wellwisher March 26, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Simple ruby solution:

def find_max_partial(list)
  result = list.reduce([0,0]) do |max, current|
    max[1] += current
    if max[1] < 0
      [max[0], 0]
     else
      [[max[0],max[1]].max, max[1]]
    end
  end
  result[0]
end

- lebron February 17, 2016 | 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