FastPrepValid Parentheses
Problems/Valid Parentheses

Valid Parentheses

Indeed logoIndeed● EasyINTERNOA
QuestionFunctionExamplesConstraints

Problem statement

Source note: The report lists Valid Parentheses as the first of two problems in a 75-minute test.

The report gives only the name. Here, the string has 1 to 10^4 characters, all from the three bracket types (), [] and {}. It is valid when every open bracket is closed by the same type, in the right order.

The judged task matches the report at about 75%.

The string s is built from three kinds of brackets: round ( ), square [ ] and curly { }.

Call s well formed if you can split all of its characters into pairs such that:

  • each pair is an opening bracket and, somewhere to its right, a closer of the same kind, and
  • pairs never cross: between the two brackets of any pair, every other bracket has its partner there too.

Report whether s is well formed: true if it is, false if it is not.

Function

isValid(s: String) → boolean

Examples

Example 1

s = "{[()()]}"return = true

The two () pairs close inside [ ], which closes inside { }. Every bracket is closed by the same type, in the right order. The answer is true.

Example 2

s = "{(})"return = false

After {(, the most recent open bracket is (, so the next closer must be ). It is } instead. The answer is false.

Example 3

s = "(()[]"return = false

The inner () and [] pairs match, but the first ( is never closed. The answer is false.

Constraints

  • s has at least 1 and at most 10^4 characters.
  • Only the six bracket characters ()[]{} appear in s.

More Indeed problems

  • Insert into an Unrolled Linked ListPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
  • Course Completion OrderPHONE SCREEN · Seen May 2025
  • Recipes You Can PreparePHONE SCREEN · Seen May 2025
All Indeed interview questionsSee Indeed hiring insights
Saved
class Solution {
    public boolean isValid(String s) {
        // Write your code here.
    }
}
Checking account…