Count Prefix Matches in a Sorted Array
Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The data structure here is the plain sorted array, and that's the whole point. This Google OA question, reported in July 2026, hands you a lexicographically sorted list of words and a prefix, then asks how many entries start with it. Everything matching sits in one contiguous block, so you don't need a trie or a hash map. You need two binary searches. It looks like a warmup, but the edge cases trip people up when they rush. If you blank on the boundary logic during the live assessment, StealthCoder runs invisibly as a safety net and can hand you the working structure.
The problem
Given a lexicographically sorted array of lowercase strings words and a lowercase string prefix, return the number of array entries that begin with prefix. Duplicate words count separately. For this exercise, assume all words and the prefix are non-empty and use ordinary lowercase lexicographic order. Use the sorted order to locate the contiguous matching range with binary search. Function countPrefixMatches(words: String[], prefix: String) → int Examples Example 1 words = ["apple","apply","apt","banana"] prefix = "app" return = 2 Only apple and apply begin with app. Example 2 words = ["a","a","ab","b"] prefix = "a" return = 3 Both copies of a and the word ab match, so duplicates contribute separately. Constraints 0 <= words.length <= 2 * 10^5 Every word and prefix is a non-empty lowercase English string. words is sorted in nondecreasing lexicographic order. The total number of characters in words and prefix is at most 2 * 10^5.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick: all words starting with prefix form one contiguous range in sorted order. Find the first index where word >= prefix (lower bound). Then find the first index where word is past every string starting with prefix. You can do that by comparing only the first len(prefix) characters of each word, and treating a word as 'less' if its truncated form is less than prefix. Answer is end minus start. The common pitfall is comparing full words against the prefix, which breaks on cases like 'a' versus 'ab'. Another is writing an off-by-one loop and missing the empty array case. Don't count with a linear scan, it works but ignores the hint. Cost per comparison is at most the prefix length, so total work stays tiny. If the live OA makes you freeze on boundaries, StealthCoder is the hedge that gets you a clean lower-bound pair.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Count Prefix Matches in a Sorted Array 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
You've seen the question.
Make sure you actually pass Google's OA.
Google 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 Prefix Matches in a Sorted Array FAQ
What's the trick in Count Prefix Matches in a Sorted Array?+
Matches are contiguous because the array is sorted. Binary search for the first word whose first len(prefix) characters are >= prefix, then the first whose truncated form is > prefix. Subtract the two indexes. Duplicates count automatically since you're measuring a range.
Do I need a trie for this Google OA question?+
No. A trie works but it's overkill and costs extra memory. The sorted order already gives you the structure. Two binary searches are shorter to write, easier to debug, and match the hint in the problem statement.
How hard is this really?+
Easy to medium. The idea is simple, but binary search boundaries cause most failures. If you've written lower bound and upper bound before, it's a ten minute problem. The trap is comparing whole words instead of the truncated prefix slice.
What edge cases should I test?+
Test an empty words array, which returns 0. Test a prefix with no matches, a prefix matching every word, and duplicates like the second example. Also test a word shorter than the prefix, such as 'a' against 'app', which must not match.
How do I prepare in 48 hours?+
Write lower bound and upper bound binary search from scratch until you can do it without thinking. Then adapt the comparison to use a truncated string slice. Run both given examples plus an empty array. That covers almost everything this problem can throw at you.