← Questions

Minimum range boosts to make server power non-decreasing

0
AmazonCodingLast asked Mar 2026

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 integer x, then add x to 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^5
  • 1 <= 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 x values only, not the number of machines in each range?
  • Are only increases allowed?
  • Should n = 0 and n = 1 return 0?
  • 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 x on [l, r] raises the difference at l by x and lowers it at r + 1. Ranges that run to the end lower nothing.
  • Every drop power[i - 1] - power[i] > 0 must 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 x times 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

arrays · greedy · difference array

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding