FastPrepLongest Zero Run Between Ones
Problems/Longest Zero Run Between Ones

Longest Zero Run Between Ones

Adobe logoAdobe● EasyFULLTIMEPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: This problem finds the length of the longest block of zeros in a 0/1 array that has a 1 on each side, with 0 when there is none. An Adobe Computer Scientist 2 report gives this as the second of two questions in its first round, with four example arrays and their answers, which show that zeros at either end do not count; it gives no limits.

Our choices: the array as integers 0 and 1, at most 20000 values, and new examples.

The judged task matches the report at about 95%.

Given an array bits of zeros and ones, find the longest block of consecutive zeros that has a 1 directly before it and a 1 directly after it.

Return its length, or 0 if no such block exists. Zeros at the very start or the very end of the array are not enclosed, because one side has no 1.

Function

longestEnclosedZeros(bits: int[]) → int

Examples

Example 1

bits = [0,1,0,0,1,0,1,0,0,0,0]return = 2

Enclosed runs: two zeros between the 1s at positions 1 and 4, and one zero between positions 4 and 6. The four zeros at the end have no 1 after them, so they do not count. The answer is 2.

Example 2

bits = [1,1,1]return = 0

There are no zeros, so the answer is 0.

Example 3

bits = [0,0,1,0,0,0,1,0]return = 3

The leading two zeros and the last zero are not enclosed. The three zeros between the two 1s are: the answer is 3.

Constraints

  • 1 <= bits.length <= 20000
  • each bits[i] is 0 or 1

More Adobe problems

  • Evaluate a Parenthesized Integer ExpressionPHONE SCREEN · Seen Oct 2026
  • All Elements of Two BSTs in OrderPHONE SCREEN · Seen Aug 2026
  • Rotate an Array In PlaceOA · PHONE SCREEN · Seen Aug 2026
All Adobe interview questionsSee Adobe hiring insights
Saved
public int longestEnclosedZeros(int[] bits) {
    // Write your code here.
}
Checking account…