FastPrepValid Parentheses
Problems/Valid Parentheses

Valid Parentheses

Microsoft logoMicrosoft● EasyINTERNFULLTIMEONSITE INTERVIEW
QuestionFunctionExamplesConstraints

Problem statement

Source note: Three Taro (Glassdoor copy) reviews of Microsoft software engineering internship interviews list a bracket-matching question. Only one adds any detail, saying the input is made of the three bracket types.

The reports do not define the rules in detail or give limits. Here a closer must match the most recent opener that is still open and be the same kind, every opener must be closed, the string has 1 to 10^4 bracket characters, and you return true or false.

The judged task matches the reports at about 95%.

Given a string s made only of brackets, return true if it is balanced and false otherwise.

A string is balanced when:

  • every closing bracket closes the most recent opening bracket that is still open, and it is the same kind () closes (, ] closes [, } closes {);
  • no closing bracket appears when nothing is open; and
  • every opening bracket is closed by the end.

Function

isValid(s: String) → boolean

Examples

Example 1

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

The ( inside is closed first, then [, then {, each by its own kind of bracket. The last pair () is closed right away. Every opener is closed in the right order, so the answer is true.

Example 2

s = "([)]"return = false

After ( and [ are opened, the next character is ). The most recent unclosed opener is [, not (, so the string is not balanced, even though each kind appears once as opener and once as closer.

Example 3

s = "(("return = false

Two openers are never closed, so the answer is false.

Constraints

  • 1 <= s.length <= 10^4
  • s consists only of the characters (, ), [, ], {, and }.

More Microsoft problems

  • Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
  • Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
  • Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026
All Microsoft interview questionsSee Microsoft hiring insights
Saved
class Solution {
    public boolean isValid(String s) {
        // Write your code here.
    }
}
Checking account…