cswork / libraryOpen practice workspace

Coding questions · Meta

Decide palindrome within k deletions

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.

Given a string s and integer k, decide whether s can be transformed into a palindrome by deleting at most k characters. Provide an algorithm to compute the minimum number of deletions required to make s a palindrome and compare it with k. Discuss a DP solution and its time/space trade-offs, and outline optimizations for large inputs.

What a Strong Answer Covers

  • A clear algorithm with the right data structures and enough pseudocode or code-level detail to implement it.
  • A correctness argument that explains why the algorithm covers all required cases.
  • Time and space complexity, plus at least one alternative approach when relevant.
  • Focused tests for normal cases, edge cases, and failure modes.

Follow-up Questions

  • How would the approach change if the input were streaming or too large for memory?
  • What invariants would you assert in production code?
  • Which tests would catch off-by-one, duplicate, or tie-breaking bugs?

Palindrome Within K Deletions

Given a string s and an integer k, determine whether s can be transformed into a palindrome by deleting at most k characters.

The minimum number of deletions required to make a string a palindrome equals n - L, where n = len(s) and L is the length of the longest palindromic subsequence (LPS) of s (each character not in the LPS must be deleted). Return True if this minimum is <= k, otherwise False.

Examples

  • s = "abcdeca", k = 2 -> True. The LPS is "aceca" (length 5), so minimum deletions = 7 - 5 = 2, which is <= 2.
  • s = "aebcbda", k = 1 -> False. The LPS is "abcba" (length 5), so minimum deletions = 7 - 5 = 2, which is > 1.
  • s = "abca", k = 1 -> True. Deleting one of b/c yields "aba"/"aca", so minimum deletions = 1 <= 1.

Constraints

  • 0 <= len(s) <= 1000
  • 0 <= k <= len(s)
  • s consists of lowercase English letters

Hints

  1. The minimum deletions to make s a palindrome is n minus the length of its longest palindromic subsequence (LPS).
  2. 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]).
  3. 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.

Solution

Solution Alignment

The prompt asks for an implementation-level answer. The safest way to present it is to define the state, maintain clear invariants, then walk through complexity and tests.

Problem Restatement

Given a string s and integer k, decide whether s can be transformed into a palindrome by deleting at most k characters. Provide an algorithm to compute the minimum number of deletions required to make s a palindrome and compare it with k. Discuss a DP solution and its time/space trade-offs, and outline optimizations for large inputs.

Recommended Approach

Define a state that captures exactly the remaining decision information. Fill base cases first, then transition from smaller subproblems to larger ones. For games, use score difference or minimax DP; for counting, sum valid predecessor states.

Correctness

The implementation should maintain an invariant after each loop or operation that directly matches the problem statement. At termination, that invariant implies the returned value has considered every valid candidate exactly once, or has preserved the required data-structure state after every API call.

Complexity

Typical DP time is number_of_states times transition_cost. Space can often be reduced when transitions only need the previous layer or diagonal.

Edge Cases and Tests

Empty input, length 1, invalid symbols, negative values where allowed, ties under optimal play, and large counts requiring modulo arithmetic.