Length of the Longest Run of One Repeated Character in a String

Quick Overview

Given a lowercase string, return the length of its longest block of consecutive identical characters. This easy string problem from a machine learning engineering internship online assessment tests careful iteration, handling a run that ends at the last character, and telling adjacent repeats apart from scattered ones.

Length of the Longest Run of One Repeated Character in a String

Company: Pinterest

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Online Assessment

Given a string, return the length of its longest run: the longest stretch of consecutive positions that all hold the same character. ### Function Signature ```python def longest_run(s: str) -> int: ``` ### Rules - A run is a non-empty block of consecutive, identical characters, for example `"ccc"` inside `"abccca"`. - Equal characters that are not adjacent never join into one run: `"abab"` consists of four runs of length 1, so its answer is `1`. - Return the length of the longest run as an integer. When several runs share the maximum length, that length is the answer. ### Constraints - `1 <= len(s) <= 10^5` - `s` contains only lowercase English letters `a` to `z`. - The answer is always between `1` and `len(s)`. ### Examples **Example 1** ```text Input: s = "aabbbcc" Output: 3 ``` The runs are `"aa"`, `"bbb"` and `"cc"`; the longest is `"bbb"`. **Example 2** ```text Input: s = "mississippi" Output: 2 ``` The string contains four `s` characters, but they form two separate runs. The runs `"ss"`, `"ss"` and `"pp"` all have length 2, and no run is longer. **Example 3** ```text Input: s = "xyzzzz" Output: 4 ``` The longest run, `"zzzz"`, ends at the last character of the string.

Overview: Given a lowercase string, return the length of its longest block of consecutive identical characters. This easy string problem from a machine learning engineering internship online assessment tests careful iteration, handling a run that ends at the last character, and telling adjacent repeats apart from scattered ones.

Pinterest logo
Pinterest
Oct 14, 2026
easyMachine Learning EngineerOnline AssessmentCoding & Algorithms
1
0

Given a string, return the length of its longest run: the longest stretch of consecutive positions that all hold the same character.

Function Signature

def longest_run(s: str) -> int:

Rules

  • A run is a non-empty block of consecutive, identical characters, for example "ccc" inside "abccca" .
  • Equal characters that are not adjacent never join into one run: "abab" consists of four runs of length 1, so its answer is 1 .
  • Return the length of the longest run as an integer. When several runs share the maximum length, that length is the answer.

Constraints

  • 1 <= len(s) <= 10^5
  • s contains only lowercase English letters a to z .
  • The answer is always between 1 and len(s) .

Examples

Example 1

Input:  s = "aabbbcc"
Output: 3

The runs are "aa", "bbb" and "cc"; the longest is "bbb".

Example 2

Input:  s = "mississippi"
Output: 2

The string contains four s characters, but they form two separate runs. The runs "ss", "ss" and "pp" all have length 2, and no run is longer.

Example 3

Input:  s = "xyzzzz"
Output: 4

The longest run, "zzzz", ends at the last character of the string.