23andMe | Phone Screen | Aggregate Max Tail Sequence Sums

A valid tail-sequence of an array meets the following criteria:

  • Contains multiple consecutive elements of the array
  • Contains the first OR last element of the array
  • Does not contain the first AND last element of the array

As an example, for array, [1, 2, 3, 4], the valid tail-sequences are:
[1, 2], [1, 2, 3], [2, 3, 4], [3, 4]

Implement a function, aggregateMaxTailSequenceSums(), which:

  • Accepts argument:
    • An array of integers
  • Outputs:
    • An integer which is the maximum possible sum of all elements using one or more tail-sequences found in the array
    • If no tail-sequence exists in the array, the function should return null

Example 1:

Input: [-2, 3, -1, 2, 0]
Output: 10
Explanation: using tail-sequences [-2, 3] => 1, [-2, 3, -1, 2] => 2, [3, -1, 2, 0] => 4, [-1, 2, 0] => 1, [2, 0] => 2
1 + 2 + 4 + 1 + 2 = 10

Example 2:

Input: [1, -6, 2, -7, 4, -4]
Output: 0
Explanation: using tail-sequence [4, -4]

Example 3:

Input: [-1, -2, -4, 1]
Output: -3
Explanation: using either tail-sequence [-1, -2] or [-4, 1]
Comments (2)