Longest Non-Decreasing Subarray
Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Google's November OA is asking you to find the longest non-decreasing subarray, which is a straightforward sliding window or single-pass greedy problem dressed up as dynamic programming. You're looking for a contiguous stretch of elements where each one is greater than or equal to the previous one. The trap is overthinking it. Most candidates jump to DP tables when a clean O(n) scan solves it. StealthCoder will catch this pattern live if you blank on the approach.
Pattern and pitfall
The core trick is recognizing this doesn't need memoization or state. Iterate through the array once, tracking the current non-decreasing streak length. When you hit an element smaller than the previous one, reset the streak counter. Keep a running maximum. The pattern is greedy, not DP, though Google labels it that way to test if you can spot the simpler solution hiding inside. Edge cases: single element (answer is 1), all decreasing (answer is 1), all non-decreasing (answer is n). Off-by-one errors on streak boundaries are common. StealthCoder gives you the template instantly if you lock up mid-OA.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Longest Non-Decreasing Subarray 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. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Google's OA.
Google reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Longest Non-Decreasing Subarray FAQ
Is this actually a DP problem or just greedy?+
It's greedy. Google may tag it DP to see if you overthink. One pass through the array, tracking the current streak length and the max seen so far. No table, no recursion needed. The simplicity is the point.
What happens if the array is empty or has one element?+
Empty returns 0. Single element returns 1 (a subarray of length 1 is always non-decreasing). Handle both edge cases before the main loop to avoid index errors.
Do I need to track the subarray itself or just the length?+
Google asked for the length. If they ask for the subarray, track start and end indices of the longest streak as you iterate. Most OAs only want the integer answer, so confirm the problem statement.
How do I avoid off-by-one errors on the streak counter?+
Start with current_length = 1 (assuming non-empty input). Increment it when arr[i] >= arr[i-1]. Reset to 1 when arr[i] < arr[i-1]. Update max after the reset. Trace one example by hand before submitting.
What's the time and space complexity Google expects?+
O(n) time, O(1) space. One pass, no extra structures. If you're building a DP table, you're doing it wrong and will time out on large inputs.