Summer sale! Get 10% off annual plan with the code SUMMERSALE23, grab your discount today! Check out other promotions

Stack

Author
Yangshun Tay
Yangshun TayEx-Meta Staff Engineer
Languages
JSTS
Difficulty
Easy
Recommended duration to spend during interviews
10 mins
Users completed

Implement a stack data structure in JavaScript that contains the following operations:

  • new Stack(): Creates an instance of a Stack class that doesn't contain any items. The constructor not accept any arguments.
  • push(): Pushes an item onto the top of the stack. Required time complexity: O(1).
  • pop(): Removes an item at the top of the stack and returns that item. Required time complexity: O(1).
  • isEmpty(): Determines if the stack is empty. Required time complexity: O(1).
  • peek(): Returns the item at the top of the stack without removing it from the stack. Required time complexity: O(1).
  • length(): Returns the number of items in the stack. Required time complexity: O(1).

Examples

const stack = new Stack();
stack.isEmpty(); // true
stack.push(1);
stack.push(2);
stack.length(); // 2
stack.push(3);
stack.peek(); // 3
stack.pop(); // 3
stack.isEmpty(); // false

Try these questions next

Similar Questions

Loading editor