Longest Substring Without Repeating Characters
Reported by candidates from Benchling's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The whole Benchling question from July 2021 comes down to one data structure: a map from character to its last seen index, paired with a window that only moves forward. If you've got an OA invite and this one is on your radar, it's the classic longest substring with no repeats, plus a follow-up about allowing k repeats. Pattern is sliding window, even though the hint says dynamic-programming. You can write the base solution in about ten lines. The danger is blanking on the pointer update. StealthCoder sits invisibly on your screen as a safety net if that happens during the live OA.
The problem
Given a string s, return the length of its longest contiguous substring that contains no repeated characters. Interview follow-up The report also describes allowing k repeats, without defining what counts as a repeat. For this exercise, assume the discussion variant counts total excess occurrences: sum(max(0,count[c]-1)) <= k. Explain how to maintain that budget in a sliding window. This variant is discussion only; the judged function still requires no repeated character. Function lengthOfLongestSubstring(s: String) → int Examples Example 1 s = "abcabcbb" return = 3 "abc" is a longest substring without repeated characters, so the answer is 3. Example 2 s = "bbbbb" return = 1 Every substring with distinct characters contains at most one b. Constraints 1 <= s.length <= 10^5. s contains English letters, digits, and common symbols.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Keep a left pointer and a hash map of each character's last index. Walk right across the string. If the current character was seen at an index at or after left, jump left to that index plus one. Update the best length as right minus left plus 1. That's O(n) time and O(min(n, alphabet)) space. The classic pitfall is moving left backward. Without the check that the last index is at least left, a stale entry shrinks your window wrongly and gives bad answers. Another is using a set and shrinking one step at a time, which works but is slower to reason about. For the k follow-up, keep a count array and an excess total. When adding a character whose count is already 1 or more, excess goes up by one. While excess exceeds k, remove from the left and decrease excess when that character's count was above 1. If you blank on the live OA, StealthCoder is the hedge.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Longest Substring Without Repeating Characters 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. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as longest substring without repeating characters. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Benchling's OA.
Benchling reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Longest Substring Without Repeating Characters FAQ
What's the trick in Longest Substring Without Repeating Characters?+
Use a sliding window with a hash map storing each character's last seen index. When you hit a repeat inside the window, move the left edge to one past that earlier index. Never move left backward. One pass, O(n) time.
Is the Benchling version harder than the standard one?+
The judged function is the standard problem, so no. The k-repeats follow-up is discussion only. Be ready to explain it, but the code you submit just needs to return the longest length with no repeated character.
How do I handle the k repeats follow-up?+
Track counts per character and a running excess equal to the sum of max(0, count-1). Adding a character that already exists raises excess by one. While excess is above k, shrink from the left, lowering excess when the removed character's count was above one. Then record the window length.
What edge cases should I test?+
Test a single character, a string of all the same character like bbbbb, a string with all distinct characters, and a repeat that sits before the left pointer. That last one catches the stale index bug. Symbols and digits work the same as letters.
How do I prepare for this in 48 hours?+
Write the last-index map version from memory twice, then the count-based version for the k variant. Practice explaining why left never moves backward. That's enough. It's a short, common pattern, and the code is small once the window logic is clear.