Find Length of Longest Common Prefix
Updated
Problem statement
You are given two arrays of positive integers, arr1 and arr2. Choose one number from arr1 and one number from arr2. Look at the digits of both numbers from the left. Their common prefix is the longest run of leading digits that both numbers share. Find the length of the longest common prefix over all such pairs.
For example, 123 and 12345 share the prefix 123, which has length 3. If no pair shares even the first digit, return 0.
Complete the function findLengthOfLongestCommonPrefix in the editor.
findLengthOfLongestCommonPrefix has the following parameters:
int[] arr1: the first array of positive integersint[] arr2: the second array of positive integers
The function returns an int: the length of the longest common prefix of any pair of numbers, one from each array.
Function
findLengthOfLongestCommonPrefix(arr1: int[], arr2: int[]) → intExamples
Example 1
arr1 = [123, 4, 5, 955]arr2 = [12345, 63, 95, 2]return = 3The number 123 from arr1 is a prefix of 12345 from arr2, so that pair shares 3 digits. The pair 955 and 95 shares only 2 digits. All other pairs share 0 or 1 digit. The best length is 3.
Constraints
1 ≤ arr1.length, arr2.length ≤ 5 × 1041 ≤ arr1[i], arr2[i] ≤ 108
More Uber interview questions
- Random Bingo Card GeneratorONSITE INTERVIEW · Seen Dec 2026
- Travelers per Consecutive City TripletONSITE INTERVIEW · Seen Dec 2026
- Worst-Performing Ads by CTR in a Campaign TreePHONE SCREEN · Seen Nov 2026
- Earliest Time All Users Are ConnectedPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Max Points on a LinePHONE SCREEN · Seen Oct 2026
- Online Meeting Scheduler with Fixed RoomsONSITE INTERVIEW · Seen Oct 2026
- Convex Function MinimizationPHONE SCREEN · Seen Oct 2026
- Meeting Rooms IIPHONE SCREEN · Seen Oct 2026
- Merge Meeting IntervalsPHONE SCREEN · Seen Oct 2026
- Minimum Meeting Rooms with AssignmentsPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Suggested ProductsSquarepoint Capital · OASquarepoint Capital interview questions
- The Huffman Decoder 🌼Wolverine Trading · OAWolverine Trading interview questions
- Count Words with Each PrefixCapital One · OACapital One interview questions
- Max DistanceGoogle · OAGoogle interview questions
- Find All Word Occurrences in TextSentry · PHONE SCREENSentry interview questions
- Highlight and Rank Overlapping CitationsHarvey · PHONE SCREEN · ONSITE INTERVIEWHarvey interview questions
- Fuzzy Word Lookup With One Misspelled LetterMeta · PHONE SCREENMeta interview questions
- Concatenated WordsAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Keyboard Autocomplete SuggestionsLyft · ONSITE INTERVIEWLyft interview questions
- Design Search Autocomplete SystemRoblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions