FastPrepDMA Stream Distribution

DMA Stream Distribution

Etched logoEtched● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

A DMA controller receives ordered write streams and fills ordered memory regions. Stream i contains streamBytes[i] bytes, and region j has regionCapacities[j] available bytes.

Consume each stream fully before moving to the next stream, and fill each region before moving to the next region. A stream may span regions and a region may contain bytes from several streams. Return one row [streamIndex, regionIndex, bytesMoved] for every non-empty transfer. Total region capacity is sufficient for all streams.

Function

distributeDmaStreams(streamBytes: int[], regionCapacities: int[]) → int[][]

Examples

Example 1

streamBytes = [8,4]regionCapacities = [5,5,2]return = [[0,0,5],[0,1,3],[1,1,2],[1,2,2]]

The first stream fills region 0 and uses three bytes of region 1; the second finishes region 1 and fills region 2.

Example 2

streamBytes = [3,2]regionCapacities = [10]return = [[0,0,3],[1,0,2]]

Both streams fit sequentially in the same region.

Constraints

  • 0 <= streamBytes.length, regionCapacities.length <= 100000
  • Every byte count and capacity is positive.
  • Total region capacity is at least total stream bytes.

More Etched problems

See Etched hiring insights
public int[][] distributeDmaStreams(int[] streamBytes, int[] regionCapacities) {
  // Write your code here.
}
streamBytes[8,4]
regionCapacities[5,5,2]
expected[[0,0,5],[0,1,3],[1,1,2],[1,2,2]]
Checking account…