Problem · Hash Table

Simulate Unique Ownership Operations

Learn this problem
MediumChicago Trading Company logoChicago Trading CompanyFULLTIMEPHONE SCREEN

Problem statement

Execute an ordered batch of operations on named move-only owners. Each owner is either empty or exclusively owns one positive integer resource ID.

Each row in operations has one of these forms:

  • ["construct", handle, value] or ["reset", handle, value]: replace the handle's current resource with value. The value is a positive decimal integer or "null" for empty.
  • ["move", target, source]: discard the target's current resource, transfer the source resource to the target, and leave the source empty.
  • ["get", handle]: observe the resource without changing ownership.
  • ["release", handle]: return the resource and leave the handle empty.
  • ["empty", handle]: report whether the handle is empty.

Append one result per operation. For construct, reset, move, get, and release, append the resulting or returned resource as a decimal string, or "null" when empty. For empty, append "true" or "false".

Inputs never assign one non-null resource to two owners at the same time. Handles that have not appeared before start empty.

Function

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

Examples

Example 1

operations = [["construct","a","7"],["move","b","a"],["get","a"],["get","b"],["empty","a"]]return = ["7","7","null","7","true"]

The move transfers resource 7 to b and leaves a empty.

Example 2

operations = [["construct","left","5"],["construct","right","9"],["move","right","left"],["release","right"],["empty","right"],["reset","right","12"]]return = ["5","9","5","5","true","12"]

Moving into right discards its old resource 9, transfers 5, and empties left. Release then returns 5 without retaining ownership.

Example 3

operations = [["construct","x","null"],["get","x"],["reset","x","3"],["reset","x","null"],["empty","x"]]return = ["null","null","3","null","true"]

An owner may start empty, acquire a resource through reset, and later reset back to empty.

Constraints

  • 1 <= operations.length <= 100000
  • Every operation has the exact arity described above and every handle is a non-empty ASCII identifier of at most 40 characters.
  • Resource IDs are decimal integers in [1, 2147483647]; only constructor and reset values may be "null".
  • For a move, target and source are distinct. Every non-null constructor or reset value is not owned by a different handle at that moment.
drafts saved locally
public String[] runUniqueOwnership(String[][] operations) {
    // Write your code here.
}
operations[["construct","a","7"],["move","b","a"],["get","a"],["get","b"],["empty","a"]]
expected["7", "7", "null", "7", "true"]
checking account