← Trexquant Interview Insights
The hint basically gave it away: binary search on k.
Recognize that the removals must be applied in the given order, so the answer is the largest prefix of the removable array that can be removed while p remains a subsequence of s. Use binary search on the number of removals, and for each candidate prefix, check if p is a subsequence of the remaining string in O(n) time. This yields an O((n + m) log m) solution, which is optimal for typical constraints.
Pro tip: Mention that a linear two-pointer check for subsequence is sufficient for each binary search step, and that the monotonic property (if removing k characters works, removing fewer also works) justifies binary search. Also note that if p is not a subsequence of s initially, the answer is 0.
Confirm that removals must be applied in the given order, and that we want the maximum number of removals from the prefix of the removable array. Ask about input sizes to choose the right algorithm.
Observe that if removing the first k characters (in order) keeps p as a subsequence, then removing any smaller number also works. This monotonicity enables binary search on k.
For a given k, mark the first k indices in the removable array as removed, then use a two-pointer scan over s and p to verify if p is still a subsequence of the remaining characters.
Set low = 0, high = length of removable array. While low < high, compute mid, check if removing mid characters works, and adjust bounds accordingly. Return the largest valid k.
State that each check is O(n), and binary search adds a log m factor, giving O((n + m) log m) time and O(n) space for marking removed indices. Handle cases where p is not a subsequence initially or removable array is empty.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.