Reported July 2026
Googlesliding window

Longest Target Run After Replacements

Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Google OA. Under 2s to a working solution.
Founder's read

Google reported this one in July 2026, and it's a classic staircase: three parts that get harder, with the last one hiding a sliding window under a pile of array bookkeeping. If you've got an OA invite, expect the final form. Find the longest contiguous run of target after changing at most maxChanges other values. The pattern is a sliding window with a count of non-target elements. The hint says dynamic programming, but you don't need a DP table. If you blank on the window logic mid-assessment, StealthCoder runs invisibly on your desktop and gives you the solution so you can keep moving.

The problem

You are given an integer array nums, an integer target, and an integer maxChanges.
You may choose at most maxChanges elements whose value is not target and change each chosen element to target.
Return the maximum possible length of a contiguous subarray that can contain only target after performing at most maxChanges changes. If no non-empty target-only subarray can be formed, return 0.
Original Interview Report
Onsite 1 was an array problem that became harder step by step. Given an original array:
Given a number n, return the longest consecutive length where n appears.
Given a target array such as [n, m, k], return each target number's longest consecutive occurrence count in the original array.
Given a number n and an integer k, you may change at most k numbers into n. Return the longest consecutive length of n after the changes.
The third part felt like a sliding-window problem, but the idea was not immediately obvious at first; it was derived from the lower-level reasoning.

Function
longestTargetRunAfterReplacements(nums: int[], target: int, maxChanges: int) → int

Examples
Example 1
nums = [1,2,1,1,3,1,2]
target = 1
maxChanges = 1
return = 4
Change the 2 at index 1 to 1. Then indices 0 through 3 form a contiguous block of four 1s.
Example 2
nums = [2,2,1,2,3,2,2]
target = 2
maxChanges = 2
return = 7
The array contains exactly two values that are not 2. Change both of them to 2, and the whole array becomes one contiguous target run.
Example 3
nums = [4,4,5,4,6,4,4,4]
target = 4
maxChanges = 1
return = 5
Change the 6 at index 4 to 4. Then indices 3 through 7 form a contiguous block of five 4s.

Constraints
1 <= nums.length <= 200,000
-10^9 <= nums[i], target <= 10^9
0 <= maxChanges <= nums.length

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick: a window is valid if the number of non-target elements inside it is at most maxChanges. Expand the right pointer, add one to a bad counter when nums[right] != target, and while bad exceeds maxChanges, move left forward and subtract if you leave a non-target. Track the max window size. That's O(n) time and O(1) space, which matters at 200,000 elements. The edge case that breaks naive solutions: maxChanges = 0, where the answer is just the longest existing run, and the case where target never appears but maxChanges is positive. There the answer is min(maxChanges, n), since you can convert any elements. Don't shrink the window by counting target values, count the bad ones. Another pitfall is rebuilding counts per window, which turns it into O(n^2). If the window shrink logic slips under pressure, StealthCoder is your hedge during the live OA.

StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.

If this hits your live OA

You can drill Longest Target Run After Replacements 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. If you're reading this with an OA window open, you're who this was built for.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as max consecutive ones iii. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Google's OA.

Google reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Longest Target Run After Replacements FAQ

What's the trick to Longest Target Run After Replacements?+

Treat it as a sliding window where the budget is maxChanges. Count non-target elements inside the window. If that count exceeds maxChanges, advance the left pointer until it's valid again. The best window length seen is your answer. One pass, constant extra space.

Do I actually need dynamic programming here?+

No. The pattern hint says dynamic programming, but a two-pointer sliding window solves it in O(n). Because the changes are free choices inside a contiguous block, you only need the count of bad elements, not any stored subproblem states.

What edge cases should I test first?+

Test maxChanges = 0, where you just return the longest existing run of target. Test when target isn't in the array, where the answer is min(maxChanges, n). Test maxChanges equal to the array length, which returns n. Also try a single-element array.

How hard is this really for a Google OA?+

Medium. The window idea is familiar if you've seen the replace-characters style problems, but the report says it wasn't immediately obvious. The earlier parts, longest run for one value and for several targets, are easy warm-ups. The third part is where people stall.

How do I prepare in 48 hours?+

Write the sliding window template from memory twice, one with a bad-element counter. Then run your code on the three given examples and the edge cases like maxChanges = 0. Check that you're using a while loop for shrinking, and that the window length is right - left + 1.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Google.

OA at Google?
Invisible during screen share
Get it