Skip to main content

20. Valid Parentheses

easyAsked at Juniper Networks

Validate bracket nesting using a stack. Juniper asks this because protocol parsers and configuration validators (Junos CLI, YANG models) constantly need to verify that delimiters are properly matched and nested — a fundamental stack-based problem in networking software.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Juniper Networks loops.

  • Glassdoor (2025-Q4)Cited in Juniper SWE intern and new-grad onsite reports as a common stack warm-up.
  • Blind (2025-10)Juniper threads mention Valid Parentheses as a go-to problem for testing stack intuition.

Problem

Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if: open brackets must be closed by the same type of brackets, and open brackets must be closed in the correct order. Every close bracket has a corresponding open bracket of the same type.

Constraints

  • 1 <= s.length <= 10^4
  • s consists of parentheses only '()[]{}'.

Examples

Example 1

Input
s = "()"
Output
true

Explanation: Single matched pair.

Example 2

Input
s = "()[]{}"
Output
true

Explanation: Three separate matched pairs.

Example 3

Input
s = "(]"
Output
false

Explanation: Mismatched bracket types.

Approaches

1. Stack with close-bracket map

Push every open bracket onto a stack. When a close bracket arrives, pop and verify it matches. If the stack is empty at the end, the string is valid.

Time
O(n)
Space
O(n)
function isValid(s) {
  const stack = [];
  const match = { ')': '(', '}': '{', ']': '[' };
  for (const ch of s) {
    if (ch === '(' || ch === '{' || ch === '[') {
      stack.push(ch);
    } else {
      if (stack.length === 0 || stack[stack.length - 1] !== match[ch]) {
        return false;
      }
      stack.pop();
    }
  }
  return stack.length === 0;
}

Tradeoff: O(n) time and space. Clean and canonical. The map-based approach avoids a chain of if/else comparisons and makes the code easy to extend to new bracket types.

Juniper Networks-specific tips

Connect the problem to real Juniper context: Junos configuration files use nested bracket structures (interfaces, routing-instances, policies) that require the same kind of delimiter matching. Mention that you would extend the map if more bracket types were added — showing forward-thinking design. Juniper values protocol correctness, so make sure you handle the empty stack case before popping.

Common mistakes

  • Forgetting to check that the stack is non-empty before popping — causes an index error on inputs like ']'.
  • Not checking stack.length === 0 at the end — a string like '(' passes all pops but leaves an unmatched opener.
  • Using three separate if/else chains instead of a map — harder to maintain and extend.
  • Not handling the empty string — return true (0 unmatched brackets) is correct.

Follow-up questions

An interviewer at Juniper Networks may pivot to one of these next:

  • How would you validate a Junos-style configuration file with nested stanzas?
  • Minimum number of bracket removals to make a string valid (LC 921).
  • What if the input is a stream of characters arriving one at a time?

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why use a map from close to open bracket?

It makes the check declarative: when you see a close bracket, the map tells you what open bracket should be on top of the stack. No long if/else chains.

What does an empty stack at the end signify?

Every open bracket was matched and closed in order. A non-empty stack means at least one open bracket was never closed.

Does order matter?

Yes. '([)]' is invalid because the inner ')' closes '(' before '[' is closed. The stack enforces this LIFO order.

Practice these live with InterviewChamp.AI

Drill Valid Parentheses and other Juniper Networks interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →