Linkedin Interview Question
Software Engineer / DevelopersCountry: United States
I think this question wants you to keep track of the range that those numbers constituting that sum occupy. It can easily be added into your solution. I would give a candidate bonus points if they also knew that this algorithm is commonly known as "Kadane's Algorithm" and maybe even that a linear time algorithm was found in 1984. To me it shows someone treats computer science as more than just being a machine for coding interviews, in order to become a widget at big corp. :D
Does it have to be a continuous subsequence?
If so, there is a linear time solution to this. You might want to lookup max subarray/ Kadane's algo
If it does not have to be a continuous subsequence,
just pick out all the positive integers in the array and add them up, this is your answer
This is apparently computing max continuous subsequence. Nobody would actually think LinkedIn pays you 130K for picking out all the positive numbers in an array right?
This was pretty much copied and pasted from the wikipedia page for Maximum_subarray_problem:
private int LargestSum(int[] numbers)
{
if (numbers == null || numbers.Length == 0)
{
return 0;
}
int max_so_far = numbers[0];
int max_ending_here = numbers[0];
for (int i = 1; i < numbers.Length; i++)
{
if (max_ending_here < 0)
{
max_ending_here = numbers[i];
}
else
{
max_ending_here += numbers[i];
}
if (max_ending_here >= max_so_far)
{
max_so_far = max_ending_here;
}
}
return max_so_far;
}
1. To find maximum sum SUBSET: eliminate all negative integers, keep only positive ones. If all numbers are negative, choose the empty subset.
2. To find maximum sum of continuous subsequence: keep summing up numbers and record maximum sum. If the sum is negative, reset it to be 0.
- ninhnnsoc April 23, 2014