Skip to content
Tech Atlas, home
Search
Esc
  • All roadmapsBrowse every learning path
  • ExploreFind a technology area
  • ProjectsGuided projects and builds
  • My AtlasYour learning dashboard
Skill

Monotonic Stacks & Queues

Keep stacks and deques in sorted order to answer next-greater and window-extreme queries in linear time.

Intermediate1 roadmap

4 objectives 6 steps about 125 min

Check what you know

The lessons are coming later. The knowledge check is ready now.

Take the knowledge check

This skill page is a public outline. Its lessons and knowledge check appear here as subject-matter experts approve them.

Learning objectives

  • Compute the next and previous greater or smaller element for every index in O(n) with a monotonic stack, and explain the amortized bound.
  • Solve span and area problems (stock span, largest rectangle in a histogram, trapped rain water) by reasoning about what each pop resolves.
  • Maintain a monotonic deque that reports the maximum of every sliding window of size k in O(n).
  • Use previous- and next-smaller boundaries to sum each element's contribution over all subarrays (sum of subarray minimums), and apply a monotonic stack greedily to build the smallest number after removing k digits.

Lesson plan

6 steps, about 125 min · 0 of 5 lessons available so far

  1. Keeping the Stack Sorted Concept · 20 min · coming later
  2. Next Greater Element, Traced Worked example · 20 min · coming later
  3. What Each Pop Resolves: Spans and Histograms Worked example · 25 min · coming later
  4. Lab: Window Maximum with a Deque Lab · 20 min · coming later
  5. Contributions and Greedy Removal with a Stack Worked example · 25 min · coming later
  6. Check: Monotonic Structure Problems Knowledge check · 8 questions

Practise it

Implement days-until-warmer, largest rectangle in a histogram, and sliding-window maximum; verify each against a brute-force O(n^2) version on 1,000 random inputs and report the timing difference. Add sum of subarray minimums and remove-k-digits, both checked against brute force on inputs with runs of equal values.

References

Hard prerequisites

Unlocks

Nothing depends on this skill yet.

On these roadmaps

Progress and evidence for this skill are shared across all of them.