Design a Stack With Increment Operation - Problem

Design a stack that supports increment operations on its elements.

Implement the CustomStack class:

  • CustomStack(int maxSize) - Initializes the object with maxSize which is the maximum number of elements in the stack.
  • void push(int x) - Adds x to the top of the stack if the stack has not reached the maxSize.
  • int pop() - Pops and returns the top of the stack or -1 if the stack is empty.
  • void inc(int k, int val) - Increments the bottom k elements of the stack by val. If there are less than k elements in the stack, increment all the elements in the stack.

Input & Output

Example 1 — Basic Operations
$ Input: operations = ["CustomStack","push","push","pop","increment","pop","pop","pop"], values = [[3],[1],[2],[],[2,100],[],[],[]]
Output: [null,null,null,2,null,101,100,-1]
💡 Note: Create stack with maxSize=3, push 1 and 2, pop returns 2, increment bottom 2 elements by 100, then pop returns 101, 100, and -1 (empty)
Example 2 — Full Stack
$ Input: operations = ["CustomStack","push","push","push","push"], values = [[2],[1],[2],[3],[4]]
Output: [null,null,null,null,null]
💡 Note: Stack has maxSize=2, so only first 2 push operations add elements, remaining pushes are ignored
Example 3 — Empty Stack Operations
$ Input: operations = ["CustomStack","pop","increment"], values = [[1],[],[3,100]]
Output: [null,-1,null]
💡 Note: Pop from empty stack returns -1, increment on empty stack does nothing

Constraints

  • 1 ≤ maxSize ≤ 1000
  • 1 ≤ x ≤ 1000
  • 1 ≤ k ≤ 1000
  • 0 ≤ val ≤ 100
  • At most 1000 calls will be made to each method of push, pop and increment.

Visualization

Tap to expand
CustomStack with Lazy Increment INPUT maxSize = 3 Stack (bottom) 1 idx 0 2 idx 1 Operations: 1. CustomStack(3) 2. push(1) 3. push(2) 4. pop() 5. inc(2, 100) 6. pop() 7. pop() 8. pop() Increment Array: 0 0 0 ALGORITHM STEPS 1 Initialize Arrays stack[] and inc[] of maxSize 2 Push Operation Add to stack, move pointer 3 Increment (Lazy) inc[min(k,top)-1] += val 4 Pop with Propagate Add inc[top] to result Execution Trace: After push(1): stack=[1], inc=[0,0,0] After push(2): stack=[1,2], inc=[0,0,0] pop(): return 2, stack=[1] inc(2,100): inc=[0,100,0] (only mark inc[0], lazy!) pop(): 1+100=101, propagate pop(): return -1 (empty) FINAL RESULT Output Array: null null null 2 null 101 100 -1 Results Explained: - null: constructor, push ops - 2: first pop (no increment) - null: increment operation - 101: 1 + 100 (incremented!) - 100: propagated increment - -1: empty stack Lazy Propagation Benefit inc(k,val) is O(1) - just mark! On pop: add inc and propagate No need to update all k elements Time: O(1) for all operations! Key Insight: Lazy propagation stores increments in a separate array instead of updating all elements immediately. When popping, add the increment value and propagate it down to the next element. This makes increment O(1) instead of O(k), achieving O(1) time complexity for ALL stack operations! TutorialsPoint - Design a Stack With Increment Operation | Lazy Propagation with Increment Array
Asked in
Amazon 12 Google 8 Microsoft 6 Facebook 4
89.6K Views
Medium Frequency
~15 min Avg. Time
1.9K 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