Longest Binary Window with at Most K Ones
Updated
Problem statement
Given a binary array nums and a nonnegative integer k, return the length of the longest contiguous subarray containing at most k ones.
The limit counts values equal to 1. Zeros do not use that limit. Return 0 for an empty array or when no nonempty subarray is valid.
Function
longestBinaryWindow(nums: int[], k: int) → intExamples
Example 1
nums = [0,1,0,0,1,0,1]k = 1return = 4The subarray at indices 2 through 5 is [0,0,1,0]. It has one 1 and length 4. Every subarray of length 5 has at least two ones.
Example 2
nums = [1,0,0,1,0]k = 0return = 2With no ones allowed, the longest run of zeros is [0,0], of length 2.
Example 3
nums = []k = 0return = 0The empty array has no nonempty subarray, so the result is 0.
Constraints
0 <= nums.length <= 100000.- Every
nums[i]is0or1. 0 <= k <= nums.length.
More LinkedIn Onsite Interview questions
- Explore an Unknown Grid and Find a PathONSITE INTERVIEW · Seen Nov 2026
- Fire Station Response BillONSITE INTERVIEW · Seen Nov 2026
- Roll a Weighted DieONSITE INTERVIEW · Seen Oct 2026
- Longest Binary Subarray With at Most K OnesONSITE INTERVIEW · Seen Oct 2026
- Kth Closest Element in a BSTONSITE INTERVIEW · Seen Oct 2026
- LRU Cache OperationsONSITE INTERVIEW · Seen Oct 2026
- Lexicographically Smallest Course OrderONSITE INTERVIEW · Seen Oct 2026
- K Closest Values in a Binary Search TreeONSITE INTERVIEW · Seen Oct 2026
- Unique Single-Use Sum CombinationsONSITE INTERVIEW · Seen Oct 2026
- Maximum Consecutive Ones in a Circular ArrayONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Shortest Window with Required CountsOracle · ONSITE INTERVIEWOracle interview questions
- Trim a Telemetry Buffer to a Target SumAmazon · ONSITE INTERVIEW
- Count Hotspot Playback WindowsGoogle · ONSITE INTERVIEWGoogle interview questions
- Subarray with a Given SumExpedia · ONSITE INTERVIEWExpedia interview questions
- Subarray Product Less Than KApple · PHONE SCREENApple interview questions
- Subarray Product Less Than KAngel One · PHONE SCREEN
- Subarray Product Less Than KThe D.E. Shaw Group · PHONE SCREENThe D.E. Shaw Group interview questions
- Count Subarrays With Product Equal to KSalesforce · OASalesforce interview questions
- Max Consecutive Ones IIINykaa · PHONE SCREENNykaa interview questions
- Maximum Consecutive Sum Within a ThresholdIndeed · OAIndeed interview questions