FastPrepTrack Package Receipt Gaps

Track Package Receipt Gaps

Amazon logoAmazon● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

Process a finite sequence of package-receipt operations. Each row of operations has one of these forms:

  • ["receive", packageId] records the nonnegative package ID written as a decimal string. Receiving the same ID again has no additional effect.
  • ["report"] produces one snapshot of the received IDs.

For a report, let maxReceived be the largest received ID, or -1 when no package has been received. Let prefixEnd be the largest value such that every ID from 0 through prefixEnd has been received. If ID 0 has not been received, prefixEnd is -1.

Encode each report as [prefixEnd, maxReceived, missingId1, missingId2, ...], where the missing IDs are every absent value from 0 through maxReceived in increasing order. Thus, an empty state produces [-1, -1]. Return the report rows in operation order.

Function

packageReceiptReports(operations: String[][]) → int[][]

Examples

Example 1

operations = [["receive","2"],["receive","0"],["report"],["receive","1"],["report"]]return = [[0,2,1],[2,2]]

At the first report, IDs 0 and 2 have arrived. The contiguous prefix ends at 0, the maximum is 2, and ID 1 is missing. After receiving ID 1, the complete prefix ends at 2 and there are no gaps.

Example 2

operations = [["report"],["receive","3"],["receive","3"],["report"],["receive","0"],["report"]]return = [[-1,-1],[-1,3,0,1,2],[0,3,1,2]]

The first report observes no packages. Receiving ID 3 twice records it once. After ID 0 arrives, the contiguous prefix advances to 0, while IDs 1 and 2 remain missing.

Constraints

  • 1 <= operations.length <= 100000.
  • Every operation is either ["receive", packageId] or ["report"].
  • Every package ID is a canonical decimal string for an integer in [0, 200000].
  • The total number of integers across all returned report rows is at most 200000.
  • Operations are applied strictly from left to right.

More Amazon problems

See Amazon hiring insights
public int[][] packageReceiptReports(String[][] operations) {
    // Write your code here.
}
operations[["receive","2"],["receive","0"],["report"],["receive","1"],["report"]]
expected[[0,2,1],[2,2]]
Checking account…