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

Lesson 6.2 of 10 in this module Unrolled Pro

Valid Parentheses

Decide whether a string of (), [] and {} is correctly nested. From repeatedly deleting matched pairs (O(n²)), to a depth counter that works for one bracket type but fails on mixed ones, to a stack of open brackets (O(n)). Then the longest valid run with a stack of indices, validating a model's JSON output while skipping brackets inside strings and reporting where it broke, and when a counter with O(1) memory is enough.

about 10 min 5 Python examples 2 quick checks

Problem. Given a string s made of the characters ()[]{}, return whether it is valid: every opening bracket is closed by the same type, in the correct order, and every closing bracket has a matching opener.

text
"()[]{}"   →   True
"{[()]}"   →   True
"([)]"     →   False      (the ] closes a [ while a ( is still open inside it)
"(("       →   False

ML context. Structured output from a language model (JSON, a tool call, a code snippet) is useless if it is unbalanced. Checking bracket nesting is the cheapest first test, it tells you where generation went wrong, and constrained decoding uses the same stack to know which closing bracket is allowed next. Config parsers and expression evaluators (lesson 6.6) run on the same idea.


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 "Valid Parentheses"

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