Maximum Common Decimal Prefix Across Arrays
Reported by candidates from ZipRecruiter's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The mistake that sinks a first attempt here is comparing every pair of numbers. With 100000 values in each array, that's 10 billion comparisons and a guaranteed timeout. This ZipRecruiter OA, reported in September 2022, asks for the longest shared decimal prefix between any number in first and any number in second. It's a prefix problem wearing a math costume. Convert to strings, store prefixes, and the nested loop disappears. If you blank on the setup during the live assessment, StealthCoder runs invisibly on your screen and can hand you the structure so you're not stuck staring at a timer.
The problem
You are given two arrays of positive integers, first and second. Choose one value from each array and compare their ordinary decimal representations. Return the maximum common-prefix length over every cross-array pair. Function maximumCommonDecimalPrefix(first: int[], second: int[]) → int Examples Example 1 first = [12345,9012] second = [12222,1,123902] return = 3 12345 and 123902 share prefix 123, of length three. Example 2 first = [7,80] second = [6,90] return = 0 No cross-array pair starts with the same digit. Constraints 1 <= first.length,second.length <= 100000 1 <= value <= 1000000000
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is to stop thinking in pairs. Every value is at most 10 digits, so it has at most 10 prefixes. Put every prefix of every number in first into a hash set. Then walk each number in second, check its prefixes from longest to shortest, and keep the longest one found in the set. That's about 10 operations per number, so roughly 2 million set operations total. A trie works the same way, but the set is faster to write. The common pitfall is the nested loop over both arrays, which is far too slow at 100000 each. Another is comparing numerically instead of as strings, which breaks on values like 1 versus 123902. Prefixes must match from the leading digit. Return 0 if nothing matches, as in Example 2. If your mind goes blank mid-assessment, StealthCoder is the safety net that reads the problem and gives you the approach.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Maximum Common Decimal Prefix Across Arrays 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 passed his OA cold and still thinks the filter is broken.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as find the length of the longest common prefix. 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 passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Maximum Common Decimal Prefix Across Arrays FAQ
What's the trick to Maximum Common Decimal Prefix Across Arrays?+
Avoid comparing pairs. Convert each number in the first array to a string and insert all of its prefixes into a hash set. Then check each number in the second array against that set, longest prefix first. The answer is the longest match found anywhere.
What's the time complexity of the optimal solution?+
About O((n + m) * d), where d is the digit count, at most 10 here. With 100000 values per array, that's a couple of million operations. The brute-force pairwise approach is O(n * m * d), which is far too slow at these sizes.
Should I use a trie or a hash set?+
Either works. A hash set of prefix strings is quicker to write and hard to get wrong under pressure. A trie uses less repeated string work, but the extra code adds bug risk. For a timed OA, I'd pick the set unless you already know trie code cold.
What edge cases should I test before submitting?+
Test Example 2, where no first digits match and the answer is 0. Test single-element arrays, and numbers of different lengths such as 1 against 123902. Also test duplicates and a value of 1000000000, which has 10 digits.
How do I prepare for this in 48 hours?+
Practice the pattern of turning a pairwise problem into a precomputed lookup. Write the prefix-set solution from scratch twice. Be comfortable converting integers to strings and slicing them. That covers this ZipRecruiter question and similar prefix or lookup problems.