For every element, the first larger element to its right. From scanning right from each position (O(n²)), to jumping along answers already computed (fast, but hard to bound), to a monotonic stack of elements still waiting for their answer, where each element is pushed and popped once (O(n)). Then answering queries for a subset, circular arrays by walking twice, the four greater/smaller and previous/next variants, and a streaming version that tells you how many eval steps passed before a metric was beaten.
about 10 min 6 Python examples 2 quick checks
Problem. Given an array a, return an array out where out[i] is the first element to the right of i that
is strictly greater than a[i], or −1 if there is none.
ML context. "When was this value first beaten?" is a common question over metric streams: for each evaluation
step, how many steps passed before validation accuracy exceeded it (a measure of progress and plateaus), the next
time a latency series spikes above the current level, the next higher-priced bid. Change D turns the answer into a
streaming tool that resolves each step as soon as it's beaten.
1. Step 1: Clarify
That's the preview. Modules 00–01 are free; this lesson is part of Unrolled Pro.
Staff AI & Pro Tier Lesson
Unlock "Next Greater Element"
The foundations are free forever. Unrolled Pro unlocks the complete lesson, production architectures, and 55 battle-tested ML system design case studies.
Everything included with Pro:
All 234+ Deep-Dive Lessons across transformer internals, inference, RAG & agents
55 Staff-Level ML & System Design Case Studies with mathematical capacity estimates
AI Engineer Sheet & Exercises with worked solutions, self-checks, and code
100 Landmark Research Papers guided walkthroughs & progress tracking
14-Day Money-Back Guarantee — 100% full refund if you aren't satisfied