First Shared Value When Popping Two Stacks
Updated
Problem statement
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[]) → intExamples
Example 1
stackA = [40,11,17,7,25,9]stackB = [40,17,3,8,11,5]return = 17The 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 = -1Popping 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 = 99 is on top of stackA twice; the first pop already finds it in stackB, so the answer is 9.
Constraints
stackAandstackBeach hold between1and20000values.- Every value is a whole number from
0to10^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