Shortest Deletion for a Distinct Remainder
Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Google's September 2026 OA reports include a string problem where the input can hit 10^5 characters, so trying every substring to delete is dead on arrival. That's roughly 5 billion candidates, each needing a distinctness check. The task: remove one contiguous chunk so what's left has no repeated characters, and return the shortest chunk. It's a sliding window problem wearing a disguise. If you blank on the setup, StealthCoder is the invisible safety net running during the live OA. But the idea is small enough to learn tonight.
The problem
Given a case-sensitive printable-ASCII string, delete one contiguous substring so that every remaining character is distinct. Return the minimum number of deleted characters. Deleting zero characters is allowed, and deleting the entire string is also a valid fallback. Function minDeletionForDistinct(text: String) → int Examples Example 1 text = "abca" return = 1 Deleting either occurrence of a leaves three distinct characters. Example 2 text = "abc" return = 0 The original string already contains no repeated character. Constraints 0 <= text.length <= 10^5 text contains printable ASCII characters.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Flip the question. Deleting a substring leaves a prefix and a suffix. Both must be distinct on their own, and they must share no characters. So find the longest distinct prefix, then slide a pointer through suffix starts, shrinking the prefix as needed. Printable ASCII means a count array of 128 slots, so each step is O(1) and the whole thing is O(n). The common pitfall is treating this as longest-unique-substring. That's the wrong target, since you keep the ends, not the middle. Another trap is an off-by-one when the prefix and suffix overlap, or forgetting that deleting everything is valid. Check that the suffix you grow is itself distinct before you ever test the prefix against it. If the pointer logic gets tangled mid-assessment, StealthCoder can hand you the working version while you stay calm. Test on empty input and an already-distinct string.
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 Deletion for a Distinct Remainder 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
You've seen the question.
Make sure you actually pass Google's OA.
Google 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 Deletion for a Distinct Remainder FAQ
What's the trick to the Google minimum deletion problem?+
Think about what you keep, not what you delete. The remainder is a prefix plus a suffix. Both must be internally distinct and share no characters. Use a two-pointer approach with a frequency array, and the answer is the smallest gap between the prefix end and the suffix start.
Why does brute force fail here?+
With length up to 10^5, there are about 5 billion substrings to delete. Each one also needs a distinctness check on what remains. Even an O(n^2) approach is around 10^10 operations. You need linear time, which means one pass with pointers and a count array.
Is this the same as longest substring without repeating characters?+
No, and mixing them up is the classic mistake. That problem keeps a middle window. Here you keep the two ends and remove the middle. The sliding window idea is related, but the window you slide marks what you delete, and the kept parts live outside it.
What edge cases should I test?+
Test the empty string, which returns 0. Test an already-distinct string, which also returns 0. Test a string of one repeated character, where you keep at most one. Test cases where the prefix and suffix share a letter. Also check case sensitivity, since 'a' and 'A' are different characters.
How do I prepare for this in 48 hours?+
Write the solution from scratch twice. First, find the longest distinct prefix with a 128-slot count array. Then add the suffix pointer and shrink logic. Run your own tests on tricky strings like 'abca' and 'aab'. Learn the pattern, not the code, so you can rebuild it under pressure.