Reported September 2026
Visadynamic programming

Maximum Non-Decreasing Array Length

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

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

Visa reported this one in September 2026, and the constraint is the first thing to read: arr.length goes up to 2000, so an O(n^2) solution passes but anything exponential won't. You merge adjacent elements into sums and want the longest non-decreasing result. It's Maximum Non-Decreasing Array Length, a dynamic programming problem on partitions with prefix sums. If you've got an invite for this week, the whole game is recognizing that you're cutting the array into contiguous segments, not simulating merges. StealthCoder sits invisibly on your screen as a safety net if you blank mid-assessment.

The problem

You are given an array arr of positive integers. In one operation, choose two adjacent elements and replace them with their sum.
Perform zero or more operations so that the resulting array is non-decreasing: every element is greater than or equal to the element before it. Return the maximum possible length of the resulting array.

Function
optimalSumOperations(arr: int[]) → int

Examples
Example 1
arr = [2,4,1,6,12]
return = 4
Merge 1 and 6 to obtain [2, 4, 7, 12]. This array is non-decreasing and has the maximum possible length of 4.
Example 2
arr = [5,4,3,2]
return = 2
Keep 5 as the first segment and merge the remaining values into 9, producing [5, 9].

Constraints
1 <= arr.length <= 2 * 10^3.
1 <= arr[i] <= 10^9.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Every sequence of merges is just a partition of the array into contiguous segments, each replaced by its sum. You want the most segments where the sums are non-decreasing. Build prefix sums. Let dp[i] be the max segment count for the first i elements, and last[i] the smallest possible final segment sum achieving it. For each i, find the best j < i where prefix[i] - prefix[j] >= last[j], then dp[i] = dp[j] + 1. Since longer prefixes of the same count aren't always better, you want the largest valid j, and the O(n^2) scan handles it fine at n = 2000. The common pitfall is greedily merging the first decreasing pair, which fails on cases like [5,4,3,2]. Another is overflow: sums reach 2 * 10^12, so use 64-bit integers. If the DP transition won't come to you live, StealthCoder gives you the working solution while the proctor sees nothing.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill Maximum Non-Decreasing Array Length 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 StealthCoder

Related leaked OAs

⏵ The honest play

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

Visa 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.

Maximum Non-Decreasing Array Length FAQ

What's the trick to Maximum Non-Decreasing Array Length?+

Stop thinking about merges. Any merge sequence equals a split of the array into contiguous segments, each summed. Then it's a DP over prefix positions: pick the previous cut so the new segment sum is at least the previous segment sum. Prefix sums make each segment sum O(1).

Will O(n^2) pass for this Visa OA?+

With arr.length up to 2 * 10^3, O(n^2) is about 4 million operations, which is fine. Brute force over all merge orders is exponential and dies immediately. An O(n log n) monotonic queue version exists, but you don't need it here.

Why does greedy merging fail?+

Merging the first violation locally can force bigger sums later and shorten the final length. In [5,4,3,2], the best is [5,9] with length 2. A greedy that merges 5 and 4 first gets [9,3,2], which is worse. You need the DP to compare partitions globally.

Do I need 64-bit integers?+

Yes. Each element can be 10^9 and there can be 2000 of them, so a segment sum reaches 2 * 10^12. That overflows a 32-bit int. Use long in Java or C++, and Python handles it natively. Prefix sums in 32-bit ints is a classic silent failure.

How do I prepare for this in 48 hours?+

Learn the partition DP shape: dp over prefix, transition over previous cut, prefix sums for segment cost. Hand-trace Example 1 and Example 2 until the answers 4 and 2 fall out. Then write it once from scratch in your OA language and test an all-equal array and a strictly decreasing one.

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

OA at Visa?
Invisible during screen share
Get it