Longest Contiguous Substring in Which No Character Repeats

Read the full interview experience this question came from →

Quick Overview

A string problem that asks for the length of the longest contiguous substring in which no character repeats. It tests telling substrings apart from subsequences, exact case-sensitive character handling, and an efficient pass over strings of up to 100,000 printable characters.

Longest Contiguous Substring in Which No Character Repeats

Company: ByteDance

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

Given a string `s`, return the length of the longest contiguous substring of `s` in which no character appears more than once. ### Function Signature ```python def longest_distinct_substring(s: str) -> int: ``` ### Rules - A substring is a contiguous run of characters of `s`. A subsequence that skips characters does not count. - Characters are compared exactly: `'a'` and `'A'` are different, and a space or a punctuation mark is a character like any other. - The empty string has answer `0`; any non-empty string has answer at least `1`. ### Constraints - `0 <= len(s) <= 100000` - Every character of `s` is printable ASCII (codes `32` through `126`). ### Examples **Example 1** ```text Input: s = "abcbdea" Output: 5 ``` The substring `"cbdea"` (positions `2` through `6`) has five different characters. Every longer substring contains `b` twice. **Example 2** ```text Input: s = "abba" Output: 2 ``` `"ab"` and `"ba"` both have length `2`, and every substring of length `3` or more repeats a character. **Example 3** ```text Input: s = "zzzz" Output: 1 ```

Overview: A string problem that asks for the length of the longest contiguous substring in which no character repeats. It tests telling substrings apart from subsequences, exact case-sensitive character handling, and an efficient pass over strings of up to 100,000 printable characters.

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

|Home/Coding & Algorithms/ByteDance
ByteDance logo
ByteDance
Sep 10, 2026
mediumSoftware EngineerOnsiteCoding & Algorithms
0
0

Given a string s, return the length of the longest contiguous substring of s in which no character appears more than once.

Function Signature

def longest_distinct_substring(s: str) -> int:

Rules

  • A substring is a contiguous run of characters of s . A subsequence that skips characters does not count.
  • Characters are compared exactly: 'a' and 'A' are different, and a space or a punctuation mark is a character like any other.
  • The empty string has answer 0 ; any non-empty string has answer at least 1 .

Constraints

  • 0 <= len(s) <= 100000
  • Every character of s is printable ASCII (codes 32 through 126 ).

Examples

Example 1

Input:  s = "abcbdea"
Output: 5

The substring "cbdea" (positions 2 through 6) has five different characters. Every longer substring contains b twice.

Example 2

Input:  s = "abba"
Output: 2

"ab" and "ba" both have length 2, and every substring of length 3 or more repeats a character.

Example 3

Input:  s = "zzzz"
Output: 1

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...