Daily Temperatures

MediumStackmonotonic stack

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given an array temperatures where temperatures[i] is the recorded temperature on day i. For every day, determine how many days you would have to wait until a day with a strictly warmer temperature arrives. Return an array answer of the same length, where answer[i] is the number of days you must wait after day i to reach a warmer day. If no future day is warmer than day i, set answer[i] to 0.

Examples

Input: temperatures = [73,74,75,71,69,72,76,73]
Output: [1,1,4,2,1,1,0,0]
Day 0 (73) is followed by day 1 (74), which is warmer, so the wait is 1. Day 2 (75) waits until day 6 (76), a gap of 4 days. Days 6 and 7 never see a warmer temperature, so both are 0.
Input: temperatures = [30,40,50,60]
Output: [1,1,1,0]
Each day is immediately followed by a warmer day, except the last, which has no future day.
Input: temperatures = [50,50,50]
Output: [0,0,0]
A warmer day must be strictly greater. Since all temperatures are equal, no day ever becomes warmer.

Constraints

  • 1 <= temperatures.length <= 10^5
  • 30 <= temperatures[i] <= 100

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More monotonic stack problems