Longest Substring With At Most Two Distinct Characters
Updated
Problem statement
Given a string s, return the length of its longest contiguous substring containing at most two distinct characters.
A substring must occupy consecutive positions. Repeated occurrences of one character count as one distinct character. An empty string has answer 0.
Function
longestTwoDistinct(s: String) → intExamples
Example 1
s = "eceba"return = 3The substring ece has length 3 and uses only e and c. Every longer substring contains at least three distinct characters.
Example 2
s = "ccaabbb"return = 5The substring aabbb has length 5. Adding either preceding c would introduce a third distinct character.
Constraints
0 <= s.length <= 50000scontains only lowercase English letters.
More ByteDance Phone Screen questions
- Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Implement QuicksortPHONE SCREEN · Seen Dec 2026
- Largest Rectangle in HistogramPHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Reverse Linked ListPHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Count N-Queens PlacementsPHONE SCREEN · Seen Nov 2026
- Matrix EscapePHONE SCREEN · Seen Nov 2026
- Largest Number Below N from Reusable DigitsPHONE SCREEN · Seen Nov 2026
- Log Analyzer: Busiest Service in a Rolling WindowPHONE SCREEN · Seen Nov 2026
- Majority ElementPHONE SCREEN · Seen Nov 2026
- Valid ParenthesesPHONE SCREEN · Seen Nov 2026
Similar questions at other companies
- Fixed Window Average and ModeNuro · PHONE SCREENNuro interview questions
- Rate Limiter with a Sliding Window per ClientStackAdapt · PHONE SCREENStackAdapt interview questions
- Per-Client API Rate LimiterDocker · PHONE SCREEN
- Longest Substring Without Repeating CharactersAngel One · PHONE SCREEN
- Longest Substring Without Repeating CharactersNykaa · PHONE SCREENNykaa interview questions
- Longest Substring Without Repeating CharactersIBM · PHONE SCREENIBM interview questions
- Per-Client Sliding-Window Rate LimiterCVS Health · PHONE SCREEN
- Compact Conversations by Unique ListingsSierra · PHONE SCREENSierra interview questions
- Implement a Rate LimiterRoblox · PHONE SCREENRoblox interview questions
- Message Event AggregationOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
ByteDance interview experiences
- ByteDance TikTok USDS SWE Intern Interview: 1 Experience
- ByteDance MLE New Grad Interviews: 1 Experience
- ByteDance Security SRE Intern Phone Screens: 1 Experience
- ByteDance USDS New Grad SWE Phone Screen: 1 Experience
- ByteDance New Grad Data Engineer Video Interviews: 1 Experience
- ByteDance Trust & Safety Research Scientist Intern: 1 Experience