Description
Solutions
Longest Common Prefix of Number Pairs
🤘 INTERN

note - feel free to see the Problem Source at the bottom of the page for the original problem description :)

Imagine you have two arrays of numbers, firstArray and secondArray, and your task is to discover the longest common prefix (LCP) that can be found between any pair of numbers, each from a different array. A prefix is formed by one or more digits starting from the leftmost digit of a number. For example, the number 123 has prefixes like 1, 12, and 123 itself. If you look at numbers 5645539 and 564554, their longest common prefix is 56455. However, if you compare 123 and 456, there is no common prefix at all. Your goal is to determine the length of the longest common prefix found between these two arrays, and if there isn't one, you'll simply return zero.

Example 1:

Input:  firstArray = [25, 288, 2655, 54546, 54, 555], secondArray = [2, 255, 266, 244, 26, 5, 54547]
Output: 4
Explanation:

The best pair is 54546 from the first array and 54547 from the second array with the LCP 5454, where 5454 is of length 4.

Example 2:

Input:  firstArray = [25, 288, 2655, 544, 54, 555], secondArray = [2, 255, 266, 244, 26, 5, 5444444]
Output: 3
Explanation:

The best pair is 544 from the first array and 5444444 from the second array with the LCP 544, where 544 is of length 3.

Example 3:

Input:  firstArray = [817, 99], secondArray = [1999, 1909]
Output: 0
Explanation:

No pair of numbers from different arrays has a common prefix, hence the answer is 0.

Constraints:
    • 1 ≤ firstArray.length ≤ 5 · 10^4
    • 1 ≤ firstArray[i] ≤ 10^9
Thumbnail 0
Thumbnail 1
Testcase

Result
Case 1

input:

output: