PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Meta

Compute unmatched parentheses length

Last updated: Mar 29, 2026

Quick Overview

This question evaluates string-processing skills and understanding of parentheses matching and cancellation, focusing on linear-time sequence analysis and edge-case handling.

  • medium
  • Meta
  • Coding & Algorithms
  • Software Engineer

Compute unmatched parentheses length

Company: Meta

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

## Problem You are given a string `s` consisting only of characters `'('` and `')'`. Repeatedly remove any **matched pair** of parentheses as part of a valid parentheses matching (equivalently, cancel out all valid matches). After all possible matches are removed, return the **number of remaining characters** (the count of unmatched parentheses). ### Examples - `s = "()"` → all matched → return `0` - `s = "())"` → one pair matched, one `')'` unmatched → return `1` - `s = "(()"` → one pair matched, one `'('` unmatched → return `1` - `s = ")()("` → two matched in the middle, ends unmatched → return `2` ### Function signature - Input: `s: string` - Output: `int` ### Constraints - `0 <= len(s) <= 10^6` (design for linear time) ### Notes - You only need to return the number of unmatched parentheses, not the corrected string.

Quick Answer: This question evaluates string-processing skills and understanding of parentheses matching and cancellation, focusing on linear-time sequence analysis and edge-case handling.

Related Interview Questions

  • Find Shortest Unique Prefixes - Meta (medium)
  • Compute Exclusive Execution Times - Meta (medium)
  • Solve Tree Columns And Maze Variants - Meta (medium)
  • Solve Tree Diameter and Palindromic Counts - Meta (medium)
  • Simulate Monster Team Battles - Meta (hard)
Meta logo
Meta
Jan 18, 2026, 12:00 AM
Software Engineer
Onsite
Coding & Algorithms
3
0
Loading...

Problem

You are given a string s consisting only of characters '(' and ')'.

Repeatedly remove any matched pair of parentheses as part of a valid parentheses matching (equivalently, cancel out all valid matches). After all possible matches are removed, return the number of remaining characters (the count of unmatched parentheses).

Examples

  • s = "()" → all matched → return 0
  • s = "())" → one pair matched, one ')' unmatched → return 1
  • s = "(()" → one pair matched, one '(' unmatched → return 1
  • s = ")()(" → two matched in the middle, ends unmatched → return 2

Function signature

  • Input: s: string
  • Output: int

Constraints

  • 0 <= len(s) <= 10^6 (design for linear time)

Notes

  • You only need to return the number of unmatched parentheses, not the corrected string.

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Meta•More Software Engineer•Meta Software Engineer•Meta Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.