Problem · Design

Parcel Event Tracking

Learn this problem
MediumAirbnb logoAirbnbFULLTIMEOA

Problem statement

Process a finite ordered sequence of operations for a simplified parcel-tracking system. Maintain parcels by string parcelId. Within each parcel, maintain a running signed 64-bit count for each string eventType.

Each row in operations is one of:

  • ["RECORD", parcelId, eventType, count]: create the event-type counter if absent, add the positive decimal integer count, and return the new running total.
  • ["GET", parcelId, eventType]: return the current total, or null if either the parcel or event type is absent.
  • ["REMOVE", parcelId, eventType]: remove that event-type counter and return true if it existed; otherwise return false. After a successful removal, delete the parcel itself when it has no remaining event types.

Return a canonical JSON array string containing one result per operation in order. Integer totals are JSON numbers, missing queries are null, and removal results are JSON booleans. The returned string contains no spaces.

Only Level 1 is in scope. Parcel ranking, courier assignment, courier-specific events, undo, and courier sign-out are not part of this contract because the source images do not provide their rules.

Function

processParcelEvents(operations: String[][]) → String

Examples

Example 1

operations = [["RECORD","A","B","5"],["RECORD","A","B","6"],["GET","A","B"],["GET","A","C"],["REMOVE","A","B"],["REMOVE","A","B"]]return = "[5,11,11,null,true,false]"

The first two records create then increment counter (A, B) to 11. Querying that counter returns 11, while event type C is absent. The first removal succeeds and deletes the now-empty parcel; repeating it returns false.

Example 2

operations = [["RECORD","P1","loaded","3"],["RECORD","P1","scanned","2"],["REMOVE","P1","loaded"],["GET","P1","scanned"],["GET","P1","loaded"]]return = "[3,2,true,2,null]"

Removing loaded does not delete parcel P1 because its scanned counter remains. The removed event type is subsequently absent.

Example 3

operations = [["GET","missing","x"],["RECORD","missing","x","7"],["REMOVE","missing","x"],["RECORD","missing","x","4"],["GET","missing","x"]]return = "[null,7,true,4,4]"

The first query sees no parcel. A successful removal deletes the only event type and therefore the parcel; recording the same identifiers afterward creates a fresh counter starting at 4.

Constraints

  • 1 <= operations.length <= 2 * 10^5.
  • Every operation has exactly the name and number of fields described above.
  • 1 <= parcelId.length, eventType.length <= 100.
  • Identifiers contain printable ASCII characters.
  • Every RECORD count is a decimal integer from 1 through 10^9.
  • Every running event count fits in a signed 64-bit integer.

More Airbnb problems

drafts saved locally
public String processParcelEvents(String[][] operations) {
    // Write your code here.
}
operations[["RECORD","A","B","5"],["RECORD","A","B","6"],["GET","A","B"],["GET","A","C"],["REMOVE","A","B"],["REMOVE","A","B"]]
expected"[5,11,11,null,true,false]"
checking account