FastPrepLongest Consecutive Ones Across Distributed Partitions
Problem · Array

Longest Consecutive Ones Across Distributed Partitions

Learn this problem
MediumMoloco logoMolocoFULLTIMEONSITE INTERVIEW

Problem statement

A binary sequence is split into contiguous partitions stored on different machines. You receive the partitions as String[] partitions in their original left-to-right order. Each string contains only 0 and 1; an empty partition is allowed.

Return the length of the longest consecutive run of 1 characters in the global sequence. The global sequence is the concatenation of the partitions in the given order. A run may cross any number of partition boundaries, including empty partitions, but it cannot cross a zero. Do not reorder partitions, and do not change any bit. Return 0 when the global sequence is empty or contains no ones.

For the distributed follow-up, design the solution so each machine can summarize its own partition independently and an ordered reduction can combine those summaries without sending the full raw sequence to one coordinator. The finite judge provides all partitions to one function to verify the same result; it does not simulate a network or failures.

Function

longestDistributedOnes(partitions: String[]) → int

Examples

Example 1

partitions = ["011","1101"]return = 4

The global sequence is 0111101. The first partition ends with two ones and the second begins with two ones, forming a run of four across the boundary.

Example 2

partitions = ["11","","111","001"]return = 5

The first and third partitions contribute two and three consecutive ones. The empty partition between them does not break the run, so its length is five. The two zeros in the last partition stop that run.

Constraints

  • 0 ≤ partitions.length ≤ 100.
  • Each partition contains from 0 through 1000 binary characters.
  • The total number of characters across all partitions is at most 1000.
  • The given partition order is authoritative, including when a partition is empty.
drafts saved locally
public int longestDistributedOnes(String[] partitions) {
    // Write your code here
}
partitions["011","1101"]
expected4
checking account