How many days until a warmer day?

Stacks and queues Mid-level Python 3.13+ Kotlin 2.4+ Reviewed By Best Latest Apps

Pick the fix

Why is the monotonic stack solution O(n) even though it has a while loop inside a for loop?

Options

Choose an answer, then open Show answer below to check it.

Show answer

Answer: A Each day is pushed once and popped at most once

Right: total pops across the whole run cannot exceed n.

Counting pushes and pops over the whole run gives at most 2n stack operations.

Why not the others?
  • B The while loop always runs once. It can pop many days in one step; the bound comes from the total.
  • C The stack never holds more than two days. It can hold a long run of falling temperatures.
Days wait on a stack until a warmer day arrives and answers them, for two weeks of temperatures.

Steps come from running the Python solution.

Text version

Week one:

  1. Push 0; waiting [30]. Day 0 (30): nothing to answer
  2. Push 1, answer 0; waiting [32]. Day 1 (32) answers day 0
  3. Push 2; waiting [32, 31]. Day 2 (31): nothing to answer
  4. Push 3; waiting [32, 31, 29]. Day 3 (29): nothing to answer
  5. Push 4, answer 3, 2, 1; waiting [33]. Day 4 (33) answers days 3, 2, 1
  6. Push 5; waiting [33, 28]. Day 5 (28): nothing to answer
  7. Push 6, answer 5, 4; waiting [35]. Day 6 (35) answers days 5, 4

Result: [1, 3, 2, 1, 2, 1, 0]

Week two:

  1. Push 0; waiting [40]. Day 0 (40): nothing to answer
  2. Push 1; waiting [40, 38]. Day 1 (38): nothing to answer
  3. Push 2; waiting [40, 38, 36]. Day 2 (36): nothing to answer
  4. Push 3, answer 2, 1, 0; waiting [41]. Day 3 (41) answers days 2, 1, 0
  5. Push 4; waiting [41, 39]. Day 4 (39): nothing to answer
  6. Push 5, answer 4, 3; waiting [42]. Day 5 (42) answers days 4, 3

Result: [3, 2, 1, 2, 1, 0]

30-second answer

Keep a stack of days still waiting for a warmer day; their temperatures decrease from bottom to top. For each new day, pop every waiting day that is colder and record the gap, then push the new day. Each day is pushed and popped at most once: O(n) time, O(n) space.

Practise saying it · 60 s. No recording, just a timer

Explain the monotonic stack for next-warmer questions.

Say your answer out loud, then check which of these points you covered:

  • Stack of waiting days
  • Pop colder days and record gaps
  • O(n) amortised

Deep dive

This is the monotonic stack pattern: the stack stays sorted, and each pop answers a question.

Checking every later day for each day is O(n²).

Walking from the right with a stack of candidates works too.

Python solution Ran on Python 3.13.5 · mypy --strict
Python 3.13
def wait_for_warmer(temps: list[int]) -> list[int]:
    wait = [0] * len(temps)
    pending: list[int] = []  # days still waiting, colder at the top
    for day, t in enumerate(temps):
        while pending and temps[pending[-1]] < t:
            earlier = pending.pop()
            wait[earlier] = day - earlier
        pending.append(day)
    return wait

Kotlin Ran on Kotlin 2.4.21 · JDK 21.0.12.1
Kotlin 2.4
fun waitForWarmer(temps: List<Int>): List<Int> {
    val wait = MutableList(temps.size) { 0 }
    val pending = ArrayDeque<Int>()
    for ((day, t) in temps.withIndex()) {
        while (pending.isNotEmpty() && temps[pending.last()] < t) {
            val earlier = pending.removeLast()
            wait[earlier] = day - earlier
        }
        pending.addLast(day)
    }
    return wait
}

Common mistakes

  • Popping with <= so equal temperatures count as warmer.
  • Storing temperatures instead of indices, which loses the gap.

Senior take & follow-ups

Explain why each day is popped at most once; it is the O(n) proof.

Follow-up questions

Think of your answer first, then open the hint.

  1. What about the next colder day?

    Show hint

    Flip the comparison so the stack keeps increasing temperatures.

  2. How would you handle a circular week?

    Show hint

    Walk the list twice (indices mod n) so the end can see the start.

What each operation costs: Stack · Queue · Deque

From the author

DSA Master - Data & Algorithms

Free on iPhone and iPad: watch 50+ data structures and algorithms step through animations, run the code in the playground, and try the daily challenge.

Suggest a fix if something here is wrong or out of date.