21. Daily Temperatures
mediumAsked at LINEFor each day, find how many days until a warmer temperature — LINE uses this to gauge whether you reach for a monotonic stack, the same primitive behind their next-read-receipt scan.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Problem
Given an array temperatures of daily temperatures, return an array answer where answer[i] is the number of days you have to wait after the i-th day to get a warmer temperature. If there is no future day with a higher temperature, set answer[i] to 0.
Constraints
1 <= temperatures.length <= 10^530 <= temperatures[i] <= 100
Examples
Example 1
temperatures = [73,74,75,71,69,72,76,73][1,1,4,2,1,1,0,0]Example 2
temperatures = [30,40,50,60][1,1,1,0]Approaches
1. Brute force look-ahead
For each index, scan forward until you find a strictly larger value.
- Time
- O(n^2)
- Space
- O(1)
for(let i=0;i<n;i++){
for(let j=i+1;j<n;j++)
if(t[j]>t[i]){ out[i]=j-i; break; }
}Tradeoff:
2. Monotonic decreasing stack
Walk left to right and keep a stack of indices with decreasing temperatures. Each time you see a hotter day, pop indices and record the gap. Each index enters and leaves the stack once.
- Time
- O(n)
- Space
- O(n)
function dailyTemperatures(temps) {
const out = new Array(temps.length).fill(0);
const stack = [];
for (let i = 0; i < temps.length; i++) {
while (stack.length && temps[i] > temps[stack[stack.length - 1]]) {
const prev = stack.pop();
out[prev] = i - prev;
}
stack.push(i);
}
return out;
}Tradeoff:
LINE-specific tips
At LINE, link the monotonic-stack pattern to scanning a chat timeline for the next message with a read-receipt acknowledgement — presence + read-receipt framing wins.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
Practice these live with InterviewChamp.AI
Drill Daily Temperatures and other LINE interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →