FastPrepGeneric Stack Operation Sequence
Problems/Generic Stack Operation Sequence

Generic Stack Operation Sequence

Arista Networks logoArista Networks● MediumNEW GRADPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in an Arista Networks new grad phone screen. The report asks for a generic C++ stack. It follows up on memory allocation, placement construction, the return type of top, and why a reference kept after pop is no longer valid. Our version takes a sequence of operations. It keeps the stack changes and the point where an item is destroyed, in a form that works the same everywhere. It does not try to show C++ undefined behavior. Our version is about 78% the same as the reported question.

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: push value and output OK.
  • TOP: output the current top value, or EMPTY when the stack is empty.
  • POP: remove and output the current top value, or output EMPTY when the stack is empty.
  • SIZE: output the current size as a decimal string.
  • EMPTY: output true when the stack is empty and false otherwise.

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, or PUSH: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, or false.

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
All Arista Networks interview questionsSee Arista Networks hiring insights
Saved
class Solution {
    public String[] simulateGenericStack(String[] operations) {
      // write your code here
    }
}
Checking account…