Quick Overview

Find the longest contiguous substring containing at most two distinct characters. This portable version of the standard referenced problem defines empty input, an ASCII character domain, exact return semantics, and two examples for later cross-language console verification.

Find the Longest Substring with at Most Two Distinct Characters

Company: TikTok

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

# Find the Longest Substring with at Most Two Distinct Characters Implement `longest_two_distinct(s)`. Return the length of the longest contiguous substring of `s` that contains at most two distinct characters. The source identifies the standard problem by its encoded number but gives no interview-specific variant. The character set, empty-input behavior, and size bound below are explicit portable practice assumptions. ## Function Contract `longest_two_distinct(s: str) -> int` ## Constraints - `0 <= len(s) <= 200000` - `s` contains printable ASCII characters. - A substring is contiguous. - Return `0` when `s` is empty. ## Examples ### Example 1 ```text Input: "eceba" Output: 3 ``` The substring `"ece"` has two distinct characters. ### Example 2 ```text Input: "ccaabbb" Output: 5 ``` The substring `"aabbb"` has length 5 and contains only `a` and `b`.

Overview: Find the longest contiguous substring containing at most two distinct characters. This portable version of the standard referenced problem defines empty input, an ASCII character domain, exact return semantics, and two examples for later cross-language console verification.

Read the full TikTok Software Engineer interview experience this question came from

Return the length of the longest contiguous substring of s containing at most two distinct characters. The source identifies the standard problem by its encoded number but gives no interview-specific variant. Printable ASCII input, empty-input behavior, and the size bound are explicit portable practice assumptions. Return 0 for the empty string.

Constraints

  • 0 <= len(s) <= 200,000
  • s contains printable ASCII characters.
  • A substring is contiguous.
  • Return 0 when s is empty.

Examples

Input: ('eceba',)

Expected Output: 3

Explanation: ece is a longest substring with two distinct characters.

Input: ('ccaabbb',)

Expected Output: 5

Explanation: aabbb has length five and uses only a and b.

Hints

  1. Track character counts inside a sliding window.
  2. When a third distinct character appears, advance the left edge until one character count reaches zero.

Loading coding console...

Show the approach

Approach

Maintain a sliding window and a frequency map for its characters. Expand the right boundary one character at a time. Whenever the window has more than two distinct characters, move the left boundary and decrement counts until only two remain. After shrinking, the current window is the longest valid window ending at that right boundary, so updating the maximum over all right boundaries yields the global optimum.

Time complexity:
O(n), where n is len(s).
Space complexity:
O(1) under the fixed printable-ASCII alphabet.