Bloomberg interview question

Given an array, there are some left and right brackets in the array. Write a program to decide if the array is closed. (i.e., all the left brackets can have a right bracket after it to close it).

Interview Answer

Anonymous

26 Oct 2017

The idea is to maintain a stack. Whenever a left bracket is found, push it into the stack; then when a right bracket is found, pop a left bracket from the stack; if the stack is already empty, return false; In the end, if the stack is empty, return true; if not empty, return false.

1