Generic Stack Operation Sequence
Updated
Problem statement
Given operations, run each command in order on a stack of strings that you implement, and return one output per command. The stack starts empty.
PUSH:value: pushvalueand outputOK.TOP: output the current top value, orEMPTYwhen the stack is empty.POP: remove and output the current top value, or outputEMPTYwhen the stack is empty.SIZE: output the current size as a decimal string.EMPTY: outputtruewhen the stack is empty andfalseotherwise.
A popped item is gone at once. No later command may read it, not even through storage your stack still holds or an old reference to it. PUSH should take amortized constant time, and every other command constant time.
Function
simulateGenericStack(operations: String[]) → String[]Examples
Example 1
operations = ["PUSH:alpha","PUSH:beta","TOP","SIZE","POP","TOP","EMPTY"]return = ["OK","OK","beta","2","beta","alpha","false"]Each push outputs OK, TOP outputs beta, and SIZE counts alpha and beta. POP removes and outputs beta, so TOP then outputs alpha and the EMPTY command outputs false.
Example 2
operations = ["TOP","POP","EMPTY","PUSH:x","POP","EMPTY"]return = ["EMPTY","EMPTY","true","OK","x","true"]TOP and POP on the empty stack output EMPTY, and the EMPTY command outputs true. Pushing and popping x outputs OK and x, so the stack is empty again and the last output is true.
Example 3
operations = ["PUSH:a","PUSH:b","PUSH:c","POP","PUSH:d","TOP"]return = ["OK","OK","OK","c","OK","d"]Each push outputs OK, and POP removes and outputs c. Pushing d makes it the new top, so TOP outputs d.
Constraints
1 <= operations.length <= 100000- Each command is exactly
TOP,POP,SIZE,EMPTY, orPUSH:value. - Each pushed value contains 1 to 32 ASCII letters, digits, underscores, or hyphens.
- Pushed values are not equal to the reserved outputs
OK,EMPTY,true, orfalse.
More Arista Networks interview questions
- First Missing Number in a Consecutive ArrayOA · PHONE SCREEN · Seen Oct 2026
- Remove All Repeated Values from a Linked ListPHONE SCREEN · Seen Oct 2026
- Trace Nested Object Destruction OrderPHONE SCREEN · Seen Oct 2026
- Trace Pointer Rebinding and Pointee UpdatesPHONE SCREEN · Seen Oct 2026
- Swap Adjacent NibblesPHONE SCREEN · Seen Aug 2026
- Unique Target-Sum Pairs in a Sorted Doubly Linked ListPHONE SCREEN · Seen Aug 2026
- API Bandwidth AllocationOA · Seen Sep 2026
- LFU CacheOA · Seen Sep 2026
- Maximum Server Processing TimeOA · Seen Aug 2026
- Minimum Adjacent Merges to Form a PalindromeOA · Seen Aug 2026
Similar questions at other companies
- Asteroid CollisionSiemens · PHONE SCREEN
- Billing Log with Undo and RedoReddit · PHONE SCREENReddit interview questions
- Compress Editor Actions into Operational Transform OperationsReplit · OA · PHONE SCREEN · ONSITE INTERVIEW
- Stack with Constant-Time Middle QueriesAmazon · PHONE SCREEN
- Recover the Stack After a WorkerCitadel · PHONE SCREENCitadel interview questions
- Confirmed Profiling Events with First-Observed StartsAnthropic · PHONE SCREEN · ONSITE INTERVIEWAnthropic interview questions
- Fountain SafetyGoogle · ONSITE INTERVIEWGoogle interview questions
- Asteroid CollisionMicrosoft · OAMicrosoft interview questions
- Surviving Balls after Directional CollisionsSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Asteroid CollisionAdobe · ONSITE INTERVIEWAdobe interview questions