FastPrepFirst Element Two Sets Share

First Element Two Sets Share

Capital One logoCapital One● EasyNEW GRADONSITE INTERVIEW

Updated

Problem statement

Arrays a and b each hold a set of distinct non-negative integers. Going through a from left to right, return the first value that also appears somewhere in b. If no value appears in both, return -1.

Function

firstCommon(a: int[], b: int[]) → int

Examples

Example 1

a = [9,4,17,2]b = [2,17,30]return = 17

Both 17 and 2 are in both sets. Order comes from a: 17 appears there before 2, so the answer is 17, even though 2 comes first in b.

Example 2

a = [5,6]b = [7,8]return = -1

No value is in both sets, so the answer is -1.

Example 3

a = [0,12]b = [12,0]return = 0

Both values are shared. 0 comes first in a, and 0 is a real value here, not a missing answer.

Constraints

  • 1 <= a.length, b.length <= 20000
  • 0 <= a[i], b[j] <= 10^9
  • The values inside a are distinct, and so are the values inside b.

More Capital One Onsite Interview questions

Similar questions at other companies

Capital One interview experiences

All Capital One interview questionsSee Capital One hiring insights
Saved
class Solution {
    public int firstCommon(int[] a, int[] b) {
        // Write your code here.
    }
}
Checking account…