FastPrepSecond Distinct Value Two Arrays Share
Problems/Second Distinct Value Two Arrays Share

Second Distinct Value Two Arrays Share

Capital One logoCapital One● EasyFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A 2017 Capital One software engineer report from an on-site interview reached through a university career fair lists a coding question about two arrays. The task is to find the second element they have in common.

It does not say whose order counts, what to do with repeated values, or what to return when there is no second one. Here the order of the first array decides, a repeated value counts once, and -1 means fewer than two values are shared. Values are from 0 to 10^9 and each array has up to 20000 of them. These are our choices.

Our version is about 75% the same as the reported question.

Read a from left to right and keep track of values that also appear somewhere in b. Count each such value only the first time you meet it. Return the second value counted, or -1 if fewer than two different values of a appear in b.

Function

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

Examples

Example 1

a = [7,3,7,9,2]b = [9,7,1,2]return = 9

Reading a from the left, 7 is shared (first), 3 is not in b, the second 7 is the same value again and does not count, 9 is shared (second), and 2 is shared (third, never reached). The answer is 9.

Example 2

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

Only the value 5 is shared. It appears twice in a, but one value cannot be both the first and the second, so the answer is -1.

Example 3

a = [8,3,5]b = [5,8,2]return = 5

Reading a, the shared values come in the order 8, then 5. The second is 5, although 5 is the smaller value and heads b.

Constraints

  • 1 <= a.length, b.length <= 20000
  • 0 <= a[i], b[j] <= 10^9; values may repeat.

More Capital One Onsite Interview questions

  • Digit-Coded Card ValidationONSITE INTERVIEW · Seen Nov 2026
  • Sort ColorsONSITE INTERVIEW · Seen Oct 2026
  • Virtual Card Transaction ValidatorONSITE INTERVIEW · Seen Oct 2026
  • Banking System with Top ActivityONSITE INTERVIEW · Seen Sep 2026
  • Transaction Event StoreONSITE INTERVIEW · Seen Jul 2026
  • Shortest Path in an Unweighted GraphONSITE INTERVIEW · Seen Jul 2026
  • Duplicate Transactions Within 60 SecondsONSITE INTERVIEW · Seen May 2026
  • LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen May 2026
  • Cashback TrackerONSITE INTERVIEW · Seen Nov 2025
  • Reverse a StringOA · ONSITE INTERVIEW · Seen Oct 2025

Similar questions at other companies

  • List the Key and Value of Every Map EntryNewgen Software · ONSITE INTERVIEW
  • Pizza Price CalculatorMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
  • Cross-Day User Trust ScoresAffirm · PHONE SCREEN · ONSITE INTERVIEWAffirm interview questions
  • Map and Accumulate Learning QueriesPalantir · ONSITE INTERVIEWPalantir interview questions
  • Filter Pins by Excluded IDsPinterest · ONSITE INTERVIEWPinterest interview questions
  • Best Average Student ScoreGoldman Sachs · PHONE SCREEN · ONSITE INTERVIEWGoldman Sachs interview questions
  • Longest Dictionary Word from Nine LettersGoogle · ONSITE INTERVIEWGoogle interview questions
  • Two SumOptimus Information · ONSITE INTERVIEWOptimus Information interview questions
  • Keep the First Key for Each Distinct ValueOneTrust · ONSITE INTERVIEW
  • Two Positions That Add Up to a TargetDocuSign · PHONE SCREEN · ONSITE INTERVIEWDocuSign interview questions

Capital One interview experiences

  • Capital One Coding OA Questions: 3 Experiences
  • Capital One Experienced Engineer Power Day and OA: 6 Experiences
  • Capital One New Grad OA Questions: 14 Experiences (Part 2 of 2)
  • Capital One TDP Agentic Round (AI Coding): 7 Experiences
  • Capital One SWE Power Day, Level Not Reported: 6 Experiences
  • Capital One TIP Intern OA and Power Day: 13 Experiences
All Capital One interview questionsSee Capital One hiring insights
Saved
class Solution {
    public int secondCommon(int[] a, int[] b) {
        // Write your code here.
    }
}
Checking account…