Stacks are a last-in-first-out data structure, typically implemented with arrays
The stack concept is useful for tracking ordered data that we may need to reverse, e.g. text edits
A stack (think stack of plates) is an array-like data structure that supports adding and removing only from the end, aka "top" of the stack. In practice we will use arrays to represent stacks, but what's important is the pattern of only adding and removing from the end of an array to model and solve certain types of problems.
The stack concept powers many features we use daily, including:
Undo and redo in text editors (adding new edits to top of stack and removing them on undo)
Parentheses-matching in code editors (adding new opening parens to top of stack, removing them on closing paren and highlighting the pair with the same colour)
Browser history (new pages get added to a stack, clicking back button pops them off the stack)
Because we only ever add or remove from the end of the stack, all stack operations run in O(1)
time complexity.
Please use JavaScript arrays to perform stack operations.
After attempting each problem, find solutions in the Leaderboard tab (HackerRank, may be on left side of page) or Solution or Discuss tabs (LeetCode) on that problem's page. If you get stuck for more than 15 minutes, review and understand the solutions and move on. Come back and re-attempt the problem after a few days.
Remove All Adjacent Duplicates in String (LeetCode)
Rocket strategy video (Python)
Make The String Great (LeetCode)
Rocket strategy video (Python)
Backspace String Compare (LeetCode)
Crawler Log Folder (LeetCode)
Rocket solution video (Python)
Maximum Element (HackerRank)
Valid Parentheses (LeetCode, HackerRank)
Rocket solution code (Python)
Minimum Add To Make Parentheses Valid (LeetCode)
Simple Text Editor (HackerRank)
Validate Stack Sequences (LeetCode)
Score of Parentheses (LeetCode)
Hint: The depth of the stack can be the power of 2
Daily Temperatures (LeetCode)
Hint: Store temperatures and their array indexes in a stack as we iterate through the input array, and pop those temperatures off the stack and populate "num days until warmer temperature" in answer array when we see a warmer temperature.
This video is slightly more detailed introduction to stacks than the video above
FTBC3 class video introducing stacks