Loading…
Meta Interview experience
1 round over 3 weeks. No offer, meh experience.
Given a string, remove the minimum number of parentheses to make the string valid.
Examples:
Input: "a)b(c)d"
Output: ab(c)d
Python solution:
def min_remove_to_make_valid(s: str) -> str:
stack = []
to_remove = set()
# identify parentheses to remove
for i, char in enumerate(s):
if char == '(':
stack.append(i)
elif char == ')':
if stack:
stack.pop()
else:
to_remove.add(i)
# add remaining unmatched '(' indices to to_remove
to_remove.update(stack)
# build the result string without the indices in to_remove
result = []
for i, char in enumerate(s):
if i not in to_remove:
result.append(char)
return ''.join(result)
s = "(a(b(c)d)"
print(min_remove_to_make_valid(s)) # outputs => "a(b(c)d)"Know someone interviewing at Meta? Pass it on.
Same company, different candidates.
Share yours while it’s fresh. It takes five minutes, it’s anonymous, and it will help future candidates.