All topics
5 patterns · 27 problems

Stacks & Queues

LIFO and FIFO structures powering monotonic stacks, expression evaluation, and windowed queues.

01Pattern

Design Problems

Build custom structures from stacks and queues

When to reach for it

  • When you need to design and implement a custom data structure based on stacks.

Spot it in the prompt

Look for problems that require you to design a stack-based system or implement specific functionalities.

Easy

Implement Queue using Stacks

LeetCode
Loading animation…

How to solve it

Animation: Design Problems technique

Use two stacks: push onto an input stack, and transfer to an output stack for O(1) amortized dequeue and peek.

Open full solution

Practice · 7 problems

Select any problem to watch the technique run. marks a problem with its own bespoke animation.

02Pattern

Monotonic Stack

Find nearest greater or smaller in one pass

This patternO(n)naïve O(n^2)

When to reach for it

  • You encounter a problem where you need to find the nearest smaller or greater element to the left or right in an array. For example, finding the nearest greater element on the right in an array.

Spot it in the prompt

Look for problems where you need to maintain a monotonic increasing or decreasing stack to efficiently track elements' positions or values.

Medium Bespoke animation

Daily Temperatures

LeetCode
Loading animation…

How to solve it

Use a decreasing monotonic stack of indices; pop while the current temperature is warmer and record the day difference.

Open full solution

Practice · 5 problems

Select any problem to watch the technique run. marks a problem with its own bespoke animation.

03Pattern

Expression Evaluation

Parse and compute expressions with stacks

When to reach for it

  • You encounter problems involving arithmetic or logical expression evaluation, where you need to parse and compute expressions.

Spot it in the prompt

Look for problems that involve evaluating arithmetic expressions or postfix notation.

Medium

Basic Calculator II

LeetCode
Loading animation…

How to solve it

Animation: Expression Evaluation technique

Scan the string, applying multiplication and division immediately while pushing additive terms on a stack, then sum the stack.

Open full solution

Practice · 5 problems

Select any problem to watch the technique run. marks a problem with its own bespoke animation.

04Pattern

Two Stacks

Pair two stacks to track extra state

When to reach for it

  • You need to maintain a specific order or perform operations efficiently by utilizing two stacks, such as simulating a queue using two stacks.

Spot it in the prompt

Look for problems where you can simulate a specific behavior or implement operations efficiently by employing two stacks.

Medium

Evaluate Reverse Polish Notation

LeetCode
Loading animation…

How to solve it

Animation: Two Stacks technique

Push operands on a stack; on each operator pop two operands, apply it, and push the result back.

Open full solution

Practice · 5 problems

Select any problem to watch the technique run. marks a problem with its own bespoke animation.

05Pattern

Sliding Window & Monotonic Queue

Track window extremes in amortized constant time

This patternO(n)naïve O(n*k)

When to reach for it

  • You need to maintain a sliding window or find the maximum/minimum element in a window efficiently, often used in problems involving data stream processing or window-based statistics.

Spot it in the prompt

Look for problems where you need to process elements in a sliding window or maintain monotonicity in a queue, especially in array-related or streaming problems.

Hard

Sliding Window Maximum

LeetCode
Loading animation…

How to solve it

Animation: Sliding Window & Monotonic Queue technique

Maintain a decreasing deque of indices; pop smaller values from the back and out-of-window indices from the front so the front stays maximal.

Open full solution

Practice · 5 problems

Select any problem to watch the technique run. marks a problem with its own bespoke animation.

You've got the patterns

Patterns get you through the screen. Shipping gets you hired.

FDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.

750+ engineers trained · frontend, backend, AWS & AI

August 15 · 0d left
Enroll Now