The best data structure to check whether an arithmetic expression has balanced…
2017
The best data structure to check whether an arithmetic expression has balanced parenthesis is a
- A.
Queue
- B.
Stack
- C.
Tree
- D.
List
Attempted by 582 students.
Show answer & explanation
Correct answer: B
The best data structure for checking balanced parentheses is a Stack. Stacks follow the Last-In-First-Out (LIFO) principle, which is essential for matching opening brackets with their corresponding closing brackets. When an opening bracket is encountered, it is pushed onto the stack; when a closing bracket appears, the top element is popped and compared. If the stack is empty at the end of the expression, the parentheses are balanced.
A video solution is available for this question — log in and enroll to watch it.