Best stretch of days

arrays

Best stretch of days

Robinhood Python Interview Question

Robinhood shows investors the best stretch of consecutive days in their portfolio's history, measured by the total change in value.

Write a function max_subarray_sum(daily_changes) that returns the largest total of any stretch of one or more consecutive days. The list has at least one day, and every day can be a loss.

Asked of

  • Data Analyst
  • Data Engineer
  • Data Scientist
  • ML Engineer
  • AI Engineer

Example 1

Input

daily_changes = [-2, 1, -3, 4, -1, 2, 1, -5, 4]

Output

6

Example 2

Input

daily_changes = [-3, -1, -2]

Output

-1

Explanation

In the first example, the days with changes 4, -1, 2 and 1 add up to 6, and no other stretch does better. In the second example, every day is a loss, so the best stretch is the single day with the smallest loss, -1.

Submit also runs 4 hidden test cases that check edge cases.