CBSE Class 12 Computer Science Question 24 of 63

Data Structures in Python — Question 7

Back to all questions
7
Question

Question 7

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:

  1. 1
  2. 2
  3. 3
  4. 4 or more
Answer

3

Reason — Initially, two opening parentheses '(' are pushed onto the stack. When a closing parenthesis ')' is encountered, it is matched with an open parenthesis on the stack. Subsequently, two new opening parentheses are pushed onto the stack. Then subsequent two closing parentheses are matched with open parentheses on the stack and this process repeats. ensuring that at most, 3 parentheses are on the stack at any given time, maintaining balance.