Find the Length of the Longest Common Prefix
Updated
Problem statement
You are given two arrays with positive integers arr1 and arr2.
A prefix of a positive integer is an integer formed by one or more of its digits, starting from its leftmost digit. For example, 123 is a prefix of the integer 12345, while 234 is not.
A common prefix of two integers a and b is an integer c, such that c is a prefix of both a and b. For example, 5655359 and 56554 have a common prefix 565 while 1223 and 43456 do not have a common prefix.
You need to find the length of the longest common prefix between all pairs of integers (x, y) such that x belongs to arr1 and y belongs to arr2.
Return the length of the longest common prefix among all pairs. If no common prefix exists among them, return 0.
Function
longestCommonPrefixLength(arr1: int[], arr2: int[]) → intExamples
Example 1
arr1 = [1,10,100]arr2 = [1000]return = 3There are 3 pairs (arr1[i], arr2[j]):
- The longest common prefix of (1, 1000) is 1.
- The longest common prefix of (10, 1000) is 10.
- The longest common prefix of (100, 1000) is 100.
The longest common prefix is 100 with a length of 3.
Example 2
arr1 = [1,2,3]arr2 = [4,4,4]return = 0There exists no common prefix for any pair (arr1[i], arr2[j]), hence we return 0. Note that common prefixes between elements of the same array do not count.
Constraints
1 ≤ arr1.length, arr2.length ≤ 5 * 10^41 ≤ arr1[i], arr2[i] ≤ 10^8
More Coinbase interview questions
- Select Transactions with Child-Pays-for-Parent PackagesPHONE SCREEN · Seen Oct 2026
- Scheduled Payments with Account AliasesONSITE INTERVIEW · Seen Oct 2026
- Task Management SystemOA · Seen Sep 2026
- Cloud Compute Credits SystemOA · Seen Aug 2026
- Dynamic Fixed-K Score LeaderboardONSITE INTERVIEW · Seen Aug 2026
- Minimum Days for Fixed-Order Tasks with CooldownONSITE INTERVIEW · Seen Aug 2026
- Sliding-Window Averages as Reduced FractionsONSITE INTERVIEW · Seen Aug 2026
- Parse and Order Structured LogsONSITE INTERVIEW · Seen Aug 2026
- Banking System with Historical SnapshotsOA · Seen Aug 2026
- Lexicographically Smallest Topological OrderPHONE SCREEN · Seen Apr 2022
Similar questions at other companies
- Perfect SubstringAXQ Capital · OA
- Count Suffix Word PairsTikTok · OATikTok interview questions
- Password Clues and the Next CommandOpenAI · OAOpenAI interview questions
- Decode Words with Anagram KeysCresta · OACresta interview questions
- Count One-Swap Number PairsCapital One · OACapital One interview questions
- Parcel Event TrackingAirbnb · OAAirbnb interview questions
- Maximum Valid Substring FrequencyMicrosoft · OAMicrosoft interview questions
- Longest Distinct StringSuperhuman · OASuperhuman interview questions
- Longest String ChainVisa · OAVisa interview questions
- Longest Substring Without Repeating CharactersAkuna Capital · OAAkuna Capital interview questions