Count Substrings
Reported by candidates from Salesforce's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Salesforce asked this in June 2024, and it's a substring-counting problem that sounds simple until you realize the trick. You're counting substrings that match some condition, usually palindromic or following a pattern. The OA expects you to avoid the brute force O(n^3) nested loop trap. StealthCoder can feed you the optimal approach if you blank on the expand-around-center trick during the live assessment.
Pattern and pitfall
The classic move here is expand-around-center, which runs in O(n^2) time and O(1) space. You iterate through each possible center (single character or between two characters for even-length palindromes) and expand outward while the condition holds. The trap candidates fall into is building every substring and checking it, which times out. The other angle is dynamic programming with a table that tracks valid substrings, but expand-around-center is usually cleaner for palindrome counts. Know both approaches before you sit down.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Count 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 Salesforce's OA.
Salesforce 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 Substrings FAQ
Is this definitely about palindromes?+
Probably, given the Salesforce pattern. But read the problem text carefully. Some variants ask for substrings where characters appear a certain number of times or follow a specific rule. Don't assume until you see the exact condition.
Can I brute force it?+
Technically yes for small inputs (n < 500), but OAs usually penalize O(n^3) solutions with tight time limits. Expand-around-center is the expected pattern. Learn it before the OA.
Do I handle both odd and even length substrings?+
Yes. For palindromes, odd-length ones have a single character center, and even-length ones center between two characters. Your loop needs to expand around both types or you'll miss half the count.
What's the common mistake on this one?+
Off-by-one errors in the expansion loop, or forgetting to check boundary conditions as you expand. Walk through a small example (like 'aba') by hand before submitting.
How do I prep this in 24 hours?+
Code up expand-around-center once, test it on 'aba' and 'abba', then trace through the Salesforce problem text if provided. You don't need to memorize variations. Know the core pattern cold.