38/46

Stack

Algorithms
Easy
10 min
data-structures
stack

Implement a Stack class: a last-in, first-out (LIFO) collection. The most recently added item is always the first one to come back out. An editor's undo history works this way: the last edit you made is the first one undone.

const history = new Stack();
history.push('type "a"'); // 1
history.push('type "b"'); // 2
history.peek();           // 'type "b"'
history.pop();            // 'type "b"'
history.length();         // 1
history.isEmpty();        // false

API

MethodReturnsDescription
push(item)numberAdds item to the top and returns the new length.
pop()item or undefinedRemoves and returns the top item, or undefined if the stack is empty.
peek()item or undefinedReturns the top item without removing it.
isEmpty()booleantrue when there are no items.
length()numberThe number of items.

Constraints

  • Every method must run in O(1) time (amortised for push).
  • Items can be any value, including undefined, null and objects.
  • Separate instances must not share state.

Follow-up

  • How would you add a min() method that also runs in O(1)?
  • How would you cap an undo history at the most recent 100 entries?

Spotted a mistake or a rights concern? Report an issue