PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

This item evaluates proficiency in string processing and tree/graph algorithms, testing competencies such as handling parentheses validity and computing node distances in binary trees. It is commonly asked in Coding & Algorithms interviews to assess core data structures and algorithmic problem-solving, emphasizing practical application of techniques alongside conceptual understanding.

  • Medium
  • Meta
  • Coding & Algorithms
  • Software Engineer

Solve LeetCode stack and tree problems

Company: Meta

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Technical Screen

##### Question LeetCode 1249. Minimum Remove to Make Valid Parentheses LeetCode 863. All Nodes Distance K in Binary Tree https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/description/ https://leetcode.com/problems/all-nodes-distance-k-in-binary-tree/description/

Quick Answer: This item evaluates proficiency in string processing and tree/graph algorithms, testing competencies such as handling parentheses validity and computing node distances in binary trees. It is commonly asked in Coding & Algorithms interviews to assess core data structures and algorithmic problem-solving, emphasizing practical application of techniques alongside conceptual understanding.

Given a string s consisting of '(', ')', and lowercase English letters, remove the minimum number of parentheses to make the parentheses valid. Letters must remain in their original order; only delete characters. Return the resulting string. A parentheses string is valid if every '(' has a corresponding ')' and the pairs are properly nested. If multiple answers with the minimum number of removals exist, return any one of them.

Constraints

  • 1 <= len(s) <= 100000
  • s contains only '(', ')', and lowercase letters 'a'-'z'
  • Return any valid result with the minimum number of removals
  • Aim for O(n) time and O(n) extra space or better

Examples

Input: lee(t(c)o)de)

Expected Output: lee(t(c)o)de

Input: a)b(c)d

Expected Output: ab(c)d

Input: ))((

Expected Output:

Input: abc

Expected Output: abc

Input: ((a)

Expected Output: (a)

Input: ())()(((

Expected Output: ()()

Hints

  1. Use a stack to store indices of '(' and mark unmatched ')' for removal.
  2. After the first pass, any indices left in the stack are unmatched '('; remove them.
  3. Building the result with a list and joining at the end is more efficient than repeated string concatenation.
Last updated: Mar 29, 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

  • 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)