Skip to content
  • Dashboard
  • Review
  • Go: plan & chat
  • Roadmap
  • Patterns & lessons49
  • Sheets
  • Problems1.5k
  • Ascend
  • Toolkit
  • Core CS + SQL
  • System Design
  • Machine Coding
  • Interview hub
  • Mock interview
  • Companies & OA
  • Leaderboard
  • Battles
  • College Cup
  • People

Keep your progress

Sync across devices with a free account.

Sign upSign in
Patterns & lessons
Sign inSign up
Patterns

Topic · 2 patterns

Stacks, Queues & Deques patterns

A monotonic stack keeps its elements in order by popping everything the new element beats, which answers "next greater" or "previous smaller" for every index in O(n). A monotonic deque does the same over a sliding window, so the window's maximum or minimum costs O(1) per step.

The patterns

  1. Monotonic Stack

    O(n)

    Keep the stack increasing/decreasing; when the new element breaks the order, pop — each pop is an answer.

    Spot it: Next/previous greater or smaller for every index, histogram rectangles, trapping rain water, spans, sum of subarray mins/maxes.

    Learn Monotonic StackCheat sheet and practice
  2. Monotonic Deque

    O(n)

    Keep a deque of candidate indices in decreasing value; pop the front when it leaves the window.

    Spot it: Max/min of every window of size k; dp[i] = best of dp[j] for j in [i-k, i-1]; shortest subarray with sum >= K with negatives.

    Learn first: Monotonic Stack, Sliding Window

    Learn Monotonic DequeCheat sheet and practice

Which one? Match the signal

If the problem saysReach for
"Next/previous greater or smaller element", histogram area, trapping waterMonotonic StackO(n)
Sliding-window maximum / minimum — running extremum over a window of size kMonotonic DequeO(n)

Easy to confuse

  • Shortest Subarray with Sum at Least K looks like Sliding Window, but it’s Monotonic Deque

    Negatives break shrinking; keep a monotone deque of prefix sums.

  • Count of Smaller Numbers After Self looks like Monotonic Stack, but it’s Segment Tree / Fenwick (BIT)

    Counts of all smaller elements, not the nearest one: BIT over compressed values or merge-sort counting.

  • Sum of Subarray Minimums looks like Sliding Window, but it’s Monotonic Stack

    No window condition; each element's contribution span comes from previous/next smaller.

  • 132 Pattern looks like Two Pointers, but it’s Monotonic Stack

    Need the best middle candidate for each position; a decreasing stack scanned from the right tracks it.

  • Max Value of Equation looks like Two Pointers, but it’s Monotonic Deque

    Rewriting gives a window maximum of (y - x).

  • Remove K Digits looks like Greedy, but it’s Monotonic Stack

    The greedy 'drop a digit larger than the next' is implemented by a monotonic stack.

Used together

  • Sliding Window + Monotonic Deque

    When: subarray where max - min <= limit, or window extreme queries. Validity depends on window max-min, which a counter cannot maintain under deletions.

  • Prefix Sum + Monotonic Deque

    When: shortest subarray with sum >= K when values can be negative; max subarray sum with length in [a, b]. Negative values break window monotonicity; on prefix sums, dominated left ends can be discarded.

  • 1D / Fibonacci-style + Monotonic Deque

    When: jump at most k steps; subsequence with gaps at most k. Transition dp[i] = f(i) + max(dp[i-k..i-1]) is O(nk) naively.

  • Monotonic Stack + Contribution Technique (sum over all subarrays/pairs)

    When: sum over all subarrays of min, max, or range. O(n2) subarrays collapse to per-element counts.

Where to start

then the The 128 and The 365 sheets

Monotonic Stack

  • Nearest Smaller ValuesMedium
  • Daily TemperaturesMedium

Monotonic Deque

  • Sliding Window MaximumHard
  • Sliding Window MinimumMedium

Further reading

free, outside Lessgo
  • Queue & Stack explore cardLeetCode

    Stack/queue basics plus BFS and DFS with explicit stacks.

  • Stack cheatsheetTech Interview Handbook

    Monotonic stack recognition and problem list.

  • Minimum stack / minimum queuecp-algorithms

    Monotonic deque for sliding-window min/max explained rigorously.

  • Sliding Window (monotonic deque)USACO Guide

    Sliding window min/max with a deque, with problems.

  • NeetCode RoadmapNeetCode

    Pattern-ordered problem tree with a short video per problem; best interview-first path.

← Dynamic ProgrammingAll 49 patternsStrings & Tries →
TodayReviewPatternsProblems