Longest streak of active days
Python data engineering interview problem. Difficulty: intermediate. Pattern: Sliding Window. About 15 minutes. Part of the Pro drill bank.
A product's daily active flag is a list of 0/1 (or False/True). Retention wants the longest consecutive streak of active days.
A product team tracks which days a user was active, as a list of 1 (active) and 0 (not active). They want the longest run of active days in a row. Return the length of the longest run of consecutive truthy values (like 1). An empty list returns 0. Write longest_active_streak(flags). In [1, 1, 0, 1, 1, 1] the first run has 2 days and the last has 3, so the answer is 3. An empty list gives 0. The starter file already has the function name and its print lines. Keep them and fill in the body.
Requirements
- Count consecutive active days.
- A 0 ends the current run.
- Return the longest run seen.
- Return 0 for an empty list or when there are no active days.
Constraints
- flags is a list of 0 and 1 (or False and True).
- Only a single pass is needed.
Examples
Input: longest_active_streak([1, 1, 0, 1, 1, 1]) Output: 3 This input follows the stated rules and produces this output.
Topics: lakebench, python, sliding-window.
More Python interview questions · All interview problems · Learn data engineering
Longest streak of active days
Interview-style drill: Given a list of daily activity flags, find the length of the longest streak of active days.
Return the longest run of consecutive active days.