Interview Cheat Sheets | Stacks Cheat Sheet

December 28th, 2023

Introduction to Stacks

This article will serve to further summarize the excellent overview of handling stacks in coding interviews from the tech interview handbook.

Overview on Stacks

A stack, an abstract data type, facilitates push and pop operations, adhering to the Last In, First Out (LIFO) principle. Being an abstract data type, stacks can be implemented using arrays or singly linked lists. They are crucial for nested function calls and depth-first search, where depth-first search algorithms can be implemented recursively or iteratively with a stack.

Implementations

LanguageAPI
C++std::stack
Javajava.util.Stack
PythonSimulated using List
JavaScriptSimulated using Array

Time Complexity

OperationBig-O
Top/PeekO(1)
PushO(1)
PopO(1)
isEmptyO(1)
SearchO(n)

Corner Cases

  • Empty stack: Popping from an empty stack
  • Stack with one item
  • Stack with two items

Essential Questions

  1. Valid Parentheses - My solution
  2. Implement Queue using Stacks - My solution
  • Implement Stack using Queues
  • Min Stack - My solution
  • Asteroid Collision
  • Evaluate Reverse Polish Notation - My solution
  • Basic Calculator
  • Basic Calculator II
  • Daily Temperatures
  • Trapping Rain Water
  • Largest Rectangle in Histogram