Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Implement Stack Operations
00:00
5 left

Implement Stack Operations

EasyPython

Problem

Implement the stack operations in any programming language.

Implement stack_operations(operations), where operations is a list of command lists. Supported commands are push with one value, pop, peek, size, and is_empty. Return a list containing the result of every operation except push. For an empty pop or peek, return None.

Example: [["push", 4], ["peek"], ["pop"]] returns [4, 4]. [["pop"], ["is_empty"]] returns [None, True].

Constraints

  • 0 <= len(operations) <= 10^4
  • Each operation is a list whose first value is one of push, pop, peek, size, or is_empty
  • Each push operation contains exactly one additional value
  • Values may be integers, strings, booleans, or null

Function Signature

def stack_operations(operations):
Interviewer

Your question is Implement Stack Operations. Start with the requirements in the Question tab.

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.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.