Quick Overview

Find the longest contiguous run of ones in a binary array under an explicit no-flips assumption. The prompt fixes empty-input behavior, a portable scalar return value, and two examples for later four-language console verification.

Find the Longest Consecutive Run of Ones

Company: Molocoads

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

# Find the Longest Consecutive Run of Ones Implement `longest_ones(nums)`. Given a binary array, return the length of its longest contiguous run containing only `1` values. This prompt assumes no bit flips are allowed because the source does not specify a flip budget. ## Function Contract `longest_ones(nums: list[int]) -> int` ## Constraints - `0 <= len(nums) <= 1000000` - Every element is either `0` or `1`. - Return `0` for an empty array or an array containing no `1`. ## Examples ### Example 1 ```text Input: [1, 1, 0, 1, 1, 1] Output: 3 ``` The final three elements form the longest run. ### Example 2 ```text Input: [0, 0, 1, 1, 0, 1] Output: 2 ``` The two adjacent ones in the middle are longer than the final singleton.

Overview: Find the longest contiguous run of ones in a binary array under an explicit no-flips assumption. The prompt fixes empty-input behavior, a portable scalar return value, and two examples for later four-language console verification.

Read the full Molocoads Machine Learning Engineer interview experience this question came from

Given a binary array nums, return the length of its longest contiguous run containing only 1 values. No bit flips are allowed. Return 0 for an empty array or an array containing no 1.

Constraints

  • 0 <= nums.length <= 1,000,000
  • Every element is the integer 0 or 1.
  • No bit flips are permitted.
  • Return 0 when nums is empty or contains no 1.

Examples

Input: ([],)

Expected Output: 0

Explanation: The empty array has no run of ones.

Input: ([0],)

Expected Output: 0

Explanation: A single zero has no run of ones.

Hints

  1. Keep the length of the run ending at the current element.
  2. A zero ends the current run, while the maximum found so far remains unchanged.

Loading coding console...