Decide palindrome within k deletions
Company: Meta
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Technical Screen
Quick Answer: This interview question evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer for Decide palindrome within k deletions states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.
Constraints
- 0 <= len(s) <= 1000
- 0 <= k <= len(s)
- s consists of lowercase English letters
Examples
Input: ("abcde", 1)
Expected Output: False
Explanation: LPS length is 1 (any single char), so min deletions = 5 - 1 = 4 > 1.
Input: ("abca", 1)
Expected Output: True
Explanation: LPS is 'aba' or 'aca' (length 3), min deletions = 4 - 3 = 1 <= 1.
Input: ("abcdeca", 2)
Expected Output: True
Explanation: LPS is 'aceca' (length 5), min deletions = 7 - 5 = 2 <= 2.
Input: ("", 0)
Expected Output: True
Explanation: Empty string is already a palindrome; 0 deletions needed.
Input: ("a", 0)
Expected Output: True
Explanation: A single character is a palindrome; 0 deletions needed.
Input: ("ab", 0)
Expected Output: False
Explanation: LPS length 1, min deletions = 2 - 1 = 1 > 0.
Input: ("aebcbda", 1)
Expected Output: False
Explanation: LPS is 'abcba' (length 5), min deletions = 7 - 5 = 2 > 1.
Input: ("racecar", 0)
Expected Output: True
Explanation: Already a palindrome; min deletions = 0 <= 0.
Hints
- The minimum deletions to make s a palindrome is n minus the length of its longest palindromic subsequence (LPS).
- Compute the LPS with interval DP: dp[i][j] is the LPS length within s[i..j]. If s[i] == s[j], dp[i][j] = dp[i+1][j-1] + 2; otherwise dp[i][j] = max(dp[i+1][j], dp[i][j-1]).
- Once you have min_deletions = n - LPS, the answer is simply min_deletions <= k. For large inputs, collapse the 2D table to two rolling rows for O(n) space.