Strong and Weak Reference Lifecycle
Learn this problemProblem statement
Simulate strong and weak ownership handles backed by shared control blocks. Handle names are unique strings. Process operations in order:
["CREATE", strong]creates a new live payload and one strong handle.["CLONE_STRONG", source, destination]creates another strong handle to the source's live block.["MAKE_WEAK", source, destination]creates a weak handle to the source strong handle's block.["LOCK_WEAK", weak, destination]creates a strong destination only when the payload is live. It creates no handle when the payload is expired.["RELEASE", handle]removes one existing strong or weak handle.["QUERY", handle]observes the handle's block without changing it.
A payload is LIVE while its strong count is positive and becomes EXPIRED exactly when that count reaches zero. Its control block remains while any weak handle exists and is REMOVED when both counts are zero.
Return one string after every operation, describing the affected block after the operation as strong:weak:LIVE or strong:weak:EXPIRED. When a release removes the control block, return 0:0:REMOVED. A failed weak lock returns the unchanged expired state.
Function
simulateReferenceControlBlocks(operations: String[][]) → String[]Examples
Example 1
operations = [["CREATE","s"],["MAKE_WEAK","s","w"],["CLONE_STRONG","s","s2"],["RELEASE","s"],["RELEASE","s2"],["LOCK_WEAK","w","late"],["QUERY","w"],["RELEASE","w"]]return = ["1:0:LIVE","1:1:LIVE","2:1:LIVE","1:1:LIVE","0:1:EXPIRED","0:1:EXPIRED","0:1:EXPIRED","0:0:REMOVED"]The payload expires at the last strong release; the weak lock then fails without increasing the count.
Example 2
operations = [["CREATE","owner"],["MAKE_WEAK","owner","observer"],["LOCK_WEAK","observer","locked"],["RELEASE","owner"],["QUERY","locked"],["RELEASE","locked"],["RELEASE","observer"]]return = ["1:0:LIVE","1:1:LIVE","2:1:LIVE","1:1:LIVE","1:1:LIVE","0:1:EXPIRED","0:0:REMOVED"]Locking while live creates a new strong owner that keeps the payload alive.
Example 3
operations = [["CREATE","a"],["CREATE","b"],["MAKE_WEAK","a","wa"],["RELEASE","a"],["QUERY","b"],["RELEASE","wa"],["RELEASE","b"]]return = ["1:0:LIVE","1:0:LIVE","1:1:LIVE","0:1:EXPIRED","1:0:LIVE","0:0:REMOVED","0:0:REMOVED"]Counts and lifetimes are isolated per control block.
Constraints
1 <= operations.length <= 2000.- Every source or queried handle exists and has the required kind.
- Every destination handle name is fresh; a failed weak lock leaves it unused.
- Every release names an existing handle.
- Handle names contain from
1through30letters or digits.