Reported February 2023
ZipRecruiterstring

Count Prefix-Related Word Pairs

Reported by candidates from ZipRecruiter's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live ZipRecruiter OA. Under 2s to a working solution.
Founder's read

Equal words count as prefix pairs. That one line in the ZipRecruiter problem, reported in February 2023, is where most people's first draft breaks. You're counting unordered index pairs (i, j) where one word is a prefix of the other, across up to 2000 words of up to 1000 characters. It's a string problem, and a trie or a sorted-order scan beats the obvious double loop. If you blank when the OA clock is running, StealthCoder sits invisibly on your screen as a safety net. Read this first so you don't need it.

The problem

You are given an array of strings words. Count unordered index pairs (i, j) with i < j for which either word is a prefix of the other.
Equal words qualify.

Function
countPrefixPairs(words: String[]) → long

Examples
Example 1
words = ["a","ab","abc","b"]
return = 3
The qualifying pairs are (a, ab), (a, abc), and (ab, abc).
Example 2
words = ["x","x","xy"]
return = 3
The two equal words qualify, and each is a prefix of xy.

Constraints
0 <= words.length <= 2000
0 <= words[i].length <= 1000
Words contain lowercase English letters.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The brute force checks every pair with startsWith. That's about 2 million pairs times up to 1000 characters, roughly 2 billion character comparisons worst case. Too risky. The clean trick is a trie with counts. Insert each word, and at every node track how many words end there. When you insert a word, add the number of earlier words that end on its path (they're prefixes of it, equal words included). Then you also need earlier words that have this word as a prefix, which is the number of earlier words passing through the final node, minus the ones that end exactly there so equal pairs aren't counted twice. The pitfall is double counting equal words and forgetting empty strings, which are a prefix of everything. Return a long, since pairs can reach about 2 million but you should still avoid int slips. If the trie logic slips mid-OA, StealthCoder can give you the working version live.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

You can drill Count Prefix-Related Word Pairs 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 StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass ZipRecruiter's OA.

ZipRecruiter 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-Related Word Pairs FAQ

What's the trick to Count Prefix-Related Word Pairs?+

Use a trie that stores two counts per node: words passing through and words ending there. For each new word, add earlier words that end along its path, plus earlier words that pass through its final node but don't end exactly there. Then handle equal words once.

Is the brute force good enough?+

Probably not. With 2000 words you get about 2 million pairs, and each startsWith can cost up to 1000 characters. That's around 2 billion operations in the worst case. It may pass small tests, then time out on the large ones. Go with the trie.

How do equal words and empty strings get handled?+

Equal words always qualify, so two copies of x make one pair. An empty string is a prefix of every word, so it pairs with all others. A trie handles this naturally if the root counts words ending there. Test both cases before submitting.

Why does the return type being long matter?+

The pair count can reach n(n-1)/2, which is about 2 million for 2000 words. That fits in an int, but the signature says long, so accumulate in a long anyway. It avoids overflow bugs if you multiply counts together in your formula.

How do I prepare for this in 48 hours?+

Write a trie from scratch twice, with a count field per node. Then solve this problem, checking against both examples: 3 for [a, ab, abc, b] and 3 for [x, x, xy]. Add a test with empty strings and duplicates. That covers the likely traps.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with ZipRecruiter.

OA at ZipRecruiter?
Invisible during screen share
Get it