Peak depth times group count of a bracket scan

You validate the bracket string {[()]}[{}] with the standard stack method (push each opener, pop on each matching closer). Let DD be the maximum number of items on the stack at any instant, and let GG be the number of times the stack returns to empty during the scan. Report the product D×GD \times G.

Show hints (2)+
  1. Stack size = current nesting depth; its peak is DD.
  2. Each time the stack returns to empty ends one top-level group; count those for GG, then multiply.

Answer

Reveal answer →

6

Want the full step-by-step worked solution? It's part of Premium - along with a worked solution for every question in the bank.

Asked at: Probability & Market-Making, Game-Based Aptitude

Related questions