AO
Back

Maximum Money in K Consecutive Bags (Sparse Segments)

Also reported as Maximum Coins From K Consecutive Bags (LeetCode 3413)

OAAsync OALast reported October 2026Medium Frequency

Problem Overview

The round: the coding half of Amazon's online assessment (40 minutes in two reports), next to an AI-assisted debugging task. Six candidates reported it between May and the end of September 2026, and a round-up of June–September Amazon reports calls it the OA question it saw most often. Two candidates posted the prompt; one of them includes the constraints and the function signature. One candidate and a commenter on another report compare it to LeetCode 3413, Maximum Coins From K Consecutive Bags. Examples and constraints below are as reported; the prose is paraphrased.

Problem

Bags stand in a line, numbered from 1 to infinity. segment is an n × 3 array: every bag from segment[i][0] to segment[i][1] (both ends inclusive) holds segment[i][2] money. Segments never intersect, and a bag outside every segment holds 0. Choose k consecutive bags with the most money in total and return that total modulo 10^9 + 7.

def calculateMaximumConsecutiveSum(k: int, segment: list[list[int]]) -> int: ...

Constraints

  • 1 ≤ n ≤ 2 * 10^5
  • 1 ≤ k ≤ 10^9
  • 1 ≤ segment[i][0] ≤ segment[i][1] ≤ 10^9
  • 1 ≤ segment[i][2] ≤ 10^6
  • No two segments intersect.

Example

k = 5
segment = [[1, 4, 2], [6, 6, 5], [7, 7, 7], [9, 10, 1]]

Bag    1  2  3  4  5  6  7  8  9  10
Money  2  2  2  2  0  5  7  0  1  1

Bags 1-5: 8    2-6: 11    3-7: 16    4-8: 14    5-9: 13    6-10: 14
Output: 16

What the reports stress: bag numbers go up to 10^9, so you cannot build an array over every bag. One candidate's prefix-sum array passed 4 tests and hit memory errors on the hidden ones. The best window can cut a segment partway, at either end.

What Reports Emphasize

What candidates report: It is LeetCode 3413 (Maximum Coins From K Consecutive Bags) with an Amazon story.; Bag numbers reach 10^9: one candidate's prefix-sum array over every bag passed 4 tests and ran out of memory on the hidden ones.; The window can cut a segment partway; one candidate warns about operator precedence in that product.; One candidate's outline: compress the coordinates to the segment ends, then prefix sums with binary search, or a sliding window.

Outcomes: The candidate whose prefix-sum array ran out of memory was rejected two days later.

Our note (standard LeetCode 3413 advice): Try the windows that start at a segment's left end and the windows that end at a segment's right end. Compare true totals and take the modulo once, at the end.

Practice

Write your own against 7 test cases, or read the worked solution — approach, complexity, and code that runs.

Prepping for Amazon with friends?

Send them this page. It is free to read, no account needed.

More Amazon Questions

Free preview

Every question in the Amazon catalog gets this depth

What you just read — canonical solution, follow-up arc, what passing candidates actually did — exists for all 57 Amazon questions, refreshed monthly from new candidate reports.

$59/mo — or $50/mo with the 3-month pass · cancel anytime
Amazon · OA · Reported 6× across candidate reports
Is this helpful?