Microsoft Interview Question for SDE-2s


Team: STB and MVO
Country: India
Interview Type: In-Person




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

private static int maxContigousWithoutDP(int A[], int n) {
		int sumSoFar = 0, sumEndingHere = 0;
		int startInd=0;
		int endInd = 0;
		for (int i = 0; i < n; i++) {
			sumEndingHere = sumEndingHere + A[i];
			if (sumEndingHere < 0) {
				sumEndingHere = 0;
				continue;
			}
			if (sumSoFar < sumEndingHere)
			{
				startInd=endInd;
				endInd=i;
				sumSoFar = sumEndingHere;
			}
			
		}
		System.out.println("Start Ind & End Ind :-"+startInd+" "+endInd);
		return sumSoFar;
	}

- Vir Pratap Uttam May 12, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void maxsum(int *a,int n){

int max_so_far=a[0],max_end_here=a[0];
int start,end,temp_start,i;

for(i=0;i<n;i++){
	if(max_end_here<0){
		max_end_here=a[i];
		temp_start=i;	

	}
	else
		max_end_here += a[i];

	if(max_end_here>max_so_far){
		max_so_far=max_end_here;
		start=temp_start;
		end=i;
		
	}

}

printf("( %d , %d )  %d",start,end+1,max_so_far);

return;
}

- Anonymous April 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Kadane's algorithm

- Anonymous April 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Dynamic programming:-Maximum subarray problem.

- nishant.cena April 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

using System;

class Program
{
    public static void Main(){
        int[] a = {1,2,3,-3,4};
        int n = a.Length;
        int maxSum = 0;

        for(int i=0;i<n;i++)
        {
            for(int j=i+1; j<n;j++)
            {
                int k=i;
                int workingSum = 0;
                while(k <= j)
                {
                    workingSum += a[k];
                    k++;
                }
                if(workingSum > maxSum)
                    maxSum = workingSum;
            }
        }

        Console.WriteLine(maxSum);
    }
}

- Antony Thomas April 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

if a[i] < a[i] + a[i-1] then
a[i] = a[i] + a[i-1] for all i>0 and then find maximum

time complexity = O(n)
and no extra memory required

- goelrinku90 April 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

How come time coplexuty is O(n) here ?

- Anil Singh April 20, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

because we traverse array only twice

- goelrinku90 April 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class ArrayMaxSum {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		String string = "1,2,-10,3,4,-20,5,6,4,-50,9,0,-1";
		String[] numbers = string.split(",");

		LinkedList<String> lst = new LinkedList<String>();
		LinkedList<String> tmp = new LinkedList<String>();

		int max_so_far = 0;
		int max_ending_here = 0;

		for (int i = 0; i < numbers.length-1; i++)
		{
			
			max_ending_here = max_ending_here + Integer.parseInt(numbers[i]);
			lst.add(numbers[i]);

			if (max_ending_here < 0)
			{
				max_ending_here = 0;
				lst.clear();
			}

			if (max_ending_here > max_so_far)
			{
				tmp.clear();
				max_so_far = max_ending_here;
				tmp.addAll(lst);
			}

		}

		for(String str : tmp)
		{
			System.out.print(str+", ");
		}
		System.out.print(" = "+max_so_far);
	}

}

- Anil Singh April 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Time complexity is O(n) here.

- Anil Singh April 20, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

using System;

namespace Algorithms
{
    class Program
    {
        static void Main(string[] args)
        {
            Program p = new Program();
            int[] array = { 31, -41, 59, 26, -53, 58, 97, -93, -23, 84 };

            int result = p.ArrayMaxSum(0, array.Length - 1, array);
            Console.Write(result.ToString());
            Console.ReadKey();
        }

        public int ArrayMaxSum(int lowerBound, int upperBound, int[] array)
        {
          
            if (lowerBound > upperBound)
                return 0;

            if ( lowerBound == upperBound )
            {
               return Math.Max(0,array[lowerBound]);
             }

            int mid = (lowerBound + upperBound) / 2;

            int sum = 0;
            int lmax = 0;

            for (int i = mid; i >= lowerBound; i--)
            {
                sum = sum + array[i];
                lmax = Math.Max(lmax, sum);
               
            }

            sum = 0;

            int rmax = 0 ;
            for (int i = mid+1 ; i <= upperBound; i++)
            {
                sum = sum + array[i];
                rmax = Math.Max(rmax, sum);
               
            }

          
            return Math.Max((lmax + rmax), Math.Max(ArrayMaxSum(lowerBound, mid, array), ArrayMaxSum(mid + 1, upperBound, array)));
        }
    }
}

- deepak tripathi April 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

complexity is O ( n log n )

- deepak tripathi April 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static Boolean FindMaxSusequenceSum(int[] N, int first, int last, ref int sum, ref int start, ref int end )
{
if (N == null) return false;
if(N.Length<=last || first>last) return false;
if(first==last)
{
sum = N[first];
start = first;
end = last;
return true;
}
int r_sum=0;
int r_start=0;
int r_end=0;

FindMaxSusequenceSum(N, first + 1, last, ref r_sum, ref r_start, ref r_end);


if (N[first] > r_sum)
{
sum = N[first];
start = first;
end = first;
}
else if (r_start == first + 1)
{
if (N[first] >= 0)
{
sum = r_sum + N[first];
start = first;
end = r_end;
}
else
{
sum = r_sum;
start = r_start;
end = r_end;
}
}
else if( r_start > first+1 )
{

int sum2 = 0;
for (int i = first; i < r_start; i++)
{
sum2 += N[i];
}

if (sum2 >= 0)
{
sum = sum2 + r_sum;
start = first;
end = r_end;
}
else
{
sum = r_sum;
start = r_start;
end = r_end;
}
}
return true;
}

- CoquitlamSDU July 03, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static Boolean FindMaxSusequenceSum(int[] N, int first, int last, ref int sum, ref int start, ref int end )
        {
            if (N == null)  return false;
            if(N.Length<=last || first>last) return false;
            if(first==last)
            {
                sum = N[first];
                start = first;
                end = last;
                return true;
            }
            int r_sum=0;
            int r_start=0;
            int r_end=0;

            FindMaxSusequenceSum(N, first + 1, last, ref r_sum, ref r_start, ref r_end);


            if (N[first] > r_sum)
            {
                sum = N[first];
                start = first;
                end = first;
            }
            else if (r_start == first + 1)
            {
                if (N[first] >= 0)
                {
                    sum = r_sum + N[first];
                    start = first;
                    end = r_end;
                }
                else
                {
                    sum = r_sum;
                    start = r_start;
                    end = r_end;
                }                
            }
            else if( r_start > first+1 )
            {

                int sum2 = 0;
                for (int i = first; i < r_start; i++)
                {
                    sum2 += N[i];
                }

                if (sum2 >= 0)
                {
                    sum = sum2 + r_sum;
                    start = first;
                    end = r_end;
                }
                else
                {
                    sum = r_sum;
                    start = r_start;
                    end = r_end;
                }
            }
            return true;
        }

- CoquitlamSDU July 03, 2014 | 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