Find the bug · Easy

Fix the Bracket Validator

Easy Find the bug Most askedSeen at Amazon, Meta, Google

isValid(s) returns true if every bracket in s ((), [], {}) is closed by the same type of bracket in the right order.

The stack-based code below rejects mismatched brackets correctly, but QA found inputs it accepts that are clearly invalid. Find the bug and fix it.

Example 1

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

Example 2

Input: s = "(("
Output: false
Explanation: The buggy version returns true.

The buggy code

function isValid(s) {
  const pairs = { ")": "(", "]": "[", "}": "{" };
  const stack = [];
  for (const c of s) {
    if (c in pairs) {
      if (stack.pop() !== pairs[c]) return false;
    } else {
      stack.push(c);
    }
  }
  return true;
}

Hints

Hint 1

What is left on the stack after processing "(("?

Find and fix the bug

Loading the editor… (JavaScript is required to run code in your browser.)

Solution and explanation

Show the bug and the fix

Time O(n) · Space O(n)

The bug: the function returns true after the loop without checking whether any opening brackets are still waiting on the stack. Unclosed brackets ("((", "(()") are accepted.

The fix: a string is valid only if the stack is empty at the end: return stack.length === 0.

Fixed version

function isValid(s) {
  const pairs = { ")": "(", "]": "[", "}": "{" };
  const stack = [];
  for (const c of s) {
    if (c in pairs) {
      if (stack.pop() !== pairs[c]) return false;
    } else {
      stack.push(c);
    }
  }
  return stack.length === 0; // every opener must have been closed
}

Common mistakes

  • Popping from an empty stack on a leading closer ("]"): make sure that path returns false rather than throwing.