Skip to content
LakeBench
ProblemsCommunityPricing
Sign inStart practicing

Longest Substring K Distinct

Python data engineering interview problem. Difficulty: intermediate. Pattern: Sliding Window. About 14 minutes. Part of the Pro drill bank.

Longest substring with at most k distinct characters. Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.

Implement length_k_distinct(s: str, k: int) -> int. ("ece").

Requirements

  • Max length with <= k distinct chars.

Constraints

  • k may be 0 (then answer is 0).
  • Lowercase letters.

Examples

Input: length_k_distinct('eceba', 2) Output: 3 ece uses two distinct letters.

Topics: lakebench, python, window, k-distinct.

More Python interview questions · All interview problems · Learn data engineering

intermediate

Longest Substring K Distinct

Interview-style drill: Longest substring with at most k distinct characters.

Implement `length_k_distinct(s: str, k: int) -> int`. Example: `"eceba", 2` → `3` (`"ece"`). Keep the harness.