Daily Temperatures is Day 16 of the Tekraze DSA series, and it wraps up the stacks chapter. It is a classic next greater element problem. For each day you need the wait until a warmer temperature shows up, and a monotonic stack answers every day in one pass.
Also Read: Evaluate Reverse Polish Notation: 6 Best Tips (Day 15)
What is the Daily Temperatures problem?
You get an integer array temperatures where temperatures[i] is the temperature on day i. Return an array answer where answer[i] is the number of days you wait after day i to see a warmer temperature. If no warmer day exists later, put 0. This is the classic LeetCode problem of the same name.
temperatures = [73,74,75,71,69,72,76,73] answer = [1, 1, 4, 2, 1, 1, 0, 0] Day 0 (73) warms on day 1 (74) after 1 day Day 2 (75) warms on day 6 (76) after 4 days Day 6 and 7 never see a warmer day later
How should you think about the Daily Temperatures problem in plain words?
For every day you are looking for the next greater temperature to the right. Scanning forward from each index is the obvious idea. A better idea is to keep unfinished colder days on a stack and resolve them the moment a warmer day arrives.
The stack stays decreasing in temperature, so the top is always the most recent day still waiting for heat. Here is the first example traced as the stack solves Daily Temperatures, storing day indexes, not temperatures.
| Day | Temp | Pops (day gets answer) | Stack after (days) |
|---|---|---|---|
| 0 | 73 | none | [0] |
| 1 | 74 | day 0 gets 1 | [1] |
| 2 | 75 | day 1 gets 1 | [2] |
| 3 | 71 | none | [2, 3] |
| 4 | 69 | none | [2, 3, 4] |
| 5 | 72 | day 4 gets 1, day 3 gets 2 | [2, 5] |
| 6 | 76 | day 5 gets 1, day 2 gets 4 | [6] |
| 7 | 73 | none | [6, 7] |
Days 6 and 7 are still on the stack at the end, so they keep their zero.

What does the brute force approach look like?
From each day i, walk right until you find a strictly greater temperature, then record the distance. If you reach the end, leave a zero. This is the simplest way to solve Daily Temperatures.
python
def daily_temperatures_brute(temperatures):
n = len(temperatures)
answer = [0] * n
for i in range(n):
for j in range(i + 1, n):
if temperatures[j] > temperatures[i]:
answer[i] = j - i
break
return answer
That is correct, but the nested loops make it O(n squared) in the worst case, which happens when temperatures keep falling. Interviews expect the linear stack version.

How do we get the optimal solution to Daily Temperatures?
Walk left to right and keep a stack of indices whose temperatures are strictly decreasing. When today is warmer than the day on top of the stack, that earlier day has found its answer. Pop it, set answer[j] = i - j, and keep popping while the stack top is still colder. Then push today.
Time is O(n) because each index is pushed and popped at most once. Space is O(n) for the stack and the answer array. If those terms feel rusty, read our Big O helper post on time and space complexity.
Daily Temperatures in Java
Java’s ArrayDeque works as the stack, and the official ArrayDeque docs recommend it over the legacy Stack class.
java
[Your original Java code, unchanged]
Daily Temperatures in Python
A plain list is the stack. The Python tutorial on lists as stacks shows how append and pop give you last in, first out behavior.
python
[Your original Python code, unchanged]
Daily Temperatures in Rust
Vec<usize> holds the indices. The while let loop peeks at the top, pops while the day is colder, and breaks as soon as it is not.
rust
[Your original Rust code, unchanged]
The article’s tests cover the classic [73,74,75,71,69,72,76,73] case that returns [1,1,4,2,1,1,0,0], plus rising runs, all equal temperatures, strictly falling temperatures, a two day rise and a single day. All seven checks passed in Java, Python and Rust when the author ran them, and I re-ran the Python version and compared it to the brute force on 2,000 random arrays.
What are the 6 best tips for an easy solution?
- Spot the pattern. Next greater element to the right is almost always a monotonic stack of indices, and Daily Temperatures is the textbook case.
- Store indices, not values. You need the distance
i - j, so the stack must hold positions. - Read “warmer” as strictly warmer. Equal temperatures do not count, so pop only while
temperatures[stack.top] < temperatures[i]. - Keep the stack decreasing. The first warmer day resolves the nearest waiting days first.
- Test the edges. Try a single day, all equal, strictly decreasing, and a long rising streak that clears the whole stack.
- Explain the nested loop. Say that each index enters and leaves the stack once, so the total work stays linear even with a
whileinside afor.
What are the common mistakes people make with Daily Temperatures?
- Pushing temperatures instead of indices, then losing the day distance.
- Using greater or equal instead of strictly greater, which breaks equal temperature days.
- Forgetting to leave zeros for days that never warm up.
- Falling back to a nested search after already knowing the monotonic stack pattern.
- Making an off by one error when computing
i - j, or when the array has only one element.
Where is this pattern used in the real world?
The same next greater element idea sits behind stock span and histogram style questions. It fits any stream where each item needs the nearest larger value to its right, such as a price series or sensor readings. Once you see Daily Temperatures as that general shape, a lot of interview problems look familiar.

Which earlier posts in the series should you read?
Start with Day 1, Two Sum for hash map basics and Day 2, Best Time to Buy and Sell Stock for a running minimum. Day 3, Contains Duplicate, Day 4, Valid Anagram and Day 5, Group Anagrams build the counting toolkit. Day 6, Valid Palindrome, Day 7, Two Sum II, Day 8, 3Sum and Day 9, Container With Most Water cover two pointers.
Day 10, Longest Substring Without Repeating Characters, Day 11, Longest Repeating Character Replacement and Day 12, Minimum Window Substring cover sliding window. Day 13, Valid Parentheses, Day 14, Min Stack and Day 15, Evaluate Reverse Polish Notation are the stack warm ups for this post. Keep our data structures cheat sheet handy to see how stacks compare with other structures.
Conclusion
You can now solve Daily Temperatures with one monotonic stack of indices and a strict comparison. Code it from memory in your favorite language, then run it on an all equal array. That finishes the stacks chapter, so next we move on to the next topic in the series.
Frequently asked questions
What is the Daily Temperatures problem in DSA?
You get daily temperatures and must return how many days you wait after each day for a strictly warmer temperature. If none appears later, that day gets 0.
Why use a monotonic stack for Daily Temperatures?
A decreasing stack of unfinished colder days lets each warmer day resolve every earlier day that was waiting for it, so every index is handled in total linear time.
What is the time and space complexity of Daily Temperatures?
Time is O(n) because each index is pushed and popped at most once. Space is O(n) for the stack and the answer array.
Do equal temperatures count as warmer in Daily Temperatures?
No. You need a strictly greater temperature. Equal days stay on the stack until a hotter day arrives or the array ends.
Where else does the next greater element stack show up?
Stock span problems, histogram questions, and any stream that asks for the next larger value to the right.





