FastPrepLongest Common Prefix of Number Pairs
Problem · Hash Table

Longest Common Prefix of Number Pairs

Learn this problem
MediumCapital One logoCapital OneNEW GRADINTERNOA

Problem statement

You are given two arrays of positive integers, arr1 and arr2.

The common prefix of two integers is the longest sequence of decimal digits they share starting from the leftmost digit. For example, 54546 and 54547 have the common prefix 5454, whose length is 4.

Choose one integer from arr1 and one integer from arr2. Return the maximum common-prefix length over all such cross-array pairs. Return 0 if no pair shares its first digit.

Function

longestCommonPrefixLength(arr1: int[], arr2: int[]) → int

Examples

Example 1

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

54546 and 54547 share the four-digit prefix 5454. No cross-array pair shares a longer prefix.

Example 2

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

The complete value 544 is a three-digit prefix of 5444444.

Example 3

arr1 = [817,99]arr2 = [1999,1909]return = 0

No cross-array pair shares its first digit.

Constraints

  • The length of each input array is between 1 and 50000, inclusive.
  • Every array value is between 1 and 1000000000, inclusive.

More Capital One problems

drafts saved locally
public int longestCommonPrefixLength(int[] arr1, int[] arr2) {
  // Write your code here.
}
arr1[25,288,2655,54546,54,555]
arr2[2,255,266,244,26,5,54547]
expected4
checking account