coding
Intuit
Google
Microsoft

Intuit Coding Question: Sum of Palindrome Modification Costs

Topics:
String
Two Pointers
Dynamic Programming
Roles:
Software Engineer
Backend Engineer
Algorithm Engineer
Experience:
Entry Level
Mid Level

Question Description

You are given a DNA string s (characters A, C, G, T). For every substring of s you must determine the minimum number of single-character changes needed to turn that substring into a palindrome, then return the sum of those minimum costs across all substrings.

This prompt tests string reasoning, two-pointers thinking, and counting/combinatorics. A naive approach checks every substring and compares characters from the ends to the middle — O(n^3) overall for length n (O(n^2) substrings × O(n) per check). Interviewers typically expect you to start with that brute force solution, explain its cost, then optimize.

Efficient approach (O(n^2)): observe that for any pair of positions i<j the pair contributes 1 to the cost of every substring where i and j are symmetric endpoints and s[i] != s[j]. The number of such substrings equals min(i, n-1-j) + 1. So you can sum (min(i, n-1-j)+1) over all i<j where s[i] != s[j]. Alternatively, iterate over 2n-1 centers and expand outwards counting mismatches — each expansion produces one substring and you add 1 when the end characters differ.

What you should demonstrate: correct counting logic, clear complexity analysis, edge-case handling (empty string, all-equal characters), and numeric types to avoid overflow. Be prepared to discuss memory trade-offs and how variants (weighted change costs or larger alphabets) affect your solution.

Common Follow-up Questions

  • Derive the closed-form contribution for a mismatched pair (i, j) and prove why summing min(i, n-1-j)+1 over mismatches is correct.
  • How would you modify the algorithm if each character change had a different cost (cost matrix between A,C,G,T)?
  • Can you design an algorithm that avoids O(n^2) work for long strings — what assumptions about n or the alphabet would let you do better?
  • How do you adapt your approach to count substrings that are already palindromes (i.e., cost 0) or to return the distribution of costs per substring length?

Related Questions

1Minimum number of changes to make a single string a palindrome (whole-string variant)
2Count all palindromic substrings in a string (expand-around-center / Manacher's algorithm)
3Minimum insertions to make a string a palindrome (DP)
4Sum of distances for mismatched character pairs across substrings — combinatorics on indices

Explore More Questions

Practice This Question with AI

Get real-time hints, detailed requirements, and insightful analysis of the question.

Intuit Coding: Sum of Palindrome Modification Costs | Voker