Tags

Stack

Stack

1 lesson
15 problems
2 code snippets
2 question banks
9 community items

stack

Data Structures

1 lesson

Stack (LIFO)

Free
Beginner

40 min

2 prereqs

Every time a function calls another function, the language runtime pushes a frame onto an internal stack; every `return` pops one off. That same Last In, First Out discipline shows up in undo buttons, expression evaluators, browser back navigation, and the call patterns of recursion, which is why **Stack (LIFO)** is one of the first abstract data types every engineer learns. This lesson introduces the four core operations of a stack (`push`, `pop`, `peek`, `isEmpty`), the underflow case you must handle on every pop, and two implementations: a dynamic array stack with amortized `O(1)` push, and a linked list stack with strict `O(1)` push at the head. You will trace stack state through bracket matching, postfix evaluation, and string reversal so the LIFO mental model becomes a tool you reach for instinctively when a problem asks you to remember the most recent thing. In **Arrays & Strings**, you saw that appending to the end of a dynamic array is `O(1)` amortized, which is exactly the cost model for an array-backed stack. **Linked Lists (Singly)** showed that head insertion is unconditionally `O(1)`, which is what makes a linked list the cleanest backing store for a stack with a strict worst-case guarantee. Next, **Queue & Deque** flips the discipline: instead of pulling from the same end you pushed to, you pull from the opposite end, and the implementation gets just a little more interesting.

Not Started

0%

Stack
LIFO
Data Structures
Foundations
Push / Pop / Peek
Amortized Analysis
Beginner
Free

Practice Problems

15 problems

Implement Queue using Stacks

Free
Not Started
Easy

Implement a first-in-first-out (FIFO) queue using only two stacks, supporting push, pop, peek, and empty operations.

Stack
Queue
Data Structures
Beginner

1k

15

Next Greater Element I

Free
Not Started
Easy

Given two arrays where nums1 is a subset of nums2, find the next greater element in nums2 for each element in nums1.

Stack
Monotonic Stack
Hash Map / Dictionary
Beginner

896

27

Valid Parentheses

Free
Not Started
Easy

Given a string containing only brackets, determine if the input string has valid (properly matched and nested) parentheses.

Stack
Parentheses Matching
Beginner

176

2

Basic Calculator

Not Started
Hard

Implement a basic calculator to evaluate a string expression containing '+', '-', parentheses, and non-negative integers.

Stack
Stack-Based Parsing
Recursion
Advanced

644

17

Largest Rectangle in Histogram

Not Started
Hard

Given an array of bar heights representing a histogram, find the area of the largest rectangle that can be formed within the histogram.

Stack
Monotonic Stack
Largest Rectangle in Histogram
Advanced

888

23

Maximal Rectangle

Not Started
Hard

Given a 2D binary matrix filled with '0's and '1's, find the area of the largest rectangle containing only '1's.

Stack
Monotonic Stack
Dynamic Programming
Largest Rectangle in Histogram
Advanced

653

17

Binary Search Tree Iterator

Free
Not Started
Medium

Implement an iterator over a BST that returns elements in ascending order (in-order traversal).

Binary Tree
Binary Search Tree (BST)
Stack
Iterator Design
Inorder
Intermediate

701

20

Car Fleet

Not Started
Medium

Given positions and speeds of cars heading toward a target, determine how many car fleets will arrive at the destination.

Stack
Monotonic Stack
Sorting
Intermediate

304

4

Daily Temperatures

Not Started
Medium

Given an array of daily temperatures, return how many days you have to wait for a warmer temperature for each day.

Stack
Monotonic Stack
Next Greater Element
Intermediate

969

14

Evaluate Reverse Polish Notation

Free
Not Started
Medium

Evaluate the value of an arithmetic expression given in Reverse Polish Notation (postfix), where operators follow their operands.

Stack
Stack-Based Parsing
Intermediate

865

19

Generate Parentheses

Free
Not Started
Medium

Given n pairs of parentheses, generate all combinations of well-formed (valid) parentheses.

Stack
Backtracking
Recursion
Parentheses Matching
Intermediate

281

1

Kth Smallest Element in a BST

Free
Not Started
Medium

Given the root of a BST and an integer k, return the kth smallest value (1-indexed) using in-order traversal.

Binary Tree
Binary Search Tree (BST)
DFS
Inorder
Stack
Intermediate

613

18

Min Stack

Free
Not Started
Medium

Design a stack that supports push, pop, top, and retrieving the minimum element, all in constant time.

Stack
Data Structures
Intermediate

534

15

Simplify Path

Not Started
Medium

Given an absolute Unix-style file path, simplify it by resolving '.', '..', and multiple slashes to produce the canonical path.

Stack
Stack-Based Parsing
Strings
Intermediate

659

19

Sum of Subarray Minimums

Not Started
Medium

Given an array of integers, find the sum of the minimum value of every contiguous subarray, modulo 10^9 + 7.

Stack
Monotonic Stack
Dynamic Programming
Intermediate

651

4

Community

9 items
Problem
Medium
Free

Decode String

Expand a run-length-encoded string with arbitrary nesting using two parallel stacks (counts and string fragments).

stack
strings
recursion

667

15

4.6 (12)

Apr 5, 2026

by CodeSnatch

Problem
Easy
Free

Remove Outermost Parentheses

Strip the outer parentheses of every primitive group in a balanced parenthesis string, in one pass with a depth counter.

stack
strings

715

19

Mar 24, 2026

by @carlosherrera

Problem
Medium
Free

Online Stock Span

Design an online structure that returns the streak of consecutive previous days with price <= today, using a monotonic stack of (price, span) pairs.

monotonic-stack
stack
online-algorithms

349

7

Mar 20, 2026

by @nehawood

Article

Stacks and Queues Cheat Sheet

LIFO vs FIFO, the operations that matter, and the language-specific footguns (JS shift, Python list.pop(0), Java Stack vs Deque) that turn O(1) into O(n).

stack
queue
data-structures
fundamentals
interview-prep

610

2

Mar 7, 2026

by @amaragupta

Problem
Easy
Free

Backspace String Compare

Compare two strings after applying backspace edits, with the catch that the O(1) space version uses two pointers walking from the end.

stack
two-pointers
strings

188

5

4.5 (13)

Feb 3, 2026

by @oliviafoster

Article

Monotonic Stack: The Pattern Everyone Skips

The next-greater-element trick that turns O(n^2) scans into O(n). Daily temperatures, largest rectangle in histogram, and trapping rain water, all from the same shape.

monotonic-stack
stack
algorithms
array-manipulation-patterns
interview-prep

655

17

4.3 (12)

Jan 15, 2026

by @alexsaeed

Problem
Medium
Free

Flatten a Multilevel Doubly Linked List

Flatten a doubly-linked list whose nodes may have a child pointer to another doubly-linked list, producing a single-level list via DFS or an explicit stack.

linked-list
stack
recursion

493

8

4.5 (13)

Jan 11, 2026

by @vikramokoro

Problem
Medium
Free

Min Add to Make Parentheses Valid

Count the minimum number of '(' and ')' insertions that turn a parenthesis string valid using a single-pass two-counter scan.

stack
greedy
strings

1.2k

27

4.6 (10)

Jan 3, 2026

by @kavyachakraborty

Problem
Medium
Free

Asteroid Collision

Simulate collisions between right- and left-moving asteroids using a stack of survivors, popping while the top loses head-on encounters.

stack
simulation
arrays

1.1k

20

4.4 (11)

Dec 2, 2025

by @jamalvargas