Epic Systems Interview Question for Software Engineer / Developers


Country: United States




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

We can solve this problem using Kandane's algorithm

max_subarray(A){
    max_so_far = max_ending_here = 0
    for x in A:
        max_ending_here = max(x, max_ending_here + x)
        max_so_far = max(max_so_far, max_ending_here)
    return max_so_far

- Anonymous July 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 3 vote

Its simple. Find the sum until the sum gets negative. When the sum gets negative start from zero.

Eg: 2,3 , -2 , 4 , -8 , 8, 9, -2, 10

1: Initially the sum is 2 (first no.). Start from second if first no. is zero and so on.
2: Keep adding the number until the sum is negative. (2+3-2 + 4) =7 but adding -8 gives sum = -1 so start from zero.
3: Repeat step 2 again. Now the sum is 8 + 9 -2 + 10 =25. This is max contiguous sum.

Note: do not include the last no. in sum if it is negative.

Complexity: O(n)

- androidify July 20, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package programs;
import java.util.Scanner;

public class dynamicprogram2 {
static int[] sequence;
static int[] memory;
public static int MaxSum(int[] x)
{
int len = x.length;
int max=0;
int status = 0;
int temp;
memory[0]=sequence[0];
//int temp;
for (int i=0;i<len;i++)
{
if(sequence[i]>0)
{
status =1;
break;
}
}
if(status == 1)
{
for (int i=1;i<len;i++)
{
temp=memory[i-1]+sequence[i];
if(temp>0)
{
memory[i] = temp;
if(max<memory[i])
max = memory[i];

}
else
memory[i]=0;
}

}
else
{
max = sequence[0];
for (int i=1;i<len;i++)
{
if(sequence[i]>max)
max = sequence[i];
}

}
return max;
}
public static void main(String[] args)
{
int length = 0;
Scanner a = new Scanner(System.in);
System.out.println("Enter the length of your sequence: ");
length = a.nextInt();
sequence = new int[length];
memory = new int[length];
for(int i=0;i<sequence.length;i++)
{
memory[i] = 0;
}
System.out.println("Enter the element of your sequence one by one: ");
for(int i=0;i<sequence.length;i++)
{
sequence[i] = a.nextInt();
}
System.out.println("Maximum contiguous sub-sequence sum of the given sequence is : "+ MaxSum(sequence));
}

}

- Biswajit Sinha August 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
using namespace std;

int max(int a,int b)
{
	return (a>b)?a:b;
}

int contg_sum(int *arr,int length)
{
	if(length<0){cout<<"ERROR, Length of array cannot be negative";return -1;}
	if(length==0){return 0;}
	int sum_so_far=arr[0];
	int max_so_far=arr[0];
	for (int i=1;i<length;i++)
	{
		sum_so_far=max(sum_so_far+arr[i],arr[i]);
		max_so_far=max(max_so_far,sum_so_far);
	}
	return max_so_far;
}

int main()
{
	int array[]={2,3,4,2,-11,4,6};
	int t=contg_sum(array,7);
	cout<<t;
}

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

An implementation based on what user "androidify" stated above.

Below code works for all cases.

#define ERROR -1
int maxSum_subArray(const int * arr, const int arrSize) {
    int i;
    int currentMax;
    int maxsoFar;

    if((NULL == arr) || (arrSize <= 0)) {
        printf("\n\n"); printf(" ERROR! "); printf("\n\n");
        return ERROR;
    }

    currentMax=0;
    maxsoFar=INT_MIN;
    for(i=0; i<arrSize; ++i) {

        currentMax += arr[i];

        if(currentMax > maxsoFar) {
            //printf("\n\n"); printf(" maxsoFar at i = %d, currentMax = %d", i, currentMax); printf("\n\n");
            maxsoFar = currentMax;
        }

        if(currentMax < 0) {
            //printf("\n\n"); printf(" -ve currentMax at i = %d", i); printf("\n\n");
            currentMax=0;
        }
    }

    return maxsoFar;
}

- helper December 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

meet this problem in my online test for summer intern

- cronaldo February 05, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Python working code, using max()

"""
1:45

Written round question for Epic systems. They asked two dynamic programming problems. 
Write a dynamic programming solution for finding maximum contiguous sub-sequence sum.
"""
class MaxSub(object):
  def __init__(self, listNum):
    if listNum is None:
      print 'Invalid!'
      raise SystemExit
      
    self._listNum = listNum
    
  def getMaxSum(self):
    curMax = 0
    maxMax = 0
    for num in self._listNum:
      curMax = max(num, curMax + num)
      maxMax = max(maxMax, curMax)
      
    return maxMax
    
m = MaxSub([2, 3 , -2 , 4 , -8 , 8, 9, -2, 10])
print m.getMaxSum()

- tosay.net March 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class MaxContSum {

    public static void main(String[] args) {
        int[] array = new int[]{2, 3, -2, 4, -8, 8, 9, -2, 10};
        int[] sum = new int[array.length];
        int max = array[0];
        sum[0] = array[0];
        int start=0,end=0;
        for (int i = 1; i < array.length; i++) {


            if (array[i] < sum[i - 1] + array[i]) {
                sum[i] = sum[i - 1] + array[i];
            } else {
                 sum[i] = array[i];
                start=i;
            }

            if (max < sum[i ])
            {
                max = sum[i ];
                end=i;
            }


        }

        System.out.println(max+" starts at "+start+" ends at "+end);
    }

}

- Anonymous February 12, 2014 | 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.
-1
of 1 vote

You can understand the solution to this problem by watching this wonderful video tutorial on maximum sub sequence problem
youtube.com/watch?v=hXlv88ddcgw

- Anonymous July 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

video is wrong. he is missing one step in the algorithm.

in the example he works out first two numbers are -2 and -3. he get max sum as -3 instead of -2.

- mythe July 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

the video is not wrong in my opinion. the Max seq sum for the first 2 elements is actually the second element, -3.

- me_trik July 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

the sequence he uses is

-2, -3, 4, -1, -2, ...

check around 2 min mark.

last time I checked -2 > -3 ...
not sure if me_trik is serious or trying to troll. i hope its the latter.

- mythe July 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Its max(-2 + -3, -3) = -3. So there is new window starting from -3. The explanation is correct

- max August 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@mythe... u hav not understood the problem..or the solution!! pls understand it before u say the video is wrong...
-2 is greater than -3, but -2 is not a subsequence at s(i).. the subsequence is -2,-3 which is less than -3.. ie, the window should have the element at i.

- duh October 31, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

S(i-1) is maximum sub-sequence sum till element i-1. Now if we are comparing S(i-1) + a[i] and a[i], it means we are assuming that 'max subsequence' sum of S(i-1) is ending at i-1. and element a[i] can also be appended to that.

- Vikas November 17, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Using Dynamic Algorithm ,
sum[i] for each element i = sum(all elements till o to i)
maxStartIndexArray[i] = index of start of the subarray.

int inputLength = this.input.length;
	
int[] sum = new int[inputLength];
int[] maxStartIndexArray = new int[inputLength];

//S[i+1] = max(S[i] + A[i+1], A[i+1]) 
//S[0]   = A[0]

sum[0] = input[0];
int maxSum = sum[0];
int maxStartIndex = 0;
int maxEndIndex = 0;
int current;
for(int index = 1 ; index < inputLength - 1 ; index++){
	if(sum[index - 1] > 0)
	{
//S[1] = S[0] + INPUT[1]
sum[index] = sum[index -1] + input[index];
maxStartIndexArray[index] = maxStartIndexArray[index -1];
	}else{
     //since current value is negative we end the max-substring.
     //new max substring beings at the current index
      sum[index] = input[index];
       maxStartIndexArray[index] = index;
	}
if(sum[index] > maxSum){
         maxSum = sum[index];
         maxStartIndex = maxStartIndexArray[index];
         maxEndIndex = index;
	}
}
System.out.println("MaxSum :" + maxSum);
System.out.println("Start Index :" + maxStartIndex);
System.out.println("End Index :" + maxEndIndex);

- ameyakarkal October 27, 2012 | 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