DSA interview question
You're building a "genetic symmetry detector" for a bioinformatics tool that scans DNA/RNA sequences for palindromic motifs (real biological structures — many restriction enzymes bind to palindromic DNA sequences). Characters don't need to be contiguous — you're looking for the longest subsequence of the given string s that reads the same forwards and backwards. Return the length of that longest palindromic subsequence (LPS).
int longestPalindromeSubseq(string s);
Input s | Output | Explanation |
|---|---|---|
"bbbab" | 4 | "bbbb" is a palindromic subsequence of length 4 |
"cbbd" | 2 | "bb" is the longest palindromic subsequence |
"a" | 1 | single character |
"" | 0 | empty string |
"aaaa" | 4 | already fully a palindrome |
"agbdba" | 5 | "abdba" or "agbga"-style — length 5 palindromic subsequence exists |
"character" | 5 | e.g. "carac" is a palindromic subsequence of length 5 |
(Note: this asks for LENGTH only. A very common, closely related follow-up asks to return the actual subsequence string — be ready for that pivot.)
s alone, without going through LCS?"On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.
Book a free mock interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in