MEDIUMasked at 3 companies

Longest Substring with At Most Two Distinct Characters

A medium-tier problem at 56% community acceptance, tagged with Hash Table, String, Sliding Window. Reported in interviews at eBay and 2 others.

Founder's read

Longest Substring with At Most Two Distinct Characters is a sliding window problem that trips up candidates who overthink it or miss the constraint entirely. eBay, TikTok, and Google ask it. The 56% acceptance rate sits in that danger zone: not beginner, but definitely not hard. You either know the pattern or you don't. The trick is that you're not looking for the longest valid substring directly; you're tracking a moving window and shrinking it only when you violate the constraint. If you hit this during a live OA and blank on the window logic, StealthCoder surfaces a working solution invisibly while you stay calm.

Companies asking
3
Difficulty
MEDIUM
Acceptance
56%

Companies that ask "Longest Substring with At Most Two Distinct Characters"

If this hits your live OA

Longest Substring with At Most Two Distinct Characters is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Made by a working Amazon engineer who got tired of watching qualified friends bomb OAs they'd solve cold in an IDE.

Get StealthCoder
What this means

The problem looks simple until you realize brute force (check every substring) fails at scale. The real solution uses a sliding window with a hash table to track character frequencies inside the window. Expand right, count characters, shrink left when you exceed two distinct characters. The common pitfall is resetting the window wrong or counting characters inefficiently. Another trap: confusing this with "at most K" problems where the logic shifts slightly. You need to know when to move the left pointer and how much, exactly. Most candidates who fail either implement the window incorrectly or get stuck on edge cases like empty strings or single characters. This is exactly where StealthCoder adds value: if you freeze on the window logic during the actual assessment, you get a clean, working implementation in seconds without the proctor seeing it.

Pattern tags

The honest play

You know the problem. Make sure you actually pass it.

Longest Substring with At Most Two Distinct Characters recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Made by a working Amazon engineer who got tired of watching qualified friends bomb OAs they'd solve cold in an IDE. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Longest Substring with At Most Two Distinct Characters interview FAQ

Is this really asked at Google, TikTok, and eBay?+

Yes. All three companies have confirmed asking it. It's a medium-difficulty classic in their string and sliding window pools. It's the kind of problem that shows whether you truly understand window contraction, not just expansion.

What's the main trick I'm missing if I can't solve it?+

You're likely not shrinking the window correctly or you're resetting character counts wrong. The trick is: when you have three distinct characters, shrink from the left until you have two again. Don't re-scan; update counts as you move left. This single mental model unlocks the solution.

How does this relate to the sliding window topic?+

It's the textbook sliding window problem. You maintain a dynamic window that grows right and shrinks left. Hash Table stores character frequencies. This pattern generalizes to any 'at most K distinct' variant. Master this, and a dozen related problems become automatic.

Can I brute force it or will it TLE?+

Brute force checking all substrings is O(n^3) with distinct character counting. It will TLE on large inputs. The sliding window solution is O(n) with O(1) space (at most 2 characters in hash table). That's the difference between pass and timeout.

What edge cases bite people?+

Empty strings, single characters, all same characters, and strings with fewer than two distinct characters total. Also watch off-by-one errors when returning the max length. Code defensively and test with length-one and length-two inputs first.

Want the actual problem statement? View "Longest Substring with At Most Two Distinct Characters" on LeetCode →

Frequency and company-tag data sourced from public community-maintained interview-report repos. Problem, description, and trademark © LeetCode. StealthCoder is not affiliated with LeetCode.