Fixed Size Stack

You are part of a development team for a programming teaching platform. As a lesson in exception handling and data structure creation, you've been asked to implement a Stack class in Python. This Stack has a fixed size and supports three operations:
  • push(item): Add an item to the top of the stack.
  • pop(): Remove the top item of the stack and return it.
  • peek(): Look at the top item of the stack without removing it.
However, your Stack class must have the following constraints:
  • The push(item) operation should raise a StackOverflowError exception if trying to push an item onto a full stack.
  • The pop() operation should raise a StackEmptyError exception if trying to pop an item from an empty stack.
stack = Stack(3); stack.push('A'); stack.push('B'); stack.push('C'); stack.push('D'); print(stack.peek()); print(stack.pop()); print(stack.pop()); print(stack.pop()); print(stack.pop())
StackOverflowError: The stack is full C C B A StackEmptyError: The stack is empty


Time limit: 2.4 seconds

Memory limit: 512 MB

Output limit: 1 MB

To check your solution you need to sign in