Longest Shared Free Time With One Change
Updated
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[]) → intExamples
Example 1
x = [1,1,1,0,1,0,1,1,1]y = [1,1,1,0,1,1,1,1,1]return = 5Changing 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 = 0One change cannot make both people free at their only slot.
Example 3
x = [1,0,1]y = [1,1,1]return = 3Changing 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
0or1.
More Microsoft Onsite Interview questions
- Bad Request AlertsONSITE INTERVIEW · Seen Nov 2026
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
- Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
- Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
- Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026
- Count Subarrays with Equal Even and Odd CountsONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Rot All OrangesONSITE INTERVIEW · Seen Nov 2026
- Coin ChangeONSITE INTERVIEW · Seen Oct 2026
- Daily USD Totals with Effective-Dated Exchange RatesONSITE INTERVIEW · Seen Oct 2026
- Number Words to Digits in Any LanguageONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Maximum Earnings Across WarehousesAmazon · ONSITE INTERVIEW
- Shortest Window with Required CountsOracle · ONSITE INTERVIEWOracle interview questions
- Count Zero Request ServersSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Longest Subarray Within an Absolute-Difference LimitGoogle · ONSITE INTERVIEWGoogle interview questions
- Longest Binary Subarray With at Most K OnesLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- Longest Continuous Subarray Within an Absolute-Difference LimitUber · ONSITE INTERVIEWUber interview questions
- Sliding-Window Rate LimiterSnowflake · ONSITE INTERVIEWSnowflake interview questions
- Count Equal Code Pairs Within Distance KInfosys · ONSITE INTERVIEWInfosys interview questions
- Maximize Element FrequencyGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
- Longest Subarray with At Most Two Distinct ValuesMoody's Investors Service · ONSITE INTERVIEW
Microsoft interview experiences
- Microsoft OA Questions, October 2026: 4 Experiences
- Microsoft Experienced SWE Phone Screen: 1 Experience
- Microsoft Explore and Student SWE: 12 Experiences (Part 6 of 6)
- Microsoft SWE Intern, Early 2025: 41 Experiences (Part 5 of 6)
- Microsoft SWE Intern, Mid 2025: 42 Experiences (Part 4 of 6)
- Microsoft SWE Intern, Late 2025: 47 Experiences (Part 3 of 6)