Shortest Way to Form a Target String
Reported by candidates from Pinterest's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Pinterest reported this one in September 2026, and the input size is the first thing to read. Both strings can hit 100000 characters, so any approach that tries every subsequence of source is dead on arrival. This is Shortest Way to Form a Target String, a string problem with a greedy core. You have a source, a target, and you need the fewest subsequences of source that glue together into target, or -1 if it can't be done. If you blank during the live OA, StealthCoder sits invisibly on your screen as a safety net. Know the trick first and you probably won't need it.
The problem
You may choose any subsequence of source, preserving character order, and concatenate any number of chosen subsequences. Return the minimum number of source subsequences whose concatenation equals target. Return -1 when formation is impossible. Function shortestWay(source: String, target: String) → int Examples Example 1 source = "abc" target = "abcbc" return = 2 Use abc and then bc. Example 2 source = "abc" target = "acdbc" return = -1 The character d never appears in the source. Example 3 source = "xyz" target = "xzyxz" return = 3 One optimal decomposition is xz, y, and xz. Constraints 1 <= source.length, target.length <= 100000. Both strings contain lowercase English letters.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is greedy. Walk through target one character at a time and scan source forward with a pointer, matching as many target characters as you can in a single pass. When the source pointer runs off the end, count one more subsequence and restart the source pointer at zero. Greedy works because taking the earliest match never hurts later matches. Before any of that, check that every target character exists in source, otherwise return -1 immediately. That check is also how you avoid an infinite loop. The naive two-pointer rescan is O(source * target) in the worst case, which at 100000 each is too slow. Fix it by precomputing, for each position and letter, the next index of that letter in source, then binary search or table lookup per target character. The common pitfall is forgetting the impossible case or resetting the pointer wrongly. StealthCoder is your hedge on the live OA if the next-index table slips your mind.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Shortest Way to Form a Target String cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as shortest way to form string. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Pinterest's OA.
Pinterest reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Shortest Way to Form a Target String FAQ
What's the trick for Shortest Way to Form a Target String?+
Greedy matching. Scan source with a pointer and consume target characters in order. When source runs out, start a new pass and add one to the count. Taking the earliest possible match is always safe. Check first that every target letter appears in source, or return -1.
Why does brute force fail with these constraints?+
Source and target can each be 100000 characters long. Enumerating subsequences is exponential, and even a naive rescan of source for every target character can approach 10^10 operations. You need roughly linear or n log n work, using a next-occurrence table or per-letter index lists with binary search.
How do I make it fast enough?+
Store a sorted list of positions for each of the 26 letters in source. For each target character, binary search for the first position after your current pointer. If none exists, wrap to a new subsequence and take the first position. That gives O(target log source) total.
When do I return -1?+
Return -1 when any character in target never appears in source. Do the check up front with a set of source letters, or detect it mid-scan when a fresh pass from index zero still finds no match. Skipping this causes an infinite loop.
How do I prepare for this in 48 hours?+
Write the two-pointer greedy once from scratch, then upgrade it to the per-letter index lists with binary search. Test it on the three examples, especially xyz with xzyxz returning 3. Also hand-check one impossible case and one where target is entirely inside source.