Practice mode Data Structures

Which data structure is most suitable for checking whether the brackets in an expression such as {} are balanced?

Correct answer A. Stack

Explanation

Push each opening bracket onto a stack; on a closing bracket, pop and check that it matches the same type. The most recently opened bracket must close first, which is exactly LIFO order. The expression is balanced if every closing bracket matches and the stack is empty at the end. A queue would compare the wrong brackets because it returns the oldest one.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.