You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
3.3 KiB
3.3 KiB
id | title | description | keywords | sidebar_label | toc_max_heading_level | ||||||
---|---|---|---|---|---|---|---|---|---|---|---|
stack | Stack cheatsheet for coding interviews | Stack study guide for coding interviews, including practice questions, techniques, time complexity, and recommended resources |
|
Stack | 2 |
Introduction
A a stack is an abstract data type that supports the operations push (insert a new element on the top of the stack) and pop (remove and return the most recently added element, the element at the top of the stack). As an abstract data type, stacks can be implemented using arrays or singly linked lists.
This behavior is commonly called LIFO (last in, first out). The name "stack" for this type of structure comes from the analogy to a set of physical items stacked on top of each other.
Stacks are an important way of supporting nested or recursive function calls and is used to implement depth-first search. Depth-first search can be implemented using recursion or a manual stack.
Learning resources
- Readings
- Stacks and Overflows, basecs
- Videos
- Stacks, University of California San Diego
Implementations
Language | API |
---|---|
C++ | std::stack |
Java | java.util.Stack |
Python | Simulated using List |
JavaScript | Simulated using Array |
Time complexity
Operation | Big-O |
---|---|
Top/Peek | O(1) |
Push | O(1) |
Pop | O(1) |
isEmpty | O(1) |
Search | O(n) |
Corner cases
- Empty stack. Popping from an empty stack
- Stack with one item
- Stack with two items
Recommended questions
- Valid Parentheses
- Implement Queue using Stacks
- Implement Stack using Queues
- Min Stack
- Asteroid Collision
- Evaluate Reverse Polish Notation
- Basic Calculator
- Basic Calculator II
- Daily Temperatures
- Trapping Rain Water
- Largest Rectangle in Histogram
Recommended courses
import AlgorithmCourses from '../_courses/AlgorithmCourses.md'