Pinterest: Fewest subsequences of a source string needed to build a target
Problem. You get a source string and a target string. You may only take subsequences of the source (delete characters, keep the order) and join them together.
Part 1. Decide whether the target can be built at all. It cannot if the target contains a character that the source lacks.
Part 2. Return the minimum number of source subsequences whose concatenation equals the target. For example, source abc and target abcbc need 2 (abc and bc).
How it went. The candidate's first idea was greedy recursion. It had a bug and did not guarantee the minimum. A corrected recursive version passed the tests, but the interviewer then asked for an iterative solution. The candidate settled on greedy two pointers: scan the source repeatedly and advance through the target as far as possible on each pass. Debugging pushed them slightly over time.
What the interviewer cared about. Proving the greedy choice gives the minimum, and an iterative version without recursion.
Comments 0
No comments yet. Ask the author something, or add what happened in your own round.