Python interview question: longest subarray with at most k distinct values

MediumSliding window

Problem

categories is the sequence of item categories on a conveyor belt. Return the length of the longest contiguous stretch with at most k distinct categories; an empty belt gives 0.

def longest_k_categories(categories: list[str], k: int) -> int:

Example

Solve it free

Checked against hidden tests, right in your browser. No sign-up needed.

Skills it tests

More interview questions

All questions