The data structure required to check whether an expression contains a balanced parenthesis is?

Asked In: BPSC TRE 3.0

Options

  • A. Array
  • B. Queue
  • C. Stack
  • D. Tree

Correct Answer (Detailed Explanation is Below)

C. Stack

Detailed Explanation

To check balanced parentheses in an expression:

  • Push every opening bracket ( { [ into the stack

  • When a closing bracket ) } ] appears → pop from stack

  • If brackets match correctly and stack is empty at the end → expression is balanced

This works because Stack follows LIFO (Last In, First Out) principle, which perfectly handles nested structures.