Google interview question

Write a function to get maximum consecutive sum of integers from an array.

Interview Answers

Anonymous

4 Aug 2013

Contrary to all the solutions posted the correct solution runs in O(n) and Lilica posted the Python solution with a link to the Wikipedia source. There's also a easy C solution that any Java or .Net developer should be able to understand.

2

Anonymous

27 Mar 2012

Sour code: int LargestSubSum(int[] arr) { int maxSum = 0;//this is value to return int tempSum = 0;//this is to keep track of current sum. check Q16 for more details for(int i=0; i0)//still can be part of the final largest sum's part { tempSum += arr[i]; if(tempSum>maxSum) maxSum = tempSum; } else//this can be discarded as the possiblity being part for largest sum tempSum = 0;//reset } return maxSum;//do not forget to return } I also made a video to demo the whole process to solve this problem including a test case at http://youtu.be/Q7Rf79mLs7M

4

Anonymous

1 Apr 2012

Penguin: Try this case {5,-1,2,-2} the max sum is 5-1+2=6. I am not sure why my reply (2nd reply) was tagged as not helpful, but that's the working code including a testing case.

Anonymous

8 Apr 2012

function maxarr($arr, $len) { $maxsofar = 0; $maxendinghere = 0; foreach ($arr as $entry) { $maxendinghere = max(0, $maxendinghere + $entry); $maxsofar = max($maxendinghere, $maxsosfar); } }

Anonymous

17 Apr 2012

not sure that all these propositions are working in real cases : let's consider this array : t = [ -1,-2,10,11,-2,-1 ] none of your solutions seem to consider a sub sum that can be bounded inside the array. I feel like a solution with two iterators should fit. a function sum(int beg, int end, int* array) returning the sum between the indexes. and something like : int highestConsecutive(int* array){ int beg=0, end=len(array); int biggest_sum=-MAXINT; for(end=len(array);end>0;--end){ for(beg=0; beg < end; ++beg){ int s_btw=sum(beg,end); biggest_sum=s_btw; } } return biggest_sum; } should return the highest consecutive sum.... I've not tested it since I've juste been writing it here, but I think that it's on the way to a correct solution, and it seems to have something like an n*log(n) complexity. such a solution even allows you to return the indexes of the sum found... this have to be checked and optimized but still...

Anonymous

23 Apr 2012

oups ^^ there a lack in this code it seems :) , lack of the if(s_btw > biggest_sum) :) appologize for this ;)

Anonymous

2 Apr 2013

def max_subarray(A): max_ending_here = max_so_far = 0 for x in A: max_ending_here = max(0, max_ending_here + x) max_so_far = max(max_so_far, max_ending_here) return max_so_far Source:http://en.wikipedia.org/wiki/Maximum_subarray_problem#Kadane.27s_algorithm

1

Anonymous

4 Apr 2012

In Java public static int findMaxConsecutive(int[] arr){ int max =0; int temp = 0; for(int i=0; i

1

Anonymous

30 Mar 2012

public static int recursiveMaxSum(int set [], int index){ if(index==set.length) return 0; if(set[index] < 0) return rMaxSum(set, ++index); return set[index] + rMaxSum(set, ++index); }