Longest Balanced Bitonic Subsequence
Reported by candidates from Wells Fargo's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The Wells Fargo OA reported in September 2020 asks for the longest balanced bitonic subsequence, and the whole thing hinges on two arrays of LIS lengths, one running left to right and one right to left. If you've got an invite and 48 hours, this is the one to understand cold. The twist is the word balanced: both sides need the same count k, and the answer is 2k-1. Nothing exotic, just a standard DP with a min() bolted on. StealthCoder is there as a safety net if your mind goes blank mid-assessment, but the pattern is short enough to learn tonight.
The problem
You are given an integer array nums. Choose a subsequence with one peak such that values are strictly increasing up to the peak and strictly decreasing after it. The peak belongs to both sides. If each side contains k selected elements including the peak, the balanced bitonic subsequence has length 2 * k - 1. Return the maximum possible length of a balanced bitonic subsequence. Function longestBalancedBitonicSubsequence(nums: int[]) → int Examples Example 1 nums = [1,2,3,2,1,4,5,6,7,19,15,12,10,9] return = 9 One balanced choice has five increasing elements ending at 19 and five decreasing elements starting there, for total length 2 * 5 - 1 = 9. Example 2 nums = [1,2,3,2,1] return = 5 The entire array is strictly increasing to 3 and then strictly decreasing, with three elements on each side including the peak. Example 3 nums = [1,2,3,4] return = 1 No element has a decreasing continuation, so only a single-element balanced subsequence is possible. Example 4 nums = [1,3,2,4,3,2] return = 5 Select 1, 3, 4, 3, 2. Both strict sides contain three elements including the peak. Constraints 1 <= nums.length <= 2000. -10^9 <= nums[i] <= 10^9.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Build inc[i], the length of the longest strictly increasing subsequence ending at i. Build dec[i], the longest strictly decreasing subsequence starting at i, by scanning from the right. For each i, the balanced k is min(inc[i], dec[i]), since you can always trim the longer side by dropping elements. The answer is the max over i of 2*min(inc[i], dec[i]) - 1. With n up to 2000, the O(n^2) double loop is fine, no need for the patience-sort trick. Pitfalls: using non-strict comparisons, forgetting the peak is counted once not twice, and returning 2*k instead of 2*k-1. Example 3 returns 1 because dec is 1 everywhere. If you freeze live, StealthCoder can surface this exact recurrence while you type, but the min() insight is the whole problem.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Longest Balanced Bitonic Subsequence 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. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Wells Fargo's OA.
Wells Fargo reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Longest Balanced Bitonic Subsequence FAQ
What's the trick to Longest Balanced Bitonic Subsequence?+
Compute LIS ending at each index and LDS starting at each index. At every candidate peak, take min of the two, then answer is 2*min-1. The min handles the balanced requirement because you can always shorten the longer side by removing elements.
How hard is this Wells Fargo OA question really?+
Medium. It's a classic bitonic subsequence DP with one extra step. If you've seen LIS before, you can finish it in 15 minutes. The n of 2000 means O(n^2) passes, so there's no need for anything fancier.
Why 2*k-1 and not 2*k?+
The peak belongs to both the increasing and decreasing sides. Each side has k elements including the peak, so you'd double count it. Subtract one. Example 2 shows it: k is 3, length is 5.
Do I need the O(n log n) LIS approach?+
No. Constraints cap length at 2000, so the O(n^2) nested loop is about 4 million operations per pass. Write the simple version, get it correct, and only optimize if you have time left over, which you probably won't need.
How do I prepare for this in 48 hours?+
Write LIS from scratch twice, then write the reversed version for decreasing. Trace Example 4 by hand: [1,3,2,4,3,2] giving 5. Test edge cases like a strictly increasing array, which returns 1, and a single element.