Trace Nested Object Destruction Order
Problem statement
Simulate automatic object lifetimes in nested scopes. Operations are enter, create:name, or exit. A created object belongs to the current scope. Exiting destroys that scope's objects in reverse creation order.
After all operations, destroy remaining scopes from innermost to outermost. Return object names in destruction order.
Function
destructionOrder(operations: String[]) → String[]Examples
Example 1
operations = ["create:a","enter","create:b","create:c","exit"]return = ["c","b","a"]The inner scope closes first, then the outer scope closes at the end.
Example 2
operations = ["enter","exit"]return = []Empty scopes destroy nothing.
Constraints
0 <= operations.length <= 100000.- Names are nonempty ASCII strings without a colon.
- Every explicit
exithas a matching active scope.
Source note: Author-written prompt excerpt retained for source fidelity.