Stack Built on a Linked List
Updated
Problem statement
Build an integer stack whose storage is a chain of linked nodes, then run a list of commands on it. Command i is commands[i]:
"push": addvalues[i]as the new top element."pop": take the top element off and write it to the output. On an empty stack, write-1."peek": write the top element to the output and leave the stack as it is. On an empty stack, write-1."size": write the number of elements in the stack.
When commands[i] is not "push", values[i] is 0 and is ignored. Return everything written to the output, in order.
Function
runStack(commands: String[], values: int[]) → int[]Examples
Example 1
commands = ["push","peek","push","push","pop","pop","size","pop","pop"]values = [9,0,2,5,0,0,0,0,0]return = [9,5,2,1,9,-1]peek right after the first push reads 9. Then 2 and 5 go on top, and the two pops return them newest first: 5, then 2. One value is left, so size is 1. The next pop returns 9, and the last one finds the stack empty and gives -1.
Example 2
commands = ["size","peek"]values = [0,0]return = [0,-1]Nothing was pushed, so size is 0 and peek gives -1.
Example 3
commands = ["push","push","pop","size"]values = [0,0,0,0]return = [0,1]Two zeros are pushed. pop returns the top one, 0, and one value remains.
Constraints
1 <= commands.length <= 5000values.length == commands.length- Each command is
"push","pop","peek"or"size". 0 <= values[i] <= 10^5. Ifcommands[i]is not"push", thenvalues[i] == 0.
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
- Max StackLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- Max StackZipline · ONSITE INTERVIEW
- Stack with Middle OperationsMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Max StackBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Implement a Queue Using Two StacksAmazon · ONSITE INTERVIEW
- Design Linked ListThe Walt Disney Company · OA · ONSITE INTERVIEWThe Walt Disney Company interview questions
- Command Undo Data StructureNetflix · ONSITE INTERVIEWNetflix interview questions
- Stack from Queues and Queue from StacksBank of America · ONSITE INTERVIEWBank of America interview questions
- A Queue Built From Two StacksExpedia · ONSITE INTERVIEWExpedia interview questions
- Implement a Basic StackAMD · ONSITE INTERVIEWAMD 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