Longest Palindromic Subsequence
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Strip away the palindrome wording and this Amazon OA question, reported in July 2026, is a classic string DP in disguise. You're finding the longest subsequence of s that reads the same both ways, and the whole thing collapses into a comparison between s and its reverse. If you've got an invite and 48 hours, this is one worth recognizing on sight. The code is short once you see it. The panic comes from not seeing it. StealthCoder sits invisibly on your screen as a safety net if your mind goes blank mid-assessment, but you should walk in knowing the shape.
The problem
Given a string s, return the length of its longest subsequence that reads the same from left to right and right to left. A subsequence keeps the relative order of selected characters but does not need to use contiguous positions. Function longestPalindromeSubseq(s: String) → int Examples Example 1 s = "bbbab" return = 4 One longest palindromic subsequence is bbbb. Example 2 s = "cbbd" return = 2
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick: define dp[i][j] as the longest palindromic subsequence inside s[i..j]. If s[i] equals s[j], the answer is dp[i+1][j-1] + 2. If not, take max(dp[i+1][j], dp[i][j-1]). Base case is dp[i][i] = 1. Fill the table with i going from n-1 down to 0 and j from i+1 up to n-1, because each cell depends on cells below and to the left. The equivalent view is longest common subsequence of s and reverse(s), which many people find easier to remember. Common pitfall: confusing subsequence with substring and reaching for expand-around-center, which gives wrong answers on bbbab. Another one is wrong loop order, which reads uninitialized cells. Time is O(n^2), and you can cut space to O(n) with a rolling row. If you freeze during the live OA, StealthCoder can hand you the recurrence while you stay in control of the typing.
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 Palindromic 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. 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 palindromic 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. 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 Palindromic Subsequence FAQ
How hard is Longest Palindromic Subsequence really?+
Medium. The recurrence is only two cases, but you need to see it's an interval DP. Once you've written it once, it takes ten minutes. The risk is mixing it up with the substring version and writing the wrong approach.
What's the trick for the Amazon version?+
Compare the two ends of a range. If they match, add 2 to the inner range's answer. If they don't, drop one end and take the better result. Single characters count as 1. That's the whole solution.
Can I solve it as longest common subsequence?+
Yes. The longest palindromic subsequence of s equals the LCS of s and its reverse. It's the same O(n^2) time. Use it if the LCS template is fresher in your memory than the interval DP.
Why does expand-around-center fail here?+
Expand-around-center only works for contiguous substrings. This problem allows skipping characters, so bbbab gives 4 from bbbb, which a contiguous approach would miss. You need DP over ranges instead.
How do I prepare in 48 hours?+
Write the 2D DP by hand twice on a blank file, then once with the rolling-row space optimization. Test with bbbab and cbbd, plus a single character and an all-same string. Know the loop direction cold, since that's where most bugs live.