FastPrepLongest Target Run After Replacements
Problems/Longest Target Run After Replacements

Longest Target Run After Replacements

Google logoGoogle● MediumNEW GRADONSITE INTERVIEW

Updated Sep 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Backed by a real Google onsite interview report. Estimated match rate: 80-90% for the core task. The original poster said the interview was completed in the first half of 2026, but did not share the exact interview date. I saw the report on 2026-07-09, so FastPrep uses that date as the last-seen date.

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:

  1. Given a number n, return the longest consecutive length where n appears.
  2. Given a target array such as [n, m, k], return each target number's longest consecutive occurrence count in the original array.
  3. 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 = 1maxChanges = 1return = 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 = 2maxChanges = 2return = 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 = 4maxChanges = 1return = 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

More Google Onsite Interview questions

  • Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
  • Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
  • Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
  • Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
  • Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
  • Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026

Similar questions at other companies

  • Shortest Window with Required CountsOracle · ONSITE INTERVIEWOracle interview questions
  • Longest Binary Window with at Most K OnesLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
  • Trim a Telemetry Buffer to a Target SumAmazon · ONSITE INTERVIEW
  • 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
  • Count Belt Sections With K Fruit Pairs (Forward Deployed Engineer)Boston Consulting Group · OABoston Consulting Group interview questions
  • Max Consecutive Ones IIINykaa · PHONE SCREENNykaa interview questions

Google interview experiences

  • Google interviews · November 2026
  • Google Software Engineer Onsite Questions: 10 Experiences
  • Google OA: What It Tests (2026)
  • Google L4 SWE Phone Screen: 1 Experience
  • Google ML Engineering Manager Onsite: 1 Experience
  • Google Experienced SWE First Round and Onsite: 1 Experience
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public int longestTargetRunAfterReplacements(int[] nums, int target, int maxChanges) {
      // write your code here
    }

}
Checking account…