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
Answer: B. Stack — 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…
- A.
Queue
- B.
Stack
- C.
Tree
- D.
Array
Attempted by 593 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.
Loading lesson…