FastPrepLongest Substring with At Most K Distinct Characters
Problems/Longest Substring with At Most K Distinct Characters

Longest Substring with At Most K Distinct Characters

Amazon Web Services logoAmazon Web Services● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported from an Amazon Web Services interview loop for an SDE 1 role in Dublin. In the first onsite round, after behavioral questions, the candidate was given this problem as the first of two coding questions; the second was a different string problem and is not part of this one.

The report names the problem and gives no rules, examples or limits, so we use the usual version: return the length of the longest substring with at most k distinct characters. The limits (up to 5 * 10^4 characters, k from 0 to 50), the rule that s holds only English letters with uppercase and lowercase counted as different, and the examples are our choices.

Our version is about 95% the same as the reported question.

Given a string s and an integer k, return the length of the longest substring of s that contains at most k distinct characters.

A substring is a run of neighbouring characters of s. Uppercase and lowercase letters are different characters, so a and A count as two. When k = 0, no character is allowed, so the answer is 0.

Function

lengthOfLongestSubstringKDistinct(s: String, k: int) → int

Examples

Example 1

s = "eceba"k = 2return = 3

The substring "ece" uses only e and c, so it has 2 distinct characters and length 3. Both substrings of length 4 have too many: "eceb" has three distinct characters and "ceba" has four.

Example 2

s = "aa"k = 1return = 2

The whole string uses one character, a, so the answer is its length, 2.

Example 3

s = "AaAbBbb"k = 2return = 4

Uppercase and lowercase are different characters. "AaA" uses A and a and has length 3, and "bBbb" uses b and B and has length 4. Every longer substring contains at least three of A, a, b and B.

Constraints

  • 1 <= s.length <= 5 * 10^4
  • 0 <= k <= 50
  • s consists of English letters only (lowercase and uppercase).

More Amazon Web Services Onsite Interview questions

  • Group AnagramsONSITE INTERVIEW · Seen Nov 2026
  • Fixed-K Kth Largest StreamONSITE INTERVIEW · Seen Aug 2026
  • Logger Rate LimiterONSITE INTERVIEW · Seen Aug 2026
  • Recent Hit CounterONSITE INTERVIEW · Seen Aug 2026
  • Spreadsheet Formula DependenciesONSITE INTERVIEW · Seen Aug 2026

Similar questions at other companies

  • Longest Substring Without Repeating CharactersOracle · ONSITE INTERVIEWOracle interview questions
  • Longest Substring Without Repeating CharactersSalesforce · ONSITE INTERVIEWSalesforce interview questions
  • Longest Substring Without Repeating CharactersMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
  • Longest Substring Without Repeating CharactersGoogle · ONSITE INTERVIEWGoogle interview questions
  • Longest Substring Without Repeating CharactersAmazon · ONSITE INTERVIEW
  • Longest Substring Without Repeating CharactersInfosys · ONSITE INTERVIEWInfosys interview questions
  • Longest Substring Without Repeating CharactersOmnissa · ONSITE INTERVIEWOmnissa interview questions
  • Longest Substring Without Repeating CharactersBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
  • Longest Substring with At Most K Distinct CharactersGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
  • Longest Substring Without Repeating CharactersBank of America · ONSITE INTERVIEWBank of America interview questions

Amazon Web Services interview experiences

  • AWS New Grad SDE Interview Loops
All Amazon Web Services interview questionsSee Amazon Web Services hiring insights
Saved
class Solution {
    public int lengthOfLongestSubstringKDistinct(String s, int k) {
        // Write your code here.
    }
}
Checking account…