Skip to content

Interview Solver

FeaturesPricingHelp
Sign In
FeaturesPricingHelpSign In
  1. Interview Questions
  2. /
  3. Problems
  4. /
  5. Daily Temperatures

Daily Temperatures

For each day, count how many days you wait until a strictly warmer one. The go-to problem for learning the monotonic stack. Solve it below in Python or JavaScript, then read the walkthrough.

Solve it in your browserRead the solution
MediumArrayStackMonotonic StackAsked at 106 companies incl. Google, Amazon, Meta

Problem

You're given an array temperatures where temperatures[i] is the temperature on day i.

Return an array answer of the same length where answer[i] is the number of days after day i until a strictly warmer day. If no later day is warmer, answer[i] is 0.

Example 1

Input
temperatures = [64, 61, 66, 70, 68, 75, 60]
Output
[2, 1, 1, 2, 1, 0, 0]

Day 0 (64) first sees something warmer on day 2 (66), two days later. Day 5 (75) is never beaten, and day 6 is the last day.

Example 2

Input
temperatures = [40, 50, 60]
Output
[1, 1, 0]

Example 3

Input
temperatures = [55, 55, 55]
Output
[0, 0, 0]

An equal temperature is not warmer.

Constraints

  • 1 ≤ temperatures.length ≤ 10⁵
  • 30 ≤ temperatures[i] ≤ 100
  • Only a strictly higher temperature counts as warmer.

Practice

1
2
3
4

Runs 13 tests in your browser. Python loads on the first run (about 6 MB, then cached). Press ⌘/Ctrl + Enter to run.

Get asked Daily Temperatures in a real interview?

Interview Solver gives you a working solution live, during your actual coding interview. Invisible to screensharing.

Start Free Trial

How to solve Daily Temperatures

Pattern: Monotonic decreasing stack

Each day is asking "what is the next greater element to my right?" That phrase is the cue for a monotonic stack.

Walk left to right and keep a stack of the indices of days that are still waiting for a warmer day. Their temperatures are always in non-increasing order from bottom to top, because any day warmer than the top would have already resolved it. When today is warmer than the day on top of the stack, today is that day's answer: pop it and record today - index. Keep popping while today beats the top, then push today.

Each index is pushed once and popped at most once, so the whole pass is linear even though there is a loop inside a loop.

Approach 1: Brute force: scan forward from each day

For each day, walk forward until you find a warmer temperature. Simple, but a long run of equal or falling temperatures makes every day scan almost the whole array.

Time O(n²) · Space O(1) besides the output

Approach 2: Scan from the right with next-warmer jumps

Go from right to left. For day i, start at j = i + 1 and, while temperatures[j] ≤ temperatures[i], jump ahead by answer[j] (or stop if it is 0). This reuses earlier answers to skip days and runs in amortized linear time with no extra stack.

Time O(n) amortized · Space O(1) besides the output

Approach 3: Monotonic stack of waiting days

  1. Create answer filled with 0 and an empty stack of indices.
  2. For each day i: while the stack is non-empty and temperatures[i] > temperatures[stack.top], pop j and set answer[j] = i - j.
  3. Push i.

Anything still on the stack at the end never found a warmer day and keeps its 0.

Time O(n) · Space O(n)

ApproachTimeSpace
Brute force: scan forward from each dayO(n²)O(1) besides the output
Scan from the right with next-warmer jumpsO(n) amortizedO(1) besides the output
Monotonic stack of waiting daysO(n)O(n)

Solution code

Try the practice editor first. Both solutions pass every test above.

▸Daily Temperatures solution in Python
def daily_temperatures(temperatures: list[int]) -> list[int]:
    answer = [0] * len(temperatures)
    stack: list[int] = []  # indices still waiting for a warmer day
    for i, temp in enumerate(temperatures):
        while stack and temp > temperatures[stack[-1]]:
            j = stack.pop()
            answer[j] = i - j
        stack.append(i)
    return answer
▸Daily Temperatures solution in JavaScript
function dailyTemperatures(temperatures) {
  const answer = new Array(temperatures.length).fill(0);
  const stack = []; // indices still waiting for a warmer day
  for (let i = 0; i < temperatures.length; i++) {
    while (stack.length && temperatures[i] > temperatures[stack[stack.length - 1]]) {
      const j = stack.pop();
      answer[j] = i - j;
    }
    stack.push(i);
  }
  return answer;
}

Common mistakes

  • Popping on an equal temperature. The problem asks for strictly warmer, so use > and not ≥.
  • Pushing temperatures instead of indices, which leaves you unable to compute the distance.
  • Using an if instead of a while, so one warm day only resolves a single waiting day.
  • Claiming the nested loop makes it O(n²). Each index is pushed and popped at most once.

What interviewers look for

Name the pattern explicitly: "this is next greater element, so I'll use a monotonic stack." Then state the invariant (the stack holds unresolved days with non-increasing temperatures) and the amortized argument (every index is pushed and popped at most once). Those two sentences are what interviewers want to hear.

Trace the example by hand for three or four steps, showing what is on the stack, and call out the equal-temperature case before you finish.

Follow-up questions

What if you wanted the next colder day instead?

Flip the comparison so the stack stays non-decreasing: pop while today is strictly colder than the top. Everything else stays the same.

Can you do it without the extra stack?

Yes. Iterate from the right and, for each day, jump forward using the answers already computed (j += answer[j]) until you hit a warmer day or a 0. Each jump skips days that can never be the answer, so it is still amortized O(n) with O(1) extra space.

How does this extend to a circular array (Next Greater Element II)?

Run the same stack loop over the array twice, using index i % n, and only push indices during the first pass. Elements that wrap around get resolved during the second pass.

Companies that ask Daily Temperatures

Daily Temperatures is tagged by 106 companies in our interview dataset. The score shows how often it comes up at each company.

  • Kin Carta100%
  • Keysight100%
  • Airwallex100%
  • Personio98%
  • Coforge96%
  • Agoda88.6%
  • Shield AI87%
  • Skyworks Solutions87%
  • Trellix84%
  • Mollie84%
  • Mercedes Benz83%
  • Zoho82.8%
  • Gopuff81%
  • Darktrace80%
  • Anduril Industries78%
  • Siriusxm78%
  • 3m78%
  • Retailmenot77%
  • Podium77%
  • Plaid76%
  • Blue Origin76%
  • Arcus Biosciences76%
  • Grab75.7%
  • Terumo75%

and 82 more.

Practice next

  • Trapping Rain WaterHardTwo pointers with running maximums
  • Two SumEasyHash map of complements
  • Number of IslandsMediumFlood fill (DFS/BFS on a grid)
  • Merge IntervalsMediumSort, then sweep

Get live help in the real interview

Working solutions in real time during your actual interview. Invisible to screensharing.

Start Free Trial

No credit card required

Interview Solver
  • Home
  • About
  • Pricing
  • Sign in
  • Contact
  • Blog
  • Companion
  • Mobile
  • Help Center
  • Developers
  • Use Cases
  • AI Interview Copilot
  • Coding Interview Copilot
  • AI Interview Assistant
  • Software Engineer
  • Product Manager
  • Misc
  • Privacy Policy
  • Terms and Conditions
  • Refund & Cancellation
  • Discord Community
  • Affiliate Program
  • Compare
  • All Comparisons
  • vs Final Round AI
  • vs UltraCode
  • vs Interview Coder
  • vs LockedIn AI
  • vs AI Apply
  • Free Tools
  • ATS Resume Checker
  • STAR Method Builder
  • Salary Lookup
  • Big O Cheat Sheet
  • Total Comp Calculator
  • View All →
  • Interview Questions
  • Google Questions
  • Amazon Questions
  • Microsoft Questions
  • Meta Questions
  • Apple Questions
  • TikTok Questions
  • View All →
© 2026 Interview Solver, Inc. All rights reserved.