Monotonic Stack Implementation in Java
14-day free trial
Try SeekerPro free for 14 days.
Full access while you try it · then $15.99/mo · cancel anytime
Start your 14-day free SeekerPro trial →30-day money-back guarantee · Cancel anytime
Not checking a site today? Turn this topic into a ready to paste AI prompt, free →
Key Takeaways
- ✓Monotonic Stack runs in O(n) time and O(n) space, in Java as in every other language.
- ✓ArrayDeque is the modern stack, with push, pop and peek. java.util.Stack also exists and still works, but it extends Vector and is synchronised on every operation, so it is legacy rather than idiomatic.
- ✓Declare it as Deque<Integer> stack = new ArrayDeque<>() so the interface stays narrow, and for a competitive or hot-path implementation use a plain int[] with an integer top pointer to avoid boxing entirely.
- ✓The Java trap: java.util.Stack iterates from the bottom upward because it inherits Vector iteration order, while ArrayDeque iterates from the top down.
- ✓Java generics are erased, so Deque<Integer> genuinely stores Integer objects and there is no way to specialise it for primitives.
What Monotonic Stack actually is
Monotonic Stack in Java: what you actually reach for
Java implementation
import java.util.ArrayDeque;
import java.util.Deque;
public final class MonotonicStack {
/** Idiomatic version: ArrayDeque, not the legacy synchronised Stack. */
public static int[] dailyTemperatures(int[] temps) {
int[] answer = new int[temps.length];
Deque<Integer> stack = new ArrayDeque<>(); // iterates top-down
for (int i = 0; i < temps.length; i++) {
while (!stack.isEmpty() && temps[stack.peek()] < temps[i]) {
int j = stack.pop();
answer[j] = i - j;
}
stack.push(i); // autoboxes: allocates outside -128..127
}
return answer;
}
/** Hot-path version: a primitive array and a top pointer. No boxing. */
public static int[] dailyTemperaturesFast(int[] temps) {
int[] answer = new int[temps.length];
int[] stack = new int[temps.length];
int top = -1;
for (int i = 0; i < temps.length; i++) {
while (top >= 0 && temps[stack[top]] < temps[i]) {
int j = stack[top--];
answer[j] = i - j;
}
stack[++top] = i;
}
return answer;
}
}Think Your Code Is Clean? Let NexusBro QA It in 20 Seconds.
Paste your code. Click QA. Get an instant expert-level audit with fixes.
QA My Code FreeThe Java-specific trap
Performance in Java, beyond the O(n)
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which Java type or library should I use for Monotonic Stack?
ArrayDeque is the modern stack, with push, pop and peek. java.util.Stack also exists and still works, but it extends Vector and is synchronised on every operation, so it is legacy rather than idiomatic.
What is the most common Java mistake when implementing Monotonic Stack?
java.util.Stack iterates from the bottom upward because it inherits Vector iteration order, while ArrayDeque iterates from the top down. Swapping one for the other, which looks like a pure modernisation, silently reverses the output of any code that prints or streams the stack. ArrayDeque additionally rejects null where Stack accepted it. And every push of an int autoboxes into an Integer, allocating for any value outside the cached -128 to 127 range, which is most indices in a real input.
How does Monotonic Stack actually perform in Java?
Java generics are erased, so Deque<Integer> genuinely stores Integer objects and there is no way to specialise it for primitives. The int[] version avoids one allocation per push and keeps the whole stack in a single contiguous array, which is typically several times faster on large inputs. Time complexity is O(n) and space is O(n).
Related Articles
Rather try before you buy?
Get the rest of the vault: every cheat sheet, guide and audit in SeekerPro, free for 14 days, then $15.99/mo. Cancel anytime. Daily usage limits apply.
Start my 14-day SeekerPro trialIf you are a student, see what GitHub Copilot offers students for free on Noizz.
Is your site built to last?
Run a free QA audit and get your Site Health Score in seconds.
Check Your Site FreeNo signup required
Reader comments
No account needed. Share what worked, what did not, and what you would add.
Add your take below. A concrete tip, a result or a question helps the next reader most.