Stack•Pattern: Monotonic Stack•Medium

Daily temperatures — next warmer day

Problem Intuition & Objective

For each day, find how many days until a warmer temperature using a monotonic stack.

Time ComplexityO(n)
Optimal
Space ComplexityO(n)
Memory Bounds
Optimal Algorithmic Approach

Monotonic Decreasing Stack: each element is pushed and popped at most once.

Core Invariants & Edge Cases

  • Maintain a monotonically decreasing stack of indices
  • When a warmer temperature is found, pop and compute distance
  • Unresolved indices in stack get 0
Previous ProblemDeep copy a linked list with random pointersNext Problem Largest rectangle in histogram

System Design & Architecture Roadmap

Coding interviews are only 50% of the bar. Master High-Level Design, distributed systems, and Object-Oriented LLD.