← Problems95. Batch rows under a byte budgetMediumPython
00:00 / 20:00

Batch rows under a byte budget

MediumNot attempted yetAsked at Adobe, PayPal

The downstream API takes a request of at most budget bytes. Rows have to go out in order, packed into as few requests as possible.

Input

  • rows — a list of (row_id, size_bytes), in the order they must be sent
  • budget — int, the most a single batch may carry

Output

A list of (batch_index, row_id) — the batch each row was placed in, numbered from 0 in the order batches are opened. Order is not graded.

Example

rows [(1, 40), (2, 40), (3, 40)] budget 100 returns [(0, 1), (0, 2), (1, 3)]

Constraints

  • Rows keep their order. This is packing, not sorting — reordering to fill a batch better would reorder the data downstream.
  • A batch may reach the budget exactly. Closing it at that point costs a whole extra request every time the arithmetic is round.
  • A row larger than the budget cannot be made to fit. It goes out on its own, and does not drag the row after it over the limit.

Example 1three rows, two batches

Input · rows
[[1, 40], [2, 40], [3, 40]]
Input · budget
100
Output
[[0, 1], [0, 2], [1, 3]]

Example 2an exact fit is still one batch

Input · rows
[[1, 60], [2, 40], [3, 10]]
Input · budget
100
Output
[[0, 1], [0, 2], [1, 3]]

Topics

aggregation

Similar problems

Community-reported interview topic. Not an official company question and no affiliation is implied.

solution.py
Loading editor…
Draft not saved yet · Spaces 4 · UTF-8⌘↵ run ⌘⇧↵ submit
Nothing run yet

Run against the public tests, or submit to score against all of them.