FastPrepLongest Shared Free Time With One Change

Longest Shared Free Time With One Change

Microsoft logoMicrosoft● MediumFULLTIMEONSITE INTERVIEW
Learn

Problem statement

Two equally long binary arrays x and y describe consecutive time slots for two people. A value of 1 means free and 0 means busy.

You may change at most one busy bit to free in either array. Return the maximum number of consecutive slots during which both people are free after that optional change. The one change is shared across the two arrays. A slot where both people are busy needs two changes and cannot become shared-free with this budget.

For this exercise, empty schedules return 0. Return the length only; do not return the changed schedule or the interval endpoints.

Function

longestSharedFree(x: int[], y: int[]) → int

Examples

Example 1

x = [1,1,1,0,1,0,1,1,1]y = [1,1,1,0,1,1,1,1,1]return = 5

Changing X at index 5 creates a five-slot shared-free run from indices 4 through 8. Index 3 remains busy for both people.

Example 2

x = [0]y = [0]return = 0

One change cannot make both people free at their only slot.

Example 3

x = [1,0,1]y = [1,1,1]return = 3

Changing X at index 1 makes all three consecutive slots shared-free.

Constraints

  • For this exercise, assume 0 <= x.length = y.length <= 100000.
  • Every entry is 0 or 1.

More Microsoft problems

See Microsoft hiring insights
public int longestSharedFree(int[] x, int[] y) {
    // Write your code here
}
x[1,1,1,0,1,0,1,1,1]
y[1,1,1,0,1,1,1,1,1]
expected5
Checking account…