GOLDMANSACHS Coding Question – Solved

11 Live
A traveler is traveling from the city of Zeta to Omega. He starts with X amount of money. Every day he spends some money and may also work on certain days to earn money. On some days, he may earn more than he spends, and on other days, he may spend more than he earns. You are given an array of integers, which represents his net savings (earnings - expenses) on any day. Your task is to find out the minimum amount the traveler should begin with to ensure that he always has some money (> 0) at the end of any day. Constraints: - -200 <= a <= 200, where `a` are the elements of the array. - 0 < i <= 100, where `i` is the array length. - X >= 0 Example: Input: 3 // Array length 4 // Array elements start 2 -3 Output: 0 Explanation: Traveler saves $4 on the first day, $2 on the second day, and $-3 on the third day (expenses are more on the third day than earnings). - End of the first day, he has X + $4. - End of the second day, he has X + $(4 + 2). - End of the third day, he has X + $(4 + 2 - 3). So, effectively, the traveler can start with X = $0 to always ensure he has some money at the end of each day.

Asked in: GOLDMANSACHS

Image of the Question

Question Image Question Image

All Testcases Passed ✔



Passcode Image

Solution


def requiredAmountAtStart(netSaving):
   
    def solve(mid):
        for value in netSaving:
// ... rest of solution available after purchase

🔒 Please login to view the solution

Explanation


```
To solve this problem, you should begin by visualizing the journey of the traveler over time as a sequence of cumulative financial states. The given array provides you with the net financial change for each day — which could be positive (a gain), negative (a loss), or zero (no net change). The key is to ensure that after every single day, the traveler’s total money never drops to zero or below.

Start by thinking in terms of a running total or a cumulative sum. Each day, the traveler’s money changes by the value of that day’s entry in the array. If you were to sum these values one by one, day after day, you’d get the cumulative total of net savings up to that point. But since the traveler also starts with an initial amount X, this starting point shifts the entire cumulative sum upward by X.

Now, consider what happens if this cumulative sum ever drops below or equal to zero — that would mean the traveler runs out of money or ends the day with none, which is not allowed. So, the goal is to prevent that from ever happening. That leads us to a crucial idea: what is the worst (i.e., most negative) point the traveler’s cumulative net savings ever reaches? Because that’s the point where the starting money would need to compensate the most.

So, as a first step, think about scanning through the array while tracking a running cumulative sum. As you go, record the minimum value this cumulative sum reaches. Why? Because that value indicates the deepest dip into negative territory the traveler’s finances ever reach without the starting amount X. If the lowest point is, say, -5, that means the traveler would need at least $6 to always stay above zero at the end of that day. This is because X + (-5) > 0 implies X > 5. So, the minimum starting amount in this case would be 6.

Notice the key point here is understanding that we only care about the **lowest** point in the cumulative net total. That's where the traveler would have the least money, and that's the point we have to protect against. Once we identify the lowest cumulative total, we can figure out how much money the traveler needs to start with to ensure that even at this lowest point, he still has more than zero.

Keep in mind also that if the cumulative sum never drops below zero, the traveler never runs into trouble, and hence, he can start with X = 0. That’s the best-case scenario and also a valid output.

Also consider how this problem is a variant of a prefix sum analysis — a common technique where you analyze sums of subarrays or cumulative totals to derive insights about certain properties (like minimums or maximums). In this case, the prefix sum (running total of net changes) helps you detect the point of greatest financial risk.

To summarize, your overall approach should look like this:

1. Start with a cumulative sum of zero.
2. As you go through each day’s net change, update this cumulative sum.
3. At each step, compare the current cumulative sum to a running minimum value.
4. After processing all days, examine the lowest point that cumulative sum reached.
5. Determine the smallest X such that X + (minimum cumulative sum) > 0.
6. Return that X as the answer.

By thinking in terms of tracking the cumulative financial health of the traveler and identifying the worst-case point, you’ll be able to determine the minimal starting funds needed to ensure the traveler always has some money left each day. This avoids brute-force checking of different starting amounts and zeroes in on the actual financial requirement directly driven by the pattern of net savings in the array.
```


Related Questions