PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches

Quick Overview

This collection evaluates proficiency with fundamental data structures and algorithmic techniques — including matrix and graph traversal, hash-based sets for O(1) operations, linked list manipulation, sliding-window strategies, recursive processing of nested structures, and stack/string handling — along with complexity analysis and reliable implementation. Commonly asked in the Coding & Algorithms domain, these problems assess pattern recognition, algorithmic reasoning, and practical implementation skills under time and space constraints, emphasizing applied problem-solving over purely theoretical concepts.

  • Medium
  • Meta
  • Coding & Algorithms
  • Software Engineer

Solve diverse LeetCode algorithm problems

Company: Meta

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Technical Screen

##### Question LeetCode 695. Max Area of Island LeetCode 380. Insert Delete GetRandom O( 1) LeetCode 19. Remove Nth Node From End of List LeetCode 1004. Max Consecutive Ones III LeetCode 339. Nested List Weight Sum LeetCode 1249. Minimum Remove to Make Valid Parentheses https://leetcode.com/problems/max-area-of-island/description/ https://leetcode.com/problems/insert-delete-getrandom-o1/description/ https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/ https://leetcode.com/problems/max-consecutive-ones-iii/description/ https://leetcode.com/problems/nested-list-weight-sum/description/ https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/description/

Quick Answer: This collection evaluates proficiency with fundamental data structures and algorithmic techniques — including matrix and graph traversal, hash-based sets for O(1) operations, linked list manipulation, sliding-window strategies, recursive processing of nested structures, and stack/string handling — along with complexity analysis and reliable implementation. Commonly asked in the Coding & Algorithms domain, these problems assess pattern recognition, algorithmic reasoning, and practical implementation skills under time and space constraints, emphasizing applied problem-solving over purely theoretical concepts.

Given a string s containing only lowercase English letters and the characters '(' and ')', delete the minimum number of parentheses so the resulting string is a valid parentheses string. A string is valid if, when considering only parentheses, at no point does the number of ')' exceed the number of '(', and the total counts of '(' and ')' are equal. Return the unique string produced by removing every ')' that is unmatched during a single left-to-right scan, then removing any remaining unmatched '(' from right to left. Letters must retain their original relative order.

Constraints

  • 1 <= len(s) <= 100000
  • s consists of lowercase English letters and parentheses '(' and ')'
  • Return the unique string produced by deleting all unmatched ')' (found during a left-to-right scan) and then deleting any remaining unmatched '(' (from right to left)
  • Expected time complexity O(n) and space complexity O(n)

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: (ab(c)d

Expected Output: ab(c)d

Input: a(b))c)d(

Expected Output: a(b)cd

Input: abc)def(

Expected Output: abcdef

Solution

def min_remove_to_make_valid(s: str) -> str:
    stack = []  # indices of '('
    remove = set()  # indices to delete

    for i, ch in enumerate(s):
        if ch == '(':  # push index of '('
            stack.append(i)
        elif ch == ')':
            if stack:
                stack.pop()  # match with a previous '('
            else:
                remove.add(i)  # unmatched ')'

    # Remaining '(' are unmatched
    for idx in stack:
        remove.add(idx)

    if not remove:
        return s

    res = []
    for i, ch in enumerate(s):
        if i not in remove:
            res.append(ch)
    return ''.join(res)
Explanation
Scan the string once, using a stack to store indices of '(' encountered. For each ')', if there is a '(' in the stack, pop it (forming a match); otherwise, mark this ')' as unmatched. After the scan, any indices remaining in the stack correspond to unmatched '('. Remove all marked indices and rebuild the string. This removes exactly the unmatched parentheses and yields a valid string with the minimum number of deletions.

Time complexity: O(n). Space complexity: O(n).

Hints

  1. Track indices of '(' using a stack.
  2. When you see ')', if the stack is empty mark this index for removal, otherwise pop a '(' index.
  3. After scanning, any indices left in the stack are unmatched '(' and should be removed.
  4. Build the answer by skipping all marked indices.
Last updated: Mar 29, 2026

Loading coding console...

PracHub

Master your tech interviews with 7,500+ 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.

Related Coding Questions

  • Solve Two Backtracking Array Problems - Meta (hard)
  • Solve Array, Matrix, and Recommendation Problems - Meta (medium)
  • Find a String Containing Another - Meta (medium)
  • Solve Subarray Sum and Local Minimum - Meta (hard)
  • Validate abbreviations and brackets - Meta (medium)