Intuit Coding Question: Sum of Palindrome Modification Costs

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

1Microsoft Coding Interview: Longest Palindromic Subsequence
2Adobe Coding Question: Shortest Subarray with K Distinct
3Bloomberg Coding: Grid Shortest Path with K Breaks
4Cisco Coding Interview: Look-and-Say Next Term
5eBay Coding Question: Increasing Triplet Subsequence
6Minimum number of changes to make a single string a palindrome (whole-string variant)
7Count all palindromic substrings in a string (expand-around-center / Manacher's algorithm)
8Minimum insertions to make a string a palindrome (DP)
9Sum 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