Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Stack Implementation

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Stack Implementation. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

A 24]7.ai conversation workflow needs a last-in, first-out history of event identifiers so the most recent event can be inspected or removed first. Implement the stack behavior for a sequence of operations.

Create a function that processes operations in order:

  1. push(value) adds value to the top of the stack and produces no output.
  2. pop() removes and returns the top value.
  3. top() returns the top value without removing it.

The input is represented as a list of operation arrays. A push operation has the form ["push", value]; pop and top operations have the form ["pop"] and ["top"]. Return a list containing results from pop and top operations in their original order. Do not include output for push operations.

Formal Specification

Given operations, a list of operation arrays containing integer values, return a list of integers produced by pop and top. The input guarantees that pop and top are never called when the stack is empty.

Constraints

  • 1 <= len(operations) <= 10^5
  • Values are integers in [-10^9, 10^9]
  • Every operation is valid
  • The operation name is one of push, pop, or top

Function Signature

def process_stack(operations):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output