Stack-based parenthesis matching

Which data structure validates balanced brackets in a single pass, and at what cost?

Show hints (2)+
  1. You must match the most recent unmatched opener first.
  2. Last-in, first-out.

Answer

Reveal answer →

A stack — $O(n)$

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: Jane Street, IMC

Related questions