FastPrepLongest Non-Decreasing Subarray After One Modification
Problems/Longest Non-Decreasing Subarray After One Modification

Longest Non-Decreasing Subarray After One Modification

Google logoGoogle● MediumINTERNFULLTIMENEW GRADPHONE SCREENONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The original author describes a variant of the longest contiguous non-decreasing subarray, and confirms that one number may be modified. The range of replacement values, the input limits, and the examples were not kept. Our version is about 93% the same as the reported question.

You are given an integer array nums. You may modify at most one element, replacing it with any integer.

Return the maximum possible length of a contiguous non-decreasing subarray after the optional modification. A subarray is non-decreasing when every element after the first is greater than or equal to the preceding element.

Function

maxNonDecreasingLength(nums: int[]) → int

Examples

Example 1

nums = [1,2,5,3,4]return = 5

Replace 5 with 3 to obtain [1,2,3,3,4].

Example 2

nums = [4,2,3,1,5]return = 4

Replace the value 1 at index 3 with 3. The subarray [2,3,3,5] is then non-decreasing.

Example 3

nums = [5,4,3]return = 2

One adjacent pair can be made non-decreasing, but all three positions cannot be fixed with one modification.

Constraints

  • 1 <= nums.length <= 200000.
  • -10^9 <= nums[i] <= 10^9.
  • The replacement value may be any signed 64-bit integer.

More Google Onsite Interview questions

  • Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • 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
  • 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

  • Assign Escalations to TeamsAmazon · ONSITE INTERVIEW
  • Maximum Subarray Sum with One DeletionServiceNow · PHONE SCREENServiceNow interview questions
  • Maximum Product of a Contiguous SubarrayLinkedIn · PHONE SCREENLinkedIn interview questions
  • Count Subsets With a Target SumSafe Security · ONSITE INTERVIEW
  • Fewest Coins CombinationBloomberg LP · PHONE SCREENBloomberg LP interview questions
  • Longest Increasing SubsequenceByteDance · PHONE SCREENByteDance interview questions
  • Jump GamePaytm · PHONE SCREENPaytm interview questions
  • Maximum Subarray Sum with One DeletionOracle · ONSITE INTERVIEWOracle interview questions
  • Query Words Within K PositionsPalantir · PHONE SCREENPalantir interview questions
  • House RobberVisa · PHONE SCREENVisa interview questions

Interview reports that mention this question

  • Google SWE Intern OA and Interviews: 42 Experiences (Part 1 of 3)

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
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public int maxNonDecreasingLength(int[] nums) {
        // Write your code here.
    }
}
Checking account…