VFlash: Fast, Accurate Visual Planning via Flash Primitive Decoding
u_Unrolled
DSA for AI/ML · Module 06: Stacks and queues

Lesson 6.4 of 10 in this module Unrolled Pro

Next Greater Element

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.

text
a = [2, 1, 2, 4, 3]   →   [4, 2, 4, -1, -1]
a = [4, 3, 2, 1]      →   [-1, -1, -1, -1]

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
View Plans & Unlock All Lessons

From ₹167/mo (~$2/mo) · Cancel anytime · Instant activation

100% Risk-Free: 14-day no-questions-asked money-back guarantee