Max Consecutive Ones III
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The mistake that sinks a first attempt at this one is trying to actually flip zeros and recount runs. Amazon candidates reported Max Consecutive Ones III in September 2026, and it's a clean sliding window problem dressed up as an array puzzle. You get a binary array and a flip budget k, and you return the longest run of ones you can build. With up to 100000 elements, brute force dies fast. If you blank during the live OA, StealthCoder runs invisibly on your desktop and gives you the window logic in real time. Better to walk in knowing it.
The problem
Given a binary array nums and an integer k, return the maximum number of consecutive ones obtainable by flipping at most k zeros to ones. Function longestOnes(nums: int[], k: int) → int Examples Example 1 nums = [1,1,1,0,0,0,1,1,1,1,0] k = 2 return = 6 Flipping two zeros creates a six-element run. Example 2 nums = [0,0,1,1,1,0,0] k = 0 return = 3 Without flips, the central run has length three. Example 3 nums = [0,0,0] k = 3 return = 3 All three zeros can be flipped. Constraints 0 ≤ nums.length ≤ 100000. Every value in nums is 0 or 1. 0 ≤ k ≤ nums.length.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick: stop thinking about flipping. Ask for the longest subarray containing at most k zeros. Keep a left and right pointer, and a count of zeros inside the window. Move right every step. If the zero count goes above k, move left forward until it drops back to k or below. Track the max window size as you go. That's O(n) time and O(1) space. The common pitfall is shrinking the window with a single if instead of a while, or resetting the window when you hit too many zeros. Also watch the edge cases: an empty array returns 0, k = 0 means plain longest run of ones, and an all-zeros array with k at least its length returns the full length. If the pointer logic slips under pressure, StealthCoder is the hedge that shows you the working version during the OA.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Max Consecutive Ones III cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as max consecutive ones iii. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Amazon's OA.
Amazon reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Max Consecutive Ones III FAQ
What's the trick to Max Consecutive Ones III?+
Reframe it as the longest window with at most k zeros. You never modify the array. Expand the right pointer, count zeros, and shrink from the left whenever the count exceeds k. The answer is the biggest window you ever see.
How hard is this problem really for the Amazon OA?+
It's medium on paper but easy once you recognize the sliding window. The risk is overthinking it with flips and recounting. If you've written one variable-size window before, you can finish it in a few minutes.
Should I use if or while when shrinking the window?+
Use while if you want the standard safe version. After adding a zero, the left pointer may need to move several steps before the zero count is valid again. A single if can work in the non-shrinking window variant, but it's easy to get wrong.
What edge cases should I test?+
Test an empty array (return 0), k = 0 (longest natural run of ones), all zeros with k equal to the length (return full length), and all ones. Example 2 in the problem, with k = 0, is a good check that you aren't flipping anything.
How do I prepare for this in 48 hours?+
Write the two-pointer window from memory three times on variants: at most k zeros, at most k distinct values, and longest substring without repeats. Focus on the invariant you maintain and when you move left. That covers most window questions.