Longest Selectable Non-Decreasing Subarray
Reported by candidates from Visa's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Visa asked this in April 2026: given two parallel arrays A and B, pick one value from each position to form the longest non-decreasing sequence. You can't skip positions, only choose which array to pull from at each step. It's a dynamic programming problem disguised as a choice problem. StealthCoder will show you the state transition the moment you see the prompt.
The problem
You are given two integer arrays A and B of equal length. For any contiguous subarray of indices, you may choose exactly one value from either A[i] or B[i] at each index i in that subarray. Your goal is to make the chosen values form a non-decreasing sequence. Return the maximum possible length of such a contiguous subarray. Function Description Complete the function longestSelectableNonDecreasingSubarray in the editor below. longestSelectableNonDecreasingSubarray has the following parameters: Returns The source thread did not provide explicit numeric bounds.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trap is thinking you need to try all 2^n combinations. Instead, track two states: the longest subarray ending at position i where the last chosen value came from A, and the longest subarray ending at i where it came from B. At each position, you can extend either state if the new value is >= the previous value, or reset to 1 if neither works. The transition is: dp_a[i] = max(dp_a[i-1] + 1 if A[i] >= last_a, 1 if A[i] < last_a), and same for B. StealthCoder catches the state layout instantly during the live OA so you don't write the wrong recurrence under pressure.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Longest Selectable 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 passed his OA cold and still thinks the filter is broken.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as longest increasing subsequence. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Visa's OA.
Visa reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Longest Selectable Non-Decreasing Subarray FAQ
Is this really just longest non-decreasing subarray with a choice at each step?+
Yes. The choice is the novelty. At each index you pick A[i] or B[i], and the chosen value must be >= the previously chosen value. Standard DP, but you're tracking two parallel states instead of one.
What happens if neither A[i] nor B[i] is >= the last value?+
The subarray breaks. Reset both states to 1 at position i because you can always start a new subarray at any position. Return the global max seen.
Do I need to track the actual sequence or just the length?+
Just the length. You only need to remember the previous chosen value and the dp state at i-1. Space-optimized: keep current and previous row, or even O(1) if you iterate carefully.
How do I handle the first position?+
Both A[0] and B[0] form valid subarrays of length 1. Set dp_a[0] = 1, dp_b[0] = 1, and track last_a = A[0], last_b = B[0] for the next step.
Is this asked often at Visa or is it a one-off?+
DP choice problems are Visa's style. This is harder than standard DP array problems because you manage two states. If you've seen longest increasing subarray, this is the natural next step.