Minimum range boosts to make server power non-decreasing
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Amazon's online assessment, last asked Mar 2026. It tests arrays, greedy and difference array.
You manage a row of n machines. Machine i has an integer capacity power[i]. You want the capacities to be non-decreasing from left to right (power[i] <= power[i+1] for every i).
You may apply the following operation any number of times:
- Pick a contiguous range
[l, r]and a positive integerx, then addxto every machine in that range.
The cost of a plan is the sum of the x values across all operations. The number of operations does not matter. Return the minimum possible cost.
Implement:
def min_increase_sum(power: list[int]) -> int: ...
Constraints
0 <= n <= 10^51 <= power[i] <= 10^9- The answer may exceed 32 bits.
Example 1
power = [3, 1, 2]
output: 2
Add 2 to the range [1, 2], giving [3, 3, 4]. Raising each element to the running maximum would cost 3. A single range operation does better.
Example 2
power = [5, 2, 4, 1, 6]
output: 6
Add 3 to [1, 4] to get [5, 5, 7, 4, 9]. Then add 3 to [3, 4] to get [5, 5, 7, 7, 12].
Be ready to explain why no cheaper plan exists, and to return the operations themselves.
Clarifying questions
- Does the cost count the sum of the
xvalues only, not the number of machines in each range? - Are only increases allowed?
- Should
n = 0andn = 1return0? - Do we need the operations themselves, or only the cost?
What a strong answer covers
- Looks at the differences between neighbours. A range add of
xon[l, r]raises the difference atlbyxand lowers it atr + 1. Ranges that run to the end lower nothing. - Every drop
power[i - 1] - power[i] > 0must be paid by at least that much. Extending each range to the end of the array pays it with no side effects. - So the answer is the sum of all drops. That is O(n) time and O(1) memory, with a 64-bit total.
- Proves the lower bound: one operation raises at most one difference, so no plan can do better.
- Tests a sorted array, a strictly decreasing array, both examples, and equal neighbours.
Follow-ups
- Return one optimal list of operations.
- The cost is
xtimes the length of the range. - Ranges can have length at most
L. - Make the array non-increasing instead, or allow decreases too.
Where it's asked
- Amazon · Online assessment · Last asked Mar 2026Coding problems plus a repository bug-fix section with a built-in assistant or a design scenario, then a work-style survey · AI only inside the bug-fix section's built-in assistant
What it tests
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. New accounts get 5 credits: each one unlocks a question for good.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
Sign in to submit your code; every attempt is kept here.
