Longest Palindromic Subsequence Hidden in a Product Code

Last updated: November 29, 2026

Quick Overview

Given a product code string, return the length of the longest subsequence that reads the same forward and backward, keeping characters in order but allowing deletions.

Amazon
Coding & Algorithms
Software Engineer
Amazon
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Medium

0

0

0 solved


Given a product code string, return the length of the longest subsequence that reads the same forward and backward, keeping characters in order but allowing deletions.

A string interval DP in the Amazon phone screen style. The interviewer checks that you distinguish subsequence from substring, define dp over i..j, and can compress memory to one row while keeping the diagonal value correct.

What the Interviewer Expects
  • Distinguish subsequence from substring
  • dp[i][j] = 2 + dp[i+1][j-1] on a match, else max of shrinking either end
  • Fill i from right to left so dependencies are ready
  • Compress to a 1D array with a saved diagonal
  • O(n^2) time
Key Topics to Cover
interval dynamic programming
palindromes
space optimization
strings
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Return the palindrome itself, not just the length.
  • What is the minimum number of deletions to make the code a palindrome?
  • How does this relate to LCS of the string and its reverse?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: string code. Output: length of the longest palindromic subsequence. Empty string returns 0....

High-Level Design

Let dp[i][j] be the answer for code[i..j].

  • Single character: 1.
  • If code[i] == code[j], both ends can wrap the best inner palindrome: 2 + dp[i+1][j-1].
  • Otherwise one end is unused: `max(...

Submit Your Answer
Markdown supported