Coding

Longest Substring with Two Distinct Characters

Find the longest substring with at most two distinct characters.

Input: s = "ccaabbb"

Output: 5

Explanation: The substring "aabbb" contains exactly two distinct characters and has a maximum length of 5.

Was asked at

Practice this question with AI

First session is free - no credit card required.

Go Premium

More interviews, more skills, more success.

Practice More Questions

1 answer published. Last candidate submission on Jun 2 2026, 12:50pm PDT. Interview question asked to Software Engineers interviewing at Marqeta, Bosch, Stoneco and other companies. Original question asked: Longest Substring with Two Distinct Characters. Question last reported by a candidate interviewing at Noon for a Software Engineer interview on Jul 13 2026, 3:54am PDT.