Pergunta de entrevista da empresa Oracle

Problem Solving: Every close bracket has a corresponding open bracket of the same type.

Resposta da entrevista

Sigiloso

21 de dez. de 2024

I wrote a simple algorithm where I declared three variables: parentheses, curlyBraces, and squareBraces, all initialized to 0. I then iterate through each character in the input. For example, if I encounter a left parenthesis '(', I increment the parentheses variable, and similarly for curlyBraces and squareBraces. If I find a closing parenthesis, I decrement the corresponding variable. During the iteration, if any of the variables fall below 0, the algorithm returns false. Finally, if all the variables are equal to 0 at the end of the loop, the algorithm returns true.