FastPrepTrace Nested Object Destruction Order

Trace Nested Object Destruction Order

Arista Networks logoArista Networks● MediumFULLTIMEPHONE SCREEN
Learn

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 exit has a matching active scope.

Source note: Author-written prompt excerpt retained for source fidelity.

More Arista Networks problems

See Arista Networks hiring insights
public String[] destructionOrder(String[] operations) {
    // Write your code here.
}
operations["create:a","enter","create:b","create:c","exit"]
expected["c", "b", "a"]
Checking account…