Count Suffix-Related Word Pairs
Reported by candidates from ZipRecruiter's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
ZipRecruiter reported this one in September 2024, and the detail that matters is buried in the statement: equal words count as suffix pairs. So ["x","x"] is a valid pair, not an edge case to skip. It's a string problem that looks like a trie or hashing exercise but with 2000 words it's mostly about getting the pair check right. If you blank on the approach during the live OA, StealthCoder runs invisibly on your desktop as a safety net and reads the problem for you. Here's the pattern and the traps.
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 suffix of the other. Equal words qualify. Function countSuffixPairs(words: String[]) → long Examples Example 1 words = ["a","ba","cba","x"] return = 3 a is a suffix of ba and cba, and ba is a suffix of cba. Example 2 words = ["x","x","yx"] return = 3 The equal x values qualify, and each is a suffix of yx. Constraints 0 <= words.length <= 2000 0 <= words[i].length <= 1000 Words contain lowercase English letters.
Reported by candidates. Source: FastPrep
Pattern and pitfall
With at most 2000 words, there are about 2 million pairs. For each pair, check whether the shorter word is a suffix of the longer one, using endsWith or a manual compare from the end. Equal length means the words must be identical, which your suffix check already handles. Worst case is 2 million pairs times 1000 characters, which is heavy but the check exits early on mismatch, so it usually runs fast. The faster route is a reversed-word trie. Insert each reversed word, and at each node count how many earlier words ended there. Reversed suffixes become prefixes, so each word adds the count of earlier words along its path. Pitfalls: return a long, handle empty strings (an empty word is a suffix of everything), and don't double count pairs. StealthCoder is your hedge if the trie logic slips mid-assessment.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Count Suffix-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. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as count prefix and suffix pairs i. 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. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Count Suffix-Related Word Pairs FAQ
What's the trick in the ZipRecruiter suffix pairs problem?+
Reverse every word so suffixes become prefixes, then use a trie. As you insert each reversed word, add up how many earlier words ended at each node you pass. That counts every valid earlier partner, including equal words, in one pass.
Is brute force good enough here?+
Often yes. With 2000 words you check around 2 million pairs, and endsWith exits early on mismatch. The worst case with long, similar words gets slow, so the trie is the safer answer if you have time to write it.
Do equal words really count as pairs?+
Yes. The statement says equal words qualify, and Example 2 confirms it: two x words form a pair. A word is a suffix of itself, so don't add a special case that excludes duplicates.
What edge cases should I test?+
Test an empty array, a single word, empty strings, and many identical words. An empty string is a suffix of every word. Also return a long, because with 2000 words of identical content the pair count can be large.
How do I prepare for this in 48 hours?+
Write the brute force first and confirm both examples return 3. Then code the reversed trie once from scratch, with a counter on each node. Practice counting pairs without double counting, since that's where most wrong answers come from.