Problem · Trie

Longest Common Prefix of Number Pairs

Learn this problem
MediumDatabricks logoDatabricksINTERNOA

Problem statement

Choose one positive integer from firstArray and one from secondArray. Their longest common prefix is the longest sequence of decimal digits shared from the leftmost digit.

Return the greatest common-prefix length over all cross-array pairs, or 0 when no pair shares its first digit.

Function

atabricksLongestCommonPrefixOfNumberPairs(firstArray: int[], secondArray: int[]) → int

Examples

Example 1

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

54546 and 54547 share the four-digit prefix 5454.

Example 2

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

544 is a complete three-digit prefix of 5444444.

Example 3

firstArray = [817, 99]secondArray = [1999, 1909]return = 0

No cross-array pair shares its first digit.

Constraints

  • 1 ≤ firstArray.length ≤ 5 · 10^4
  • 1 ≤ firstArray[i] ≤ 10^9

More Databricks problems

drafts saved locally
public int atabricksLongestCommonPrefixOfNumberPairs(int[] firstArray, int[] secondArray) {
  // write your code here
}
firstArray[25, 288, 2655, 54546, 54, 555]
secondArray[2, 255, 266, 244, 26, 5, 54547]
expected4
checking account