Count Palindromes
Reported by candidates from Goldman Sachs's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
You've got a Goldman Sachs OA coming in the next 48 hours and they're asking you to count palindromes. No problem text provided, but this is a classic pattern: given a string (or sometimes a range of numbers), return how many palindromic substrings exist. The trick is avoiding the brute-force O(n^3) approach that will time out. StealthCoder sits quietly in the corner as your safety net if the exact variant catches you off guard during the live assessment.
Pattern and pitfall
The standard move here is expand-around-center, which runs in O(n^2) time and O(1) space. For each position in the string, treat it as a potential palindrome center and expand outward while characters match. Remember: odd-length palindromes expand from a single character, even-length ones from the gap between two characters, so you loop twice per position. The common pitfall is forgetting to count single characters (they're all palindromes) or miscounting overlapping palindromes. If the problem is actually counting palindromic numbers in a range, you're looking at digit DP, which is harder but less common in Goldman's assessments. StealthCoder will show you the exact input format during the real OA, which tells you immediately whether it's string-based or range-based.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Count Palindromes 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 Goldman Sachs's OA.
Goldman Sachs 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 Palindromes FAQ
Is this the expand-around-center problem or something else?+
Most likely expand-around-center. If they give you a string and ask for palindrome count, that's the play. If it's a numeric range (e.g., 1 to 1000), it's digit DP, which is rarer at Goldman but possible. The input format will tell you which one.
How do I handle the two cases (odd and even length palindromes) without doubling my code?+
Write a helper function that takes a center index and expands outward, counting as it goes. Call it twice per position: once treating the index as a single-character center, once treating the gap after the index as an even-length center. Clean and O(n^2).
What's the common mistake that tanks the runtime?+
Trying to use a hash table or DP table to memoize when you don't need to. Expand-around-center is fast enough without it. Another mistake: forgetting that substrings of length 1 count as palindromes, so you start with at least n of them.
Can I use Manacher's algorithm to impress them?+
Technically yes, it's O(n) and brilliant. But it's overkill for most assessments and easy to botch under time pressure. Expand-around-center is the safe, correct answer. Save Manacher for the follow-up conversation if they ask.
How much time should I spend on this problem live?+
If it's the standard string version, 20-30 minutes to code and test. If they surprise you with a range variant, that's harder and you'll want 40 minutes. That's where StealthCoder catches you: instant clarity on the exact problem statement and approach.