PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates proficiency in string processing and algorithmic reasoning for balancing parentheses, testing understanding of invariants and edge-case handling.

  • medium
  • Bytedance
  • Coding & Algorithms
  • Data Scientist

Compute Minimum Parentheses Additions

Company: Bytedance

Role: Data Scientist

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Given a string `s` consisting only of the characters `'('` and `')'`, compute the minimum number of parentheses that must be inserted anywhere in the string so that the resulting string is a valid parentheses string. A valid parentheses string is one where every opening parenthesis has a matching closing parenthesis, every closing parenthesis matches a previous unmatched opening parenthesis, and the pairs are properly nested. Return a single integer: the minimum number of insertions required. Example: ```text Input: s = "()))((" Output: 4 ``` Explain your approach and analyze its time and space complexity.

Quick Answer: This question evaluates proficiency in string processing and algorithmic reasoning for balancing parentheses, testing understanding of invariants and edge-case handling.

Return the minimum number of parentheses insertions needed to make s valid.

Constraints

  • s contains only '(' and ')'

Examples

Input: ('()))((',)

Expected Output: 4

Explanation: Given example.

Input: ('',)

Expected Output: 0

Explanation: Empty string is already valid.

Input: ('(((',)

Expected Output: 3

Explanation: Needs closing parentheses.

Input: (')))',)

Expected Output: 3

Explanation: Needs opening parentheses.

Input: ('()()',)

Expected Output: 0

Explanation: Already valid.

Hints

  1. Count unmatched closing parentheses and unmatched openings.
Last updated: Jun 27, 2026

Loading coding console...

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
  • AI Coding 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.

Related Coding Questions

  • Course Schedule Feasibility - Bytedance (hard)
  • Least Frequently Used (LFU) Cache - Bytedance (hard)
  • SQL: Students Above Average and Passing Every Subject - Bytedance (hard)
  • Determine If One Binary Tree Is a Substructure of Another - Bytedance (hard)
  • Reverse Nodes in K-Sized Groups - Bytedance