Count Palindromic Substrings
Reported by candidates from Airbnb's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Strip the wrapper off this Airbnb question from September 2022 and it's just "count every palindrome center." That's the whole thing. Candidates who see "substrings" and start generating all of them burn time they don't have. With a length cap of 2000, you can expand around centers in O(n^2) and be done. If you blank on the setup during the live OA, StealthCoder runs invisibly on your desktop as a safety net and hands you the solution while you keep typing. Know the center-expansion idea first, though, because it's short and you can write it from memory.
The problem
Given a string text, return the number of palindromic substrings in it. A palindrome reads the same forward and backward. Substrings are counted by their positions, so equal text appearing at different positions contributes more than once. Every one-character substring is a palindrome. Function countPalindromicSubstrings(text: String) → int Examples Example 1 text = "abc" return = 3 The palindromic substrings are "a", "b", and "c". Example 2 text = "aaa" return = 6 There are three one-character palindromes, two occurrences of "aa", and one "aaa". Constraints 1 <= text.length <= 2000. text contains lowercase English letters only.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick: every palindrome has a center. For a string of length n there are 2n-1 centers, n single characters and n-1 gaps between neighbors. For each center, put two pointers there and expand outward while the characters match. Each successful expansion is one more palindromic substring, so you add one to the count each time. Odd and even lengths are handled by starting at (i, i) and (i, i+1). The common pitfall is forgetting the even case, which breaks inputs like "aa". Another is checking every substring with a reverse comparison, which is O(n^3) and risky at 2000. A DP table also works but uses O(n^2) memory for no gain. Return an int, no modulo needed. If the live OA freezes your head, StealthCoder is the hedge that surfaces this exact expansion loop.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Count Palindromic 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 Airbnb's OA.
Airbnb 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 Palindromic Substrings FAQ
What's the trick for Count Palindromic Substrings?+
Expand around centers. There are 2n-1 centers: each character and each gap between two characters. From each, move left and right while the characters match, counting one palindrome per step. It's O(n^2) time and O(1) extra space.
How hard is this really for an Airbnb OA?+
Medium on paper, easy once you know center expansion. The code is about ten lines. The difficulty is seeing that you don't need to enumerate substrings. If you've seen it once, it's a five-minute problem.
Will brute force pass with length 2000?+
Probably not. Checking every substring is O(n^2) substrings times O(n) per check, roughly 8 billion operations at n=2000. Center expansion cuts it to about 4 million steps. Don't gamble on the brute force version.
What edge cases should I test?+
Test a single character (answer 1), "abc" (3), and "aaa" (6). The all-same-letter case is the best check for the even-length centers. If "aaa" returns 6, both odd and even expansions are working.
How do I prepare in 48 hours?+
Write the center-expansion function from scratch twice, with a helper that takes left and right indices and returns the count. Then trace "aaa" by hand. Also skim the DP version in case you're asked for an alternative. That's enough for this one.