Data Structures & AlgorithmsEasyTCS NQT Digital · Product Company SDE

Problem: A wallet starts with a positive balance s and then applies the day's changes in order. Find the minimum s such that the running balance never drops below 1.

Problem: A wallet starts with a positive balance s and then applies the day's changes in order. Find the minimum s such that the running balance never drops below 1.
def min_start(changes):
    run = low = 0
    for c in changes:
        run += c
        low = min(low, run)
    return 1 - low
print(min_start([-3, 2, -3, 4, 2]), min_start([1, 2]))
What is printed?
  1. A. -4 1
  2. B. 5 0
  3. C. 5 1
  4. D. 4 1

Correct answer: C. 5 1

Track the lowest prefix sum. The running sums are −3, −1, −4, 0, 2, so the minimum is −4 and the start must satisfy s − 4 ≥ 1 → s = 5. If every prefix is non-negative (low stays 0), the start must still be positive: 1 − 0 = 1. Returning −low (4) forgets the 'at least 1' condition. O(n).

More Coding Interview Problems questions

Exams that ask this topic