Reported June 2026
Uberbit manipulation

Total Palindrome Substring Cost

Reported by candidates from Uber's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Uber OA. Under 2s to a working solution.
Founder's read

Uber's June 2026 OA hands you a DNA string and asks for the summed palindrome transformation cost of every substring. With |dna| up to 2 * 10^5, the obvious loop over all substrings is dead on arrival. The hint says two-pointers, but the real engine is a parity bitmask with prefix XOR. You've got a clock and an invite, so here's the shape: cost of a substring is floor(oddCount / 2), where oddCount is how many letters appear an odd number of times. StealthCoder sits invisibly as a safety net if the math freezes you mid-assessment.

The problem

Some data scientists are building a utility to analyze palindromic trends in the DNA sequencing of a string.
The palindrome transformation cost of a string is defined as the minimum number of characters that need to be changed so that the string can be rearranged to form a palindrome. For example, the palindrome transformation cost of "aabcd" is 1, since changing 'd' to 'c' makes "aabcc", which can be rearranged to "acbca".
Given a string dna, find the total sum of palindrome transformation costs of all substrings of dna.
Note: A palindrome is a sequence that reads the same backward as forward. For example, "z", "aba", and "aaa" are palindromes, but "xy" and "rank" are not.

Function
totalPalindromeSubstringCost(dna: String) → long
Complete the function totalPalindromeSubstringCost in the editor below. The function returns the total sum of palindrome transformation costs across all substrings.
totalPalindromeSubstringCost has the following parameter:
dna: a string

Examples
Example 1
dna = "abca"
return = 6
For dna = "abca", the single-character substrings "a", "b", "c", and "a" each have cost 0.
"ab" has cost 1; change 'b' to 'a' to make "aa".
"abc" has cost 1; change 'b' to 'c', then it can be rearranged to "cac".
"abca" has cost 1; change 'b' to 'c' to make "acca".
"bc" has cost 1; change one character to match the other.
"bca" has cost 1; change 'c' to 'a', then it can be rearranged to "aba".
"ca" has cost 1; change 'c' to 'a' to make "aa".
Therefore the total is 1 + 1 + 1 + 1 + 1 + 1 = 6.
Example 2
dna = "acbaed"
return = 19
For dna = "acbaed", the single-character substrings "a", "c", "b", "a", "e", and "d" each have cost 0.
All substrings of length 2, namely "ac", "cb", "ba", "ae", and "ed", have cost 1.
"acb", "cba", "bae", and "aed" have cost 1; one character can be changed so the substring can be rearranged into a palindrome.
"acba" has cost 1; for example, change 'b' to 'c' to make "acca".
"acbae" has cost 1; change 'c' to 'e', then it can be rearranged to "aebea".
"acbaed" has cost 2; change 'c' to 'e' and 'b' to 'd', then it can be rearranged to "aeddea". Similarly, "cbae", "baed", and "cbaed" each have cost 2.
Therefore the answer is 5 * 1 + 4 * 1 + 1 + 1 + 4 * 2 = 19.
Example 3
dna = "wwwww"
return = 0
All substrings of "wwwww" are already palindrome-like because every character is the same, so every substring has cost 0. The total sum is 0.

Constraints
1 <= |dna| <= 2 * 10^5
The string dna contains lowercase English letters only.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick: a substring's cost is floor(k / 2), where k is the number of letters with odd frequency. Encode parity in a 26-bit mask, prefix XOR as you scan. The substring (i, j] has mask prefix[j] XOR prefix[i], and k is the popcount of that XOR. So you need the sum of floor(popcount(p_j ^ p_i) / 2) over all pairs. Pairs number about 2 * 10^10, so you can't enumerate them. Since popcount of the XOR can only be 0 to 26, the usual move is to count pairs by popcount using the letter-by-letter structure, not by looping. The pitfall is brute-forcing with a count array per substring, which times out, or using int instead of long for the total. If you blank on the counting step during the live OA, StealthCoder can surface the full solution quietly. Check against example 2, which should give 19.

Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.

If this hits your live OA

You can drill Total Palindrome Substring Cost cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Uber's OA.

Uber reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Total Palindrome Substring Cost FAQ

What's the trick to Uber's Total Palindrome Substring Cost?+

Cost of a substring equals floor(odd-frequency letters / 2). Track letter parity as a 26-bit prefix XOR mask. The popcount of the XOR of two prefix masks gives the odd-letter count for the substring between them. Then sum floor(popcount / 2) over all pairs.

Why doesn't plain two pointers solve it?+

Two pointers needs a monotonic window property, and cost doesn't give you one. Extending a substring can raise or lower the odd count. The hint is loose. Prefix parity masks do the real work here, not a shrinking window.

How hard is this one really?+

Hard for an OA. The cost formula is easy once you see it. The part that bites is summing over about 2 * 10^10 substrings under the 2 * 10^5 length limit. Without the bitmask idea you'll write a correct but too slow solution.

What mistakes will fail hidden tests?+

Using a 32-bit int for the total, since the sum can exceed 2 billion. Also computing cost as the odd count instead of half of it. Check example 1 (answer 6) and example 3, wwwww (answer 0), before submitting.

How do I prepare in 48 hours?+

Practice prefix XOR parity masks, like the palindrome-permutation substring counting problems. Learn that a string can be rearranged into a palindrome when at most one letter has odd frequency. Then hand-trace the three examples in this problem so the cost formula is automatic.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Uber.

OA at Uber?
Invisible during screen share
Get it