Solve diverse LeetCode algorithm problems
Company: Meta
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Technical Screen
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.
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
Time complexity: O(n). Space complexity: O(n).
Hints
- Track indices of '(' using a stack.
- When you see ')', if the stack is empty mark this index for removal, otherwise pop a '(' index.
- After scanning, any indices left in the stack are unmatched '(' and should be removed.
- Build the answer by skipping all marked indices.