Machine-translated by Google. Questions are authored in English.
Top

Discussion

Consider the usual algorithm for determining whether a sequence of parentheses is balanced.

The maximum number of parentheses that appear on the stack AT ANY ONE TIME when the algorithm analyzes ( ( ) ( ( ) ) ( ( ) ) ) is :

  • A.12
  • B.6
  • C.3
  • D.4

Answer: C

The stack holds at most 3 parentheses.

The algorithm pushes each '(' and pops one for each ')'. The maximum stack size is the deepest nesting. For ( ( ) ( ( ) ) ( ( ) ) ): the outer '(' gives depth 1, the next pairs reach depth 2, and the inner '(' of each '( ( ) )' group reaches depth 3. The stack never holds more than three at once.

12 is the total number of characters, and 6 the number of '(' symbols, neither of which is ever on the stack at the same time. 4 would need one more level of nesting.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.