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?- A. -4 1
- B. 5 0
- C. 5 1
- 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
- Problem: A warehouse aisle has shelves with weights; find the leftmost 'balance shelf' i such that the total weight strictly to its left…
- Problem: A street has n = 6 lamps, all starting at brightness 0. Each command (l, r, v) adds v to every lamp from l to r (0-based…
- Problem: A 3 × 4 grid of crop yields must answer 'total yield in rectangle (r1, c1)–(r2, c2)' in O(1) using a 2-D prefix table with one…
- Problem: Find the total of the sums of ALL contiguous subarrays without enumerating them: element a[i] lies in (i + 1)·(n − i) subarrays…
- Problem: A login service flags an account if the same device ID appears twice within at most k positions of the access log (|i − j| ≤ k)…
- Problem: A DP table for a 2 × 3 grid is created in two ways and one cell is set in each. python dp = [[0] 3] 2 dp[0][1] = 5 dp2 = [[0] 3…