## GE (General Electric) Interview Question for Software Developers

Country: United States
Interview Type: In-Person

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

@Stephen, since the question does not give a length for the subarray, applying Kadane's algorithm for Maximum subarray problem would be more optimal.

``````int getMaxSubarraySum(int[] a) {
if (a == null || a.length == 0)	throw new IllegalArgumentException();

boolean allNegative = true;
int max = Integer.MIN_VALUE;
for (int i : a) {
max = Math.max(max, i);
if (i >= 0)	{
allNegative = false;
break;
}
}

if (allNegative)	return max;

int tempMax = a[0];
max = a[0];
for (int i = 1; i < a.length; i++) {
tempMax = Math.max(tempMax + a[i], 0);
max = Math.max(max, tempMax);
}
return max;
}``````

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

Just had a quick go at this using a sliding window approach in C assuming an array of integers:

``````#include <stdio.h>

// Uses a sliding window
int MaxSumOfSubArray(int *intArray, int len, int subLen)
{
if ( ( ! intArray ) || (subLen > len) )
{
return 0;
}

int blockSum = 0; // To store each possible sum

// Calculate the initial sum
int frontIdx = 0;
while (frontIdx < subLen)
{
blockSum += intArray[frontIdx];
frontIdx++;
}

int maxSum = blockSum; // Initial Result

// Slide the window through the array
int backIdx = 0;
while (frontIdx < len)
{
blockSum -= intArray[backIdx];
blockSum += intArray[frontIdx];

if (blockSum > maxSum)
{
maxSum = blockSum;
}

backIdx++;
frontIdx++;
}

return maxSum;
}

#define TEST_ARRAY_LEN		4
#define TEST_LENGTHS_LEN	6

void main(int argc, char * argv[])
{
int testArray[TEST_ARRAY_LEN] = { 1, 3, 5, 4 };
int testLengths[TEST_LENGTHS_LEN] = { 0, 1, 2, 3, 4, 5 };

printf("For testing array: { ");
for (int i = 0; i < TEST_ARRAY_LEN; i++)
{
printf("%d ", testArray[i]);
}
printf("}\n");

for (int i = 0; i < TEST_LENGTHS_LEN; i++)
{
printf("Max sum for subarray of length %d: %d\n", testLengths[i], MaxSumOfSubArray(testArray, TEST_ARRAY_LEN, testLengths[i]));
}
}``````

Seems to work pretty quickly but doesn't really employ any shortcuts.

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

We could use divide and conquer approach in this case.

``````public class MaxSumOfSubArray {
public static int findMaxSumOfSubArray(int[] elements) {
return findMaxSumOfSubArray(elements, 0, elements.length - 1);
}

private static int findMaxSumOfSubArray(int[] elements, int start, int end) {
if(start == end) {
return elements[start];
}
int sum = 0;
int mid = (start + end)/2;

int left = findMaxSumOfSubArray(elements, start, mid);

int right = findMaxSumOfSubArray(elements, mid+1, end);
int maxSum = 0;

int leftSum = Integer.MIN_VALUE;
int rightSum = Integer.MIN_VALUE;
for(int i = mid + 1; i <= end; i++) {
sum += elements[i];
leftSum = Math.max(sum, leftSum);
}
sum = 0;
for(int i = mid; i >= 0; i--) {
sum += elements[i];
rightSum = Math.max(rightSum, sum);
}

maxSum = Math.max(left, right);
int leftRightSum = leftSum + rightSum;
return Math.max(maxSum, leftRightSum);
}

public static void main(String[] args) {
int arr[] = {2, 3, -4, -5, -7};
int max = MaxSumOfSubArray.findMaxSumOfSubArray(arr);
System.out.println("Max: " + max);
}
}``````

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.

### 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.