Count Different Palindrome Substrings
Reported by candidates from Pure Storage's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Pure Storage reported this one in March 2025, and the title is misleading. It says "different" but the examples count every position separately. Two "ll" substrings in "hellolle" count as two. So the question really reduces to counting every palindromic substring by position, which is the expand-around-center pattern. If you've got an OA invite for Pure Storage this week, this is a clean two-pointer problem once you read the examples right. StealthCoder sits invisibly on your screen during the live OA as a safety net if you blank on the center expansion.
The problem
A string S is considered palindrome if it reads same way if spelled backwards, for example "nolemonnomelon", "ASANtaLivedAsAdeviLatNASA". Any non-empty string has substrings that are palindromes. For example, in the string S = "hellolle", there are many of such "subpalindromes": ellolle ll, ll - note that these are two distinct substrings that only happen to be equal. lol and lloll And each single letter can be considered a palindrome - 8 of them. Please write a function that, given a string S (only contains lowercase letters), returns number of different ways are there to pick a palindrome substring fro mS. Function countDifferentPalindromeSubstrings(s: String) → int Examples Example 1 s = "hellolle" return = 13 Output 13. Example 2 s = "wowpurerocks" return = 14 each letter + "wow" + "rer"
Reported by candidates. Source: FastPrep
Pattern and pitfall
Check the example first. "hellolle" gives 13: 8 single letters, then "ll" twice, "lol", "lloll", "ellolle". That's 8+2+1+1+1 = 13. So you count by start and end index, not by unique string. No set needed. The trick: every palindrome has a center. Loop over 2n-1 centers (n odd ones, n-1 even ones). For each, set left and right pointers, expand while the characters match, and add one per successful expansion. That's O(n^2) time and O(1) space. The common pitfall is dedupe with a hash set, which gives the wrong answer on example 1. Another is forgetting even-length centers, which drops "ll". Check "wowpurerocks" too: 12 letters, plus "wow" and "rer" gives 14. If you freeze in the live OA, StealthCoder can hand you the expand-around-center code as a hedge.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Count Different Palindrome Substrings 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. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as palindromic substrings. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Pure Storage's OA.
Pure Storage reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Count Different Palindrome Substrings FAQ
What's the trick for Count Different Palindrome Substrings?+
Expand around centers. For each index, expand once as an odd center and once as an even center, counting one for every match. Don't dedupe. The examples show equal substrings at different positions count separately, so the answer is a total count of palindromic index ranges.
Do I count unique palindromes or every occurrence?+
Every occurrence. In "hellolle" the two "ll" substrings count separately and the total is 13. If you used a set of unique strings you'd get 12. Verify against example 1 before you write any code.
What's the time complexity I should aim for?+
O(n^2) time and O(1) extra space with center expansion is the expected answer. A brute force that checks every substring is O(n^3). Manacher's algorithm gets O(n) but it's overkill unless the input is huge, and the problem doesn't say it is.
How hard is this one really?+
Easy to medium. It's the classic palindromic substrings problem with confusing wording. The hard part is reading the title and examples correctly. Once you see that positions count, the code is about ten lines.
How do I prepare in 48 hours?+
Write the expand-around-center function from memory twice. Test it on "hellolle" (13) and "wowpurerocks" (14). Then check edge cases: a single character, all identical letters like "aaaa", and a string with no repeats where the answer equals the length.