Validate parentheses with one or three bracket types
Company: LinkedIn
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates understanding of string parsing and abstract data structures—particularly stack behavior—and the ability to reason about matching and nesting of brackets within the Coding & Algorithms domain.
Valid Parentheses — Three Bracket Types
Constraints
- 0 <= |s| <= 2 * 10^5
- s consists only of the characters '()[]{}'
Examples
Input: "()"
Expected Output: true
Explanation: A single matched pair is valid.
Input: "([{}])"
Expected Output: true
Explanation: All three types correctly nested: {} inside [] inside ().
Input: "([)]"
Expected Output: false
Explanation: Brackets close in the wrong order — ')' tries to close '[', a type mismatch.
Input: ""
Expected Output: true
Explanation: Empty string has no unmatched brackets, so it is valid.
Input: "(]"
Expected Output: false
Explanation: ']' does not match the open '(' on top of the stack.
Input: "{[]}"
Expected Output: true
Explanation: Properly nested mixed types.
Input: "]"
Expected Output: false
Explanation: A closing bracket with nothing open before it is invalid.
Input: "([{}"
Expected Output: false
Explanation: '(' and '[' are never closed, so the stack is non-empty at the end.
Solution
def solution(s):
pairs = {')': '(', ']': '[', '}': '{'}
stack = []
for ch in s:
if ch in '([{':
stack.append(ch)
elif ch in ')]}':
if not stack or stack[-1] != pairs[ch]:
return False
stack.pop()
return len(stack) == 0Time complexity: O(n). Space complexity: O(n).
Hints
- Push every opening bracket onto a stack.
- When you see a closing bracket, the top of the stack must be the matching opening bracket — otherwise the string is invalid.
- After scanning the whole string, the stack must be empty (no unclosed openers).
- A counter is insufficient here: it would accept '([)]' because it can't enforce type-correct nesting order.
Valid Parentheses — Single Bracket Type
Constraints
- 0 <= |s| <= 2 * 10^5
- s consists only of the characters '(' and ')'
Examples
Input: "()"
Expected Output: true
Explanation: One matched pair.
Input: "(()"
Expected Output: false
Explanation: One '(' is never closed; counter ends at 1.
Input: ""
Expected Output: true
Explanation: Empty string is balanced.
Input: ")("
Expected Output: false
Explanation: The leading ')' makes the counter negative — fail fast.
Input: "((()))"
Expected Output: true
Explanation: Fully nested and balanced.
Input: "("
Expected Output: false
Explanation: Single unclosed opener; counter ends at 1.
Input: "())("
Expected Output: false
Explanation: Counter goes negative at the third character.
Solution
def solution(s):
count = 0
for ch in s:
if ch == '(': # opening
count += 1
elif ch == ')': # closing
count -= 1
if count < 0:
return False
return count == 0Time complexity: O(n). Space complexity: O(1).
Hints
- Keep a single integer counter of currently-open '(' brackets.
- Increment on '(', decrement on ')'.
- If the counter ever drops below zero, a ')' appeared with no matching '(' — return False immediately.
- At the end the counter must be exactly zero for the string to be balanced.