Skip to content
playdsa
Preferences

Make yourself comfortable.

Saved on this browser. Your device’s reduced-motion preference is always respected.

Theme
Advanced settings

The Balanced Branches

Learn
Play
Prove
Problem context and objectives
Mission briefing

Puzzle systems engineer · A game mechanic is behaving incorrectly

Prune prefixes that can never close correctly: The Balanced Branches.

Brute force may pass the demo but fail when the world fills with players.

How you win

  1. 1Recognize when Generate parentheses matches the clues
  2. 2Keep this true after every move: every path prefix has closes <= opens <= n
  3. 3Reach the result within Catalan output time

Rules and pressure

  • Target cost: Catalan output time
  • State rule: every path prefix has closes <= opens <= n
Lesson 1 of 3

Live algorithm trace

Generate parentheses

Complete execution
1 of 5
(
token
(
)
(
)
)

Only an opening parenthesis is legal at the root.

1search(path, opens, closes)
2if length is 2n: record
3if opens < n: append open
4if closes < opens: append close
5undo each choice
6return results
n = 3opens = 0closes = 0
Truth to preserve / Cost target
Truth to preserve

every path prefix has closes <= opens <= n

Cost target

Catalan output time

A prefix may open while opens remain, and may close only when more opens than closes have been used. These constraints prevent invalid strings from entering the tree.

Your call · What should guide every step of this algorithm?

Help shape PlayDSA

Something confusing, broken, or missing? Leave a quick note without leaving your lesson.

Please leave out passwords, payment details and other private information.

Page included: /

Sign in to save feedback here, or send it with your email app. Your draft stays here while you sign in.

Open email instead