As reported: Amazon SDE 2 loop (June 2025), round 2 — DSA, face to face, with two Leadership Principle questions: the first of two problems (the second was Maximum Profit in Job Scheduling); the hint given was to "think in a window way". Also asked in an Amazon SDE2 loop (Hyderabad, 2025), round 5 — bar raiser, after a stepping-numbers question.
Given a binary array nums and an integer k, return the maximum number of consecutive 1s in the array if you can flip at most k 0s.
This is LeetCode 1004 — Max Consecutive Ones III.
Example 1
nums = [1,1,1,0,0,0,1,1,1,1,0], k = 2
Output: 6
Flip the last two 0s of the middle block: [1,1,1,0,0,1,1,1,1,1,1].
Example 2
nums = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], k = 3
Output: 10
Constraints: 1 ≤ nums.length ≤ 10⁵; nums[i] is 0 or 1; 0 ≤ k ≤ nums.length.
Code runner format: one JSON value per line, in order: nums, k (LeetCode's test-case format). Implement longestOnes(nums, k); the harness prints the result.