Find closest BST value and remove parentheses
Company: Meta
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Overview: This question evaluates understanding of binary search tree properties and numerical proximity reasoning alongside string parsing and parentheses validation, focusing on data structure manipulation and algorithmic reasoning.
Constraints
- For 'closest_bst': 1 <= number of nodes <= 100000
- Node values are integers in [-1e9, 1e9]
- target is a float or int in [-1e9, 1e9]
- tree is a level-order array with None for missing nodes; root is not None; tree encodes a valid BST
- For 'min_remove': 0 <= len(s) <= 100000
- s contains ASCII characters; only '(' and ')' affect validity; other characters are preserved
- Tie-breaking for 'closest_bst': if two values are equally close to target, return the smaller value
- Follow-up: achieve 'min_remove' without using an explicit stack (two-pass approach is possible)
Examples
Input:
Expected Output: ab(c)d
Hints
- For 'closest_bst', traverse from root using BST property, tracking the best value seen so far.
- Update the best when you find a closer value; on a tie, prefer the smaller value.
- For 'min_remove', use two passes: left-to-right to skip invalid ')', then right-to-left to skip leftover '('.
- Avoid extra data structures for 'min_remove'; a counter and string builders suffice.
- Build the tree from the level-order array using index relationships (2*i+1 and 2*i+2).