FastPrepFirst Shared Value When Popping Two Stacks
Problems/First Shared Value When Popping Two Stacks

First Shared Value When Popping Two Stacks

Capital One logoCapital One● EasyINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Two Capital One intern reports from October 2017, from Plano and Richmond, include a final-round question about the first value that two stacks have in common. One says about ten minutes were given.

Neither says what first means for stacks. Here the stacks are arrays from bottom to top, values are popped from the top of the first stack, and the answer is the first popped value that the second stack holds anywhere, or -1. Stacks may be as tall as 20000, hold numbers no smaller than 0 and no larger than 10^9, and repeat values freely. These are our choices.

Our version is about 70% the same as the reported questions.

Two stacks are given as arrays listed from bottom to top, so the last element of each array is its top. Pop values off stackA one at a time, starting from its top. Stop at the first popped value that stackB contains somewhere and return it. If stackA runs out first, return -1. The order of stackB plays no part.

Function

firstCommonInStacks(stackA: int[], stackB: int[]) → int

Examples

Example 1

stackA = [40,11,17,7,25,9]stackB = [40,17,3,8,11,5]return = 17

The top of stackA is its last element. Popping gives 9, 25 and 7, none of which stackB holds, then 17, which it does. 11 and 40 are shared too, but they lie deeper in stackA. How stackB is ordered plays no part.

Example 2

stackA = [6,6,1]stackB = [2,3]return = -1

Popping stackA gives 1, 6, 6; none of them is in stackB, so the answer is -1.

Example 3

stackA = [5,9,9]stackB = [1,9]return = 9

9 is on top of stackA twice; the first pop already finds it in stackB, so the answer is 9.

Constraints

  • stackA and stackB each hold between 1 and 20000 values.
  • Every value is a whole number from 0 to 10^9; repeats are allowed.

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

  • Next Greater Element IAmazon · ONSITE INTERVIEW
  • Filesystem Entity Total SizeGoogle · ONSITE INTERVIEWGoogle interview questions
  • KV Store with Nested TransactionsApplied Intuition · PHONE SCREEN · ONSITE INTERVIEWApplied Intuition interview questions
  • Number Words to Digits in Any LanguageMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
  • Construct a Tree from Level-Order and Inorder TraversalsSalesforce · ONSITE INTERVIEWSalesforce interview questions
  • Most Frequent Call Path in Interleaved LogsRoblox · ONSITE INTERVIEWRoblox interview questions
  • Nested Transaction Key-Value StorexAI · ONSITE INTERVIEWxAI interview questions
  • Configurable Valid BracketsMaven Clinic · PHONE SCREEN · ONSITE INTERVIEWMaven Clinic interview questions
  • Transactional In-Memory RedisHashiCorp · ONSITE INTERVIEWHashiCorp interview questions
  • Key-Value Store with Nested TransactionsRippling · PHONE SCREEN · ONSITE INTERVIEWRippling 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 firstCommonInStacks(int[] stackA, int[] stackB) {
        // Write your code here.
    }
}
Checking account…