Longest Palindromic Subsequence
Reported by candidates from Goldman Sachs's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Brute force on this one dies fast. A string of any real length has 2^n subsequences, and checking each for palindromes won't finish. That's the wall Goldman Sachs candidates hit when this Longest Palindromic Subsequence question showed up, reported in September 2026. The input is a string s, the output is the length of its longest palindromic subsequence, and the examples are "bbbab" giving 4 and "cbbd" giving 2. It's a dynamic programming problem, and once you see the table it's short. If you blank during the live OA, StealthCoder sits invisibly on your screen as a safety net.
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 is an interval DP. Let dp[i][j] be the longest palindromic subsequence inside s[i..j]. If s[i] equals s[j], then dp[i][j] = dp[i+1][j-1] + 2. Otherwise it's max(dp[i+1][j], dp[i][j-1]). Base case: dp[i][i] = 1. Fill the table with i going from n-1 down to 0 and j going from i+1 up to n-1, so the dependencies are already computed. That's O(n^2) time. You can compress to one row if you want, but the 2D version is safer under pressure. The common pitfall is confusing subsequence with substring and reaching for expand-around-center, which gives wrong answers here. Another one is forgetting the empty inner case when j = i+1. A second route is reversing s and running longest common subsequence against the original. Same answer, same complexity. If the table logic slips away mid-assessment, StealthCoder can hand you the working solution in real time.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
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. If you're reading this with an OA window open, you're who this was built for.
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 Goldman Sachs's OA.
Goldman Sachs 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 Palindromic Subsequence FAQ
What's the trick to Longest Palindromic Subsequence?+
Define dp[i][j] as the best length within s[i..j]. Matching ends add 2 to the inner result. Mismatched ends take the max of dropping either end. Base case is a single character equals 1. Fill it from the bottom row up so inner intervals exist first.
How hard is this really for a Goldman Sachs OA?+
It's a medium. The recurrence is short, but you have to recognize it's interval DP rather than a substring problem. If you've seen the longest common subsequence pattern, this is a small step. Cold, expect to spend most of the time on the recurrence.
Can I solve it with longest common subsequence?+
Yes. Reverse the string and compute LCS between s and its reverse. A common subsequence of both is a palindromic subsequence of s. It gives the same O(n^2) time and is easy to write if you already know the LCS template.
Why doesn't expand-around-center work?+
Expand-around-center finds palindromic substrings, which must be contiguous. This problem allows skipped characters. In "bbbab" the answer 4 comes from bbbb, which isn't contiguous. So you need DP over intervals, not center expansion.
How do I prepare for this in 48 hours?+
Write the 2D interval DP from memory twice. Test on "bbbab" and "cbbd", then a single character and a two-character mismatch. Then write the LCS-with-reverse version. Knowing both gives you a fallback if one recurrence slips on the day.