Data Structures & AlgorithmsHard

Why is a stack-based check that tracks TYPE as well as nesting needed for two bracket kinds?

  1. A. Two bracket kinds can be checked by counting alone.
  2. B. A closing bracket must match the most recently opened bracket of the same kind, so the stack must store which bracket was opened.
  3. C. Each bracket type can be checked independently of order.
  4. D. Type matters only for the first bracket.

Correct answer: B. A closing bracket must match the most recently opened bracket of the same kind, so the stack must store which bracket was opened.

Counting alone accepts strings such as '(]' that are not balanced, and the stack's top must match the closing kind. Storing the opened symbol makes each closing check exact.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic