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

  1. A.

    Queue

  2. B.

    Stack

  3. C.

    Tree

  4. 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.

Explore the full course: Niacl Ao It Specialist