MEDIUMasked at 6 companies

Longest Substring with At Most K Distinct Characters

A medium-tier problem at 49% community acceptance, tagged with Hash Table, String, Sliding Window. Reported in interviews at AppDynamics and 5 others.

Founder's read

Longest Substring with At Most K Distinct Characters hits your online assessment and you blank on the window bounds. The problem asks you to find the longest contiguous substring containing at most K unique characters. It's a medium-difficulty sliding window classic reported by Microsoft, Goldman Sachs, Coupang, and others. About 50% of candidates pass it on first attempt. The trap is thinking you need to preprocess or use a fancy data structure. You don't. But if you freeze mid-OA, StealthCoder surfaces the working solution invisible to the proctor.

Companies asking
6
Difficulty
MEDIUM
Acceptance
49%

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

If this hits your live OA

Longest Substring with At Most K 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. Built by a senior engineer who knows the OA is theater. This is the script.

Get StealthCoder
What this means

The pattern is textbook sliding window with a hash table counter. Expand the right pointer, add characters, track distinct count. When distinct count exceeds K, shrink from the left until you're back under the limit. The trick most candidates miss: you're not finding the longest substring of exactly K characters, but at most K. This means you don't reset when you hit K. Instead, you keep the window valid by contracting only when you have to. Common failure mode is moving left pointer incorrectly or forgetting to update the character frequency map as you shrink. The hash table stores character-count pairs, letting you detect when a character leaves the window in O(1). Sliding Window, Hash Table, and String manipulation are the core topics. If this problem shows up live and you're stuck on the contraction logic, StealthCoder gives you the exact pointer moves and counter updates in seconds.

Pattern tags

The honest play

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

Longest Substring with At Most K 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. Built by a senior engineer who knows the OA is theater. This is the script. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Longest Substring with At Most K Distinct Characters interview FAQ

Is this still asked at big tech companies?+

Yes. Microsoft, Goldman Sachs, Coupang, and others have reported it. It's not a trendy problem, but it's a reliable sliding-window checkpoint in OA pipelines. If you see it, they're testing whether you can manage two pointers and a hash table under pressure.

What's the main trick I'm missing if I get this wrong?+

Most candidates expand right correctly but shrink left wrong. They either shrink too much (losing a valid window), or they fail to update the frequency map as characters exit. The rule is simple: decrement the count when a character leaves, remove it from the hash table only when its count hits zero.

How does this relate to the Sliding Window topic?+

This is the canonical two-pointer sliding window problem with a constraint (at most K distinct). You'll see variations across interviews. Master the pattern here: expand, count, shrink to restore validity, track the maximum. The exact same logic applies to K-length subarrays and frequency-based windows.

Can I solve this without a hash table?+

Not efficiently. Without a hash table to track character frequencies in O(1), you'd need to recount characters every time you contract, tanking performance to O(n^2). The hash table lets you maintain the count in O(1) as the window moves.

What's the time and space complexity I should aim for?+

O(n) time and O(K) space, where n is the string length. Each character is visited at most twice (once by right pointer, once by left). The hash table stores at most K+1 distinct characters before the window shrinks. Both are tight bounds.

Want the actual problem statement? View "Longest Substring with At Most K 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.