Recover the Stack After a Worker
Updated
Problem statement
Given two integer arrays pushed and popped, return what is left on a stack after a worker has finished with it, from bottom to top. If the worker could not have popped the values in the order given by popped, return [-1].
The worker starts with an empty stack and pushes the values of pushed one at a time, in order, until every value has been pushed. At any moment, including after the last push, it may pop the value on top of the stack. popped lists every value the worker popped, in the order it popped them. Values that were never popped stay on the stack.
Function
recoverStack(pushed: int[], popped: int[]) → int[]Examples
Example 1
pushed = [3,1,4,5,9,2]popped = [4,5,1]return = [3,9,2]The worker pushes 3, 1 and 4, pops 4, pushes and pops 5, then pops 1. It then pushes 9 and 2, which are never popped, so the stack is 3, 9, 2 from bottom to top.
Example 2
pushed = [5,8,2,7,4]popped = [7,2,4,5]return = [-1]To pop 7, the worker must first push 5, 8, 2 and 7. After it pops 7 and 2 and then pushes and pops 4, the stack is 5, 8 with 8 on top, so 5 cannot be popped next.
Example 3
pushed = [6,2,8]popped = [2,8,6]return = []The worker pushes 6 and 2 and pops 2, then pushes 8 and pops 8 and 6. Every value was popped, so the stack is empty.
Constraints
1 <= pushed.length <= 2 * 10^40 <= pushed[i] <= 10^5- All the values of
pushedare unique. 0 <= popped.length <= pushed.length- All the values of
poppedare unique. - Every value of
poppedis inpushed.
More Citadel Phone Screen questions
- Speed Up a Slow FunctionOA · PHONE SCREEN · Seen Nov 2026
- Portfolio Tickers with Constant-Time Random PickPHONE SCREEN · Seen Nov 2026
- Square Root FunctionPHONE SCREEN · Seen Oct 2026
- Reachable Transfer PositionsPHONE SCREEN · Seen Oct 2026
- Spreadsheet Formula DependenciesPHONE SCREEN · Seen Oct 2026
- Currency Arbitrage CyclePHONE SCREEN · Seen Sep 2026
- Order Book with Cancel by Order IDPHONE SCREEN · Seen Sep 2026
- Smart Queue of Latest PricesPHONE SCREEN · Seen Sep 2026
- Merge Price-Delta FeedsPHONE SCREEN · Seen Sep 2026
- Reconcile Unmatched Trades with Timestamp TolerancePHONE SCREEN · Seen Sep 2026
Similar questions at other companies
- Asteroid CollisionSiemens · PHONE SCREEN
- Billing Log with Undo and RedoReddit · PHONE SCREENReddit interview questions
- Compress Editor Actions into Operational Transform OperationsReplit · OA · PHONE SCREEN · ONSITE INTERVIEW
- Stack with Constant-Time Middle QueriesAmazon · PHONE SCREEN
- Generic Stack Operation SequenceArista Networks · PHONE SCREENArista Networks interview questions
- Confirmed Profiling Events with First-Observed StartsAnthropic · PHONE SCREEN · ONSITE INTERVIEWAnthropic interview questions
- Fountain SafetyGoogle · ONSITE INTERVIEWGoogle interview questions
- Asteroid CollisionMicrosoft · OAMicrosoft interview questions
- Surviving Balls after Directional CollisionsSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Asteroid CollisionAdobe · ONSITE INTERVIEWAdobe interview questions
Citadel interview experiences
- Citadel Securities Quant Researcher Intern Loop: 1 Experience
- Citadel Securities Research Engineer Interview: 1 Experience
- Citadel Securities SWE Intern First Round: 2 Experiences
- Citadel Securities Experienced SWE Interviews: 1 Experience
- Citadel SWE Intern Later Rounds and Superday: 13 Experiences
- Citadel SWE Intern First-Round Questions: 20 Experiences