Shortest Sequence of Source Subsequences
Reported by candidates from Pinterest's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The mistake that sinks a first attempt on this Pinterest OA, reported in September 2026, is restarting the scan of source from index 0 for every single character of target. That turns a linear problem into a timeout. You're given source and target, and you need the fewest subsequences of source that concatenate into target, or -1 if it can't be done. With both strings up to 10^5, the greedy idea is simple but the lookup has to be fast. StealthCoder is the safety net if you blank during the live assessment, but the pattern is short enough to own tonight.
The problem
Form target by concatenating subsequences of source. Return the minimum number of source subsequences required, or -1 when impossible. An empty target requires zero subsequences. Function shortestSourceSubsequences(source: String, target: String) → int Examples Example 1 source = "abc" target = "abcbc" return = 2 Use abc followed by bc. Example 2 source = "abc" target = "acdbc" return = -1 The character d never occurs in source. Example 3 source = "xyz" target = "" return = 0 No subsequence is needed. Constraints 1 <= source.length <= 10^5. 0 <= target.length <= 10^5. Both strings contain lowercase English letters.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is greedy matching with a pointer into source. Walk through target and match each character as far forward in source as possible. When you can't find the next character before source ends, start a new pass and increment the count. The naive version rescans source linearly for each target character, which can hit O(n*m) and die on 10^5 inputs. Fix it by precomputing, for each index and each letter, the next position of that letter in source. That's a 26-wide table, so each target character resolves in O(1). Check feasibility first: if any target letter is missing from source, return -1. Empty target returns 0. The common pitfall is an off-by-one when wrapping to a new pass, so the same character can get matched twice at one index. If you freeze on the live OA, StealthCoder can hand you the working table-based solution while you keep typing calmly.
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 Sequence of Source Subsequences 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 Sequence of Source Subsequences FAQ
What's the trick to Shortest Sequence of Source Subsequences?+
Greedy matching. Advance through source matching target characters in order. When the next target character isn't available ahead of your pointer, begin a new pass over source and add one to the count. Each pass is one subsequence. Return -1 if a letter is absent from source entirely.
How do I avoid a timeout on 10^5 length strings?+
Don't rescan source for each target character. Build a next-occurrence table: for every index, store the next position of each of the 26 letters. Then each target character is an O(1) jump. Total work is O(26n + m), which fits comfortably under these constraints.
Is a two-pointer solution good enough?+
A plain two-pointer works and is O(n*m) worst case if you restart carelessly. Done right, with the pointer only moving forward and resetting once per new subsequence, it can be fine. But worst-case inputs like long source with a repeating target can hurt. The precomputed table or per-letter index lists with binary search are safer.
What edge cases should I test before submitting?+
Empty target returns 0. A target letter missing from source returns -1, like the d in example 2. Single-character source with a long target of that same letter needs one pass per character. Also test target equal to source, which should return 1.
How do I prepare for this in 48 hours?+
Write the solution twice from scratch. First the simple pointer version, then the next-occurrence table version. Run the three given examples plus a missing-letter case and a repeated-letter case. This is a standard subsequence-greedy pattern, so two clean runs is enough to recognize it fast.