Adding Stack 2.0
Updated
Problem statement
Process a sequence of operations on a stack that starts empty. The stack supports these commands:
push v: push integerv.pop: remove the top element.inc i v: addvto each of the bottomielements.
Return an array whose kth value is the sum of all values in the stack after the kth command. The sum of an empty stack is 0.
Every operation and every reported sum must be processed in O(1) time.
Function
processAddingStack(operations: String[]) → long[]Examples
Example 1
operations = ["push 4","push 5","inc 2 1","pop","pop"]return = [4,9,11,5,0]The first two sums are 4 and 9. The increment changes the stack to [5, 6] with sum 11; the two pops then leave sums 5 and 0.
Example 2
operations = ["push 1","push 2","inc 1 10","pop"]return = [1,3,13,11]Incrementing only the bottom value changes [1, 2] to [11, 2], whose sum is 13. Popping 2 leaves a sum of 11.
Constraints
1 <= operations.length <= 2 * 10^5-10^9 <= v <= 10^9- For every
inc i v,1 <= i <= current stack size. - A
popcommand is issued only when the stack is nonempty. - Every command has exactly one of the three forms described above.
- Every stack value and reported sum fits in a signed 64-bit integer.
More IMC Trading Online Assessment questions
- Earliest Meeting Start on a 24-Hour ClockOA · Seen Nov 2026
- Grid Paths with Override PassesOA · Seen Oct 2026
- Weighted LFU CacheOA · Seen Sep 2026
- Rock JumpingOA · Seen Aug 2026
- Stack Batch RemovalOA · Seen Aug 2026
- Chain of CommandOA · Seen Aug 2026
- Choose ContainersOA · Seen Aug 2026
- Sub-matrix SumsOA · Seen Jul 2026
- Sub-matrix SumsOA · Seen Jul 2026
- Can Every Package Fit in a BoxOA · Seen Jul 2026
Similar questions at other companies
- Billing Log with Undo and RedoReddit · PHONE SCREENReddit interview questions
- Stack with Constant-Time Middle QueriesAmazon · PHONE SCREEN
- Bank Accounts with Balance HistoryMeta · OAMeta interview questions
- Compress Editor Actions into Operational Transform OperationsReplit · OA · PHONE SCREEN · ONSITE INTERVIEW
- Process Banking OperationsVercel · OA
- Layer History with Batch Undo and RedoFigma · OA · PHONE SCREENFigma interview questions
- Asteroid CollisionMicrosoft · OAMicrosoft interview questions
- Undo/Redo Timeline State EngineKickdrum · OA
- Memory AllocatorTikTok · OATikTok interview questions
- Integer Map Using a VectorAdobe · OAAdobe interview questions