Count Overlapping Substring Occurrences
Reported by candidates from ZipRecruiter's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The data structure that decides this ZipRecruiter OA question is a plain window over a string, plus an optional prefix table if you want to be fancy. It was reported in October 2023, and it asks you to count every start index in source where target matches, overlaps included. Sounds trivial. The catch is the size: both strings can hit 100000 characters. If you blank on the efficient version, StealthCoder is the invisible safety net that can hand you a working solution live. But the core idea fits in your head in five minutes.
The problem
Count zero-based starting positions in source whose substring of length target.length equals target. Occurrences may overlap and matching is case-sensitive. Function countSubstringOccurrences(source: String, target: String) → int Examples Example 1 source = "aaaa" target = "aa" return = 3 Matches begin at positions zero, one, and two. Example 2 source = "abcabc" target = "abc" return = 2 Two nonoverlapping occurrences are counted. Constraints 0 <= source.length <= 100000 1 <= target.length <= 100000
Reported by candidates. Source: FastPrep
Pattern and pitfall
Naive approach: slide a window of length target.length across source and compare each slice. That's O(n*m) and can blow up at 100000 by 100000, say with source of all a's and target of all a's. Overlap is the easy part. Just advance by one index after each match, never by target.length. That's why "aaaa" and "aa" gives 3, not 2. The efficient fix is KMP: build the failure array for target, then scan source once, and on a full match record it and fall back using the failure table. Z-function or rolling hash also work. Pitfalls: empty source returns 0, target longer than source returns 0, and matching is case-sensitive so don't lowercase anything. If you freeze on the prefix table during the live OA, StealthCoder is the hedge that gives you the KMP code while you keep your cool.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Count Overlapping Substring Occurrences 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 by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as find the index of the first occurrence in a string. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass ZipRecruiter's OA.
ZipRecruiter reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Count Overlapping Substring Occurrences FAQ
What's the trick in the ZipRecruiter substring count question?+
Advance the start index by one after every match so overlaps count. Then make the comparison fast. A naive slice compare works on small input, but with 100000 characters you want KMP, Z-function, or a rolling hash to stay linear.
Will brute force pass?+
Maybe on small tests, but it's risky. Worst cases like all a's in both strings push O(n*m) toward billions of operations. Language built-in find in a loop has the same problem. Assume hidden tests include large repetitive inputs and go linear.
What edge cases should I check?+
Empty source returns 0. Target longer than source returns 0. Target equal to source returns 1. Fully repeated characters test overlap. Mixed case like "Ab" versus "ab" must not match, since matching is case-sensitive.
Is this a sliding window or KMP problem?+
Both framings work. The fixed-length window is the brute force idea. KMP is the optimized version of the same scan, reusing earlier matches via the failure array. Pick whichever you can write bug-free under pressure, as long as it's linear.
How do I prepare in 48 hours?+
Write KMP's failure array from scratch twice, then code the counting scan with fallback after a full match. Test on aaaa/aa and abcabc/abc. That covers the overlap logic and the standard case. Skip anything fancier.