← Questions

Score how close a string is to a palindrome

0
AppleCodingLast asked Mar 2026

A coding interview question asked in Apple's coding round, last asked Mar 2026. It tests strings, two pointers and dynamic programming.

Design a score that says how close a string is to being a palindrome, so imperfect strings can be compared, and implement it. There is no single correct formula. Give one complete proposal: what you compare, which features you measure, how they are weighted, the score's range, what the edge cases get, worked examples, and how you would check that the weighting is useful.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • palindrome_score(s: str) -> float — returns a score in [0, 1], where 1 means an exact palindrome under your normalization.

Example

Two features that capture different defects:

"racecar"    mismatched mirrored pairs 0 of 3, deletions needed 0   # exact palindrome
"racecbr"    mismatched mirrored pairs 1 of 3, deletions needed 2   # one substituted character
"xracecar"   mismatched mirrored pairs 4 of 4, deletions needed 1   # one extra character

Comparing mirrored pairs scores "xracecar" as far from a palindrome, even though removing one character fixes it. The fewest deletions (string length minus its longest palindromic subsequence) sees that, but treats one substitution as two deletions. Your score should say which of these strings is closer and why.

Constraints

  • State what is compared: exact characters, or text with case, spaces and punctuation removed. If you allow Unicode, say what a character is and how you normalize.
  • Define the score for the empty string and a one-character string.
  • Say how length affects the score, so a long string with one defect and a short one with one defect compare sensibly.

Clarifying questions

  • What is the score for: ranking candidates, a pass/fail threshold, or reporting?
  • Should a substitution and an inserted or missing character cost the same?
  • Is case or punctuation a defect?
  • How long can the strings be?

What a strong answer covers

  • At least one precisely defined feature, and a reason for each extra feature (mirrored-pair mismatches catch substitutions; edit distance or longest palindromic subsequence catches insertions and deletions).
  • A normalized formula with explicit weights, its range and its boundary cases.
  • Cost: the pair count is O(n); the longest palindromic subsequence is O(n²) time, O(n) space with a rolling row.
  • Worked examples that separate exact palindromes, substitutions and shifted strings.
  • A plan to evaluate the weights: a small labeled set of string pairs ranked by people, and checks for unintuitive rankings, without claiming accuracy you haven't measured.

Follow-ups

  • Can a long string with several defects outscore a short one with a single defect? Is that what you want?
  • How does the score change if only substitutions are allowed?
  • Which examples would show that two of your features are redundant?

Where it's asked

  • Apple · Coding · Last asked Mar 2026

What it tests

strings · two pointers · dynamic programming

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding