Longest Increasing Subsequence
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Most people sink their first attempt at this one by confusing a subsequence with a subarray. Amazon candidates reported Longest Increasing Subsequence in October 2026, and the example (10,9,2,5,3,7,101,18 returns 4) is a trap if you only look at adjacent elements. The elements don't need to be contiguous, they just need to keep their order and strictly increase. It's a dynamic programming problem with a faster binary search variant. If you blank during the live OA, StealthCoder runs invisibly on your desktop and gives you the approach in real time. Know the shape before you open the editor.
The problem
Given an integer array nums, return the length of its longest strictly increasing subsequence. Examples Example 1 nums = [10,9,2,5,3,7,101,18] return = 4 One longest strictly increasing subsequence is [2, 3, 7, 101], which has length 4.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The classic route is dp[i] = length of the longest strictly increasing subsequence ending at index i. For each i, scan every j before it, and if nums[j] < nums[i], take dp[j] + 1. The answer is the max over all dp values, not dp[last]. That's O(n^2) and usually passes. The faster route keeps a tails array, where tails[k] is the smallest possible ending value of an increasing subsequence of length k+1. For each number, binary search for the first tail that's >= it and replace it, or append if none exists. That's O(n log n). The pitfall: strictly increasing means use lower-bound, not upper-bound, or duplicates inflate your length. Also remember tails is not the actual subsequence. If you freeze on the live OA, StealthCoder is the safety net that surfaces the recurrence or the binary search trick on screen.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Longest Increasing 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. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
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 Amazon's OA.
Amazon reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Longest Increasing Subsequence FAQ
What's the trick to Longest Increasing Subsequence?+
Define dp[i] as the best length ending exactly at i, then take the max across all i. The common miss is returning dp[n-1], which is wrong because the best subsequence may end earlier. Start with O(n^2), then optimize only if you have time.
Do I need the O(n log n) solution for the Amazon OA?+
Not always. The O(n^2) DP is correct and often passes on moderate input sizes. But if constraints are large, you'll need the tails array with binary search. Write the simple version first so you have something working, then upgrade it.
Why does strictly increasing matter in the code?+
With equal values allowed you'd use upper-bound. With strict, you replace the first tail that's greater than or equal to the current number, so lower-bound. Test on [2,2,2]. The answer should be 1, not 3.
Is the tails array the actual subsequence?+
No. It only holds the smallest possible tail for each length, so its contents can mix elements from different subsequences. Its length equals the answer, but you can't read the sequence from it. Reconstructing needs parent pointers.
How do I prepare for this in 48 hours?+
Hand-trace the example [10,9,2,5,3,7,101,18] through both the dp table and the tails array. Then code each from memory once. Check edge cases: a single element, all descending, all equal. That covers most of what can go wrong.