Design a Stack With Increment Operation - Problem

Design a custom stack data structure that supports traditional stack operations (push and pop) along with a special increment operation that can modify multiple elements at once.

Your task is to implement the CustomStack class with the following methods:

  • CustomStack(int maxSize): Initialize the stack with a maximum capacity
  • void push(int x): Add element x to the top of the stack (only if capacity allows)
  • int pop(): Remove and return the top element, or -1 if empty
  • void inc(int k, int val): Add val to the bottom k elements of the stack

Challenge: The increment operation should be efficient even when performed frequently on large stacks!

Input & Output

basic_operations.py โ€” Python
$ Input: ["CustomStack","push","push","pop","push","push","push","increment","increment","pop","pop","pop","pop"] [[3],[1],[2],[],[2],[3],[4],[5,100],[2,100],[],[],[],[]]
โ€บ Output: [null,null,null,2,null,null,null,null,null,103,202,201,-1]
๐Ÿ’ก Note: CustomStack stk = new CustomStack(3); // Stack is Empty [] stk.push(1); // stack becomes [1] stk.push(2); // stack becomes [1, 2] stk.pop(); // return 2 --> Return top of the stack, stack becomes [1] stk.push(2); // stack becomes [1, 2] stk.push(3); // stack becomes [1, 2, 3] stk.push(4); // stack still [1, 2, 3], Don't add another element as size is 4 stk.increment(5,100); // stack becomes [101, 102, 103] stk.increment(2,100); // stack becomes [201, 202, 103] stk.pop(); // return 103 --> Return top of the stack, stack becomes [201, 202] stk.pop(); // return 202 --> Return top of the stack, stack becomes [201] stk.pop(); // return 201 --> Return top of the stack, stack becomes [] stk.pop(); // return -1 --> Stack is empty return -1.
capacity_limits.py โ€” Python
$ Input: ["CustomStack","push","push","push","push","increment","pop"] [[2],[1],[2],[3],[4],[2,10],[]]
โ€บ Output: [null,null,null,null,null,null,12]
๐Ÿ’ก Note: Stack with capacity 2. Push 1,2 successfully, push 3,4 ignored due to capacity. increment(2,10) adds 10 to bottom 2 elements. Pop returns 2+10=12.
empty_stack_edge_case.py โ€” Python
$ Input: ["CustomStack","pop","increment","pop"] [[1],[],[1,100],[]]
โ€บ Output: [null,-1,null,-1]
๐Ÿ’ก Note: Operations on empty stack. Pop returns -1, increment on empty stack does nothing, pop still returns -1.

Constraints

  • 1 โ‰ค maxSize โ‰ค 1000
  • 1 โ‰ค x โ‰ค 1000
  • 1 โ‰ค k โ‰ค 1000
  • 0 โ‰ค val โ‰ค 100
  • At most 1000 calls will be made to each of push, pop and increment

Visualization

Tap to expand
Custom Stack with Lazy IncrementMain Stack4 (top)321Increment Array00+100increment(3,10)After PopReturns: 4O(1) operation!Bonus applied lazily๐Ÿ’ก Key Insight: Defer increment work until pop() - makes increment O(1)Lazy Propagation Technique
Understanding the Visualization
1
Stack Setup
Initialize empty stack with maximum capacity
2
Push Operations
Add elements to top of stack (like stacking plates)
3
Increment Magic
Mark bottom k elements for future bonus (lazy approach)
4
Pop with Bonus
Remove top element and apply accumulated bonuses
Key Takeaway
๐ŸŽฏ Key Insight: Lazy propagation transforms an O(k) increment operation into O(1) by deferring the actual work until elements are popped
Asked in
Google 25 Amazon 18 Microsoft 12 Apple 8
46.2K Views
Medium Frequency
~15 min Avg. Time
1.6K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen