Reported September 2021
IMCstack

Adding Stack 2.0

Reported by candidates from IMC's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live IMC OA. Under 2s to a working solution.
Founder's read

The IMC OA reported in September 2021 hands you a stack with a twist, and the whole question hinges on one data structure: a stack with lazy increments. You push, pop, and bump the bottom i elements, and you must report the running sum after every command in O(1). If your first instinct is to loop over the bottom i items on each inc, stop. That's O(n^2) on 2 * 10^5 operations and it won't pass. The trick is small once you see it. If you blank mid-assessment, StealthCoder runs invisibly on your screen as a safety net.

The problem

Process a sequence of operations on a stack that starts empty. The stack supports these commands:
push v: push integer v.
pop: remove the top element.
inc i v: add v to each of the bottom i elements.
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 pop command 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.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The pattern is a stack with lazy propagation. Keep an array of values plus a parallel array of pending increments, where add[i] means every element at index i and below gets bumped. For inc i v, just do add[i-1] += v and add the total v * i to the running sum. No loop. On pop, take the top value plus its pending add, remove it, then push that pending add down to the element beneath it so it isn't lost. Update the sum by subtracting the real popped value. The common pitfall is forgetting to carry the increment down on pop, which silently corrupts later sums. Use 64-bit integers for the sum, since values reach 10^9 across 2 * 10^5 operations. Record the sum after each command. If the carry-down logic slips under pressure, StealthCoder is the hedge during the live OA.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill Adding Stack 2.0 cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as design a stack with increment operation. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass IMC's OA.

IMC reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Adding Stack 2.0 FAQ

What's the trick in Adding Stack 2.0?+

Lazy increments. Store a pending add at index i-1 instead of touching the bottom i elements. Track the total sum separately, adding v * i on each inc. Resolve the pending add only when that element is popped, passing it down to the element below.

How hard is this IMC question really?+

Medium. The stack part is easy. The difficulty is seeing that the O(1) requirement forbids looping on inc. Once you know lazy propagation, it's about 20 lines. Most people fail by looping or by dropping the carry-down on pop.

What happens on pop with a pending increment?+

The real value is the stored value plus the pending add at that index. Subtract that from the sum, then add the pending amount to the index below, since it covered that element too. Then clear the top slot.

Do I need 64-bit integers?+

Yes. The constraints say values and sums fit in signed 64-bit. Increments of 10^9 times up to 2 * 10^5 elements overflow a 32-bit int fast. Use long for the sum, stored values, and the pending adds.

How do I prep for this in 48 hours?+

Work through the LeetCode problem Design a Stack With Increment Operation, which uses the same lazy idea. Then add the running sum yourself and test with the two given examples, including popping right after an inc.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with IMC.

OA at IMC?
Invisible during screen share
Get it