Stacks & Queues — Complete Guide: All Patterns & Problem Index

Sanjeev SharmaSanjeev Sharma
5 min read

Advertisement

Stacks & Queues — Complete Pattern Guide

Problems 251–300 · 50 posts · Easy × 10, Medium × 30, Hard × 10


Core Patterns

#PatternKey IdeaProblems
1Monotonic Stack (decreasing)Pop when current > topNext Greater Element, Daily Temperatures
2Monotonic Stack (increasing)Pop when current < topLargest Rectangle in Histogram
3BFS / Level-OrderQueue + while queueBinary Tree Level Order, Rotting Oranges
4Two-Stack TricksSimulate queue / min-stackMin Stack, Queue via Stacks
5Bracket MatchingPush open, pop on closeValid Parentheses, Decode String
6Expression EvaluationOperator + operand stacksBasic Calculator, Polish Notation
7Deque / Sliding Window MaxMonotonic deque, pop front+backSliding Window Maximum

Templates

Monotonic Decreasing Stack (Next Greater)

stack = []  # indices
res = [0] * n
for i in range(n):
    while stack and nums[stack[-1]] < nums[i]:
        res[stack.pop()] = nums[i]
    stack.append(i)

Monotonic Increasing Stack (Previous Smaller)

stack = []
for i in range(n):
    while stack and nums[stack[-1]] >= nums[i]:
        stack.pop()
    left[i] = stack[-1] if stack else -1
    stack.append(i)

BFS Template

from collections import deque
queue = deque([start])
visited = {start}
while queue:
    node = queue.popleft()
    for neighbor in get_neighbors(node):
        if neighbor not in visited:
            visited.add(neighbor)
            queue.append(neighbor)

Min Stack

class MinStack:
    def __init__(self): self.stack = []; self.min_stack = []
    def push(self, val):
        self.stack.append(val)
        self.min_stack.append(min(val, self.min_stack[-1] if self.min_stack else val))
    def pop(self): self.stack.pop(); self.min_stack.pop()
    def top(self): return self.stack[-1]
    def getMin(self): return self.min_stack[-1]

Sliding Window Maximum (Deque)

from collections import deque
dq = deque()  # stores indices, front is max
res = []
for i in range(n):
    while dq and dq[0] < i - k + 1: dq.popleft()  # remove out-of-window
    while dq and nums[dq[-1]] < nums[i]: dq.pop()  # remove smaller
    dq.append(i)
    if i >= k - 1: res.append(nums[dq[0]])

Problem Index — Stacks & Queues

Easy (251–260)

#ProblemPattern
251Valid ParenthesesBracket Matching
252Implement Stack using QueuesDesign
253Implement Queue using StacksTwo-Stack
254Min StackTwo-Stack / Min Tracking
255Baseball GameStack Simulation
256Backspace String CompareStack / Two Pointer
257Crawler Log FolderStack
258Number of Recent CallsQueue
259Make the String GreatStack
260Next Warmer Temperature (Easy)Monotonic Stack

Medium (261–290)

#ProblemPattern
261Daily TemperaturesMonotonic Stack
262Next Greater Element IMonotonic Stack
263Next Greater Element II (Circular)Monotonic Stack
264Online Stock SpanMonotonic Stack
265Remove K DigitsMonotonic Stack
266132 PatternMonotonic Stack
267Monotonic Stack Remove Duplicates to Get SmallestMonotonic Stack
268Decode StringStack + Counter
269Evaluate Reverse Polish NotationStack
270Flatten Nested List IteratorStack Design
271Design Circular QueueArray Design
272Design Circular DequeDeque Design
273Sliding Window MaximumMonotonic Deque
274Jump Game VIMonotonic Deque + DP
275Shortest Subarray with Sum at Least KDeque + Prefix Sum
276Max QueueDeque Design
277Task SchedulerGreedy / Queue
278Rearrange String k Distance ApartGreedy + Heap
279Number of Visible People in QueueMonotonic Stack
280Asteroid CollisionStack
281Score of ParenthesesStack
282Sum of Subarray MinimumsMonotonic Stack
283Basic Calculator IIStack + Precedence
284Rotting OrangesMulti-Source BFS
285Number of IslandsBFS / DFS
286Walls and GatesMulti-Source BFS
287Pacific Atlantic Water FlowBFS
288Course ScheduleTopological Sort BFS
289Course Schedule IITopological Sort
290Word LadderBFS

Hard (291–300)

#ProblemPattern
291Largest Rectangle in HistogramMonotonic Stack
292Maximal RectangleMono Stack on Matrix
293Basic Calculator IStack + Recursion
294Max Frequency StackFreq Buckets
295Design Hit CounterQueue / Binary Search
296Sliding Window Maximum (Hard variant)Deque
297Trapping Rain Water (Stack approach)Monotonic Stack
298Find Median from Data StreamTwo Heaps
299Serialize and Deserialize Binary TreeBFS Queue
300Stacks & Queues Master RecapCheatsheet

Advertisement

Sanjeev Sharma

Written by

Sanjeev Sharma

Full Stack Engineer · E-mopro