Longest Contiguous Stretch of Distinct Characters in a String

Read the full interview experience this question came from →

Quick Overview

A Spotify backend technical-screen coding problem: given a string, find the length of the longest contiguous substring in which no character repeats. It tests careful string scanning, linear-time reasoning, and edge cases such as case sensitivity, ties and empty input.

Longest Contiguous Stretch of Distinct Characters in a String

Company: Spotify

Role: Backend Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Given a string `s`, return the length of the longest substring of `s` in which every character differs from every other character. A substring is a contiguous block of characters of `s`. The empty string counts as a substring, so an empty input has answer 0. Characters are compared exactly as given: an uppercase letter and its lowercase form are different characters, and spaces, digits and punctuation are ordinary characters that can repeat like any other. ### Function Signature ```python def longest_distinct_substring_length(s: str) -> int: ``` ### Rules - Only contiguous substrings count. Picking characters that are not adjacent in `s` does not form a substring. - Return only the length. Several different substrings may share the maximum length; the length is still a single, unique answer. ### Constraints - `0 <= len(s) <= 50000` - Every character of `s` is printable ASCII (character codes 32 through 126): letters, digits, symbols and the space character. - Because there are only 95 such characters, the answer is between 0 and `min(len(s), 95)`. ### Examples **Example 1** ```text Input: s = "streets" Output: 4 ``` The substring "stre" has four distinct characters. Making it one character longer would include a second "e", and no substring of five or more characters is free of repeats. **Example 2** ```text Input: s = "aAbBa" Output: 4 ``` "aAbB" and "AbBa" both have four distinct characters, because "a" and "A" are different. The whole string repeats "a". **Example 3** ```text Input: s = "" Output: 0 ``` The only substring of the empty string is the empty string itself.

Overview: A Spotify backend technical-screen coding problem: given a string, find the length of the longest contiguous substring in which no character repeats. It tests careful string scanning, linear-time reasoning, and edge cases such as case sensitivity, ties and empty input.

Read the full Spotify Backend Engineer interview experience this question came from

Spotify logo
Spotify
Jul 7, 2026
mediumBackend EngineerTechnical ScreenCoding & Algorithms
0
0

Given a string s, return the length of the longest substring of s in which every character differs from every other character.

A substring is a contiguous block of characters of s. The empty string counts as a substring, so an empty input has answer 0. Characters are compared exactly as given: an uppercase letter and its lowercase form are different characters, and spaces, digits and punctuation are ordinary characters that can repeat like any other.

Function Signature

def longest_distinct_substring_length(s: str) -> int:

Rules

  • Only contiguous substrings count. Picking characters that are not adjacent in s does not form a substring.
  • Return only the length. Several different substrings may share the maximum length; the length is still a single, unique answer.

Constraints

  • 0 <= len(s) <= 50000
  • Every character of s is printable ASCII (character codes 32 through 126): letters, digits, symbols and the space character.
  • Because there are only 95 such characters, the answer is between 0 and min(len(s), 95) .

Examples

Example 1

Input: s = "streets"
Output: 4

The substring "stre" has four distinct characters. Making it one character longer would include a second "e", and no substring of five or more characters is free of repeats.

Example 2

Input: s = "aAbBa"
Output: 4

"aAbB" and "AbBa" both have four distinct characters, because "a" and "A" are different. The whole string repeats "a".

Example 3

Input: s = ""
Output: 0

The only substring of the empty string is the empty string itself.