The data structure required to check whether an expression contains a balanced…
2024
The data structure required to check whether an expression contains a balanced parenthesis is
- A.
Queue
- B.
Stack
- C.
Tree
- D.
Array
Attempted by 175 students.
Show answer & explanation
Correct answer: B
To check balanced parentheses, scan the expression from left to right. Push every opening parenthesis onto a stack. For each closing parenthesis, pop the most recent opening parenthesis and check that it matches. This last-in-first-out behavior is exactly what a stack provides, so the required data structure is a stack.