Design a fixed-size stack that can add a value to its bottom k elements

Quick Overview

Design a fixed-capacity stack with push, pop and an increment that adds a value to the bottom k elements, following exact rules for full and empty stacks. It tests whether you can replace a per-element loop with pending increments and argue that every popped value stays correct.

Design a fixed-size stack that can add a value to its bottom k elements

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Design a stack that supports an increment operation on its elements. The stack has a maximum size. Pushing onto a full stack does nothing. Popping from an empty stack returns `-1`. The `increment` operation adds a value to the bottom `k` elements of the stack. Implement the class `CustomStack` with these methods: - `CustomStack(maxSize: int)`: Creates an empty stack that holds at most `maxSize` elements. - `push(x: int)`: Adds `x` to the top of the stack if the stack has not reached `maxSize`. - `pop() -> int`: Pops and returns the top of the stack, or `-1` if the stack is empty. - `increment(k: int, val: int)`: Adds `val` to the bottom `k` elements of the stack. If there are fewer than `k` elements, adds `val` to all of them. In the explanations below, stack contents are listed bottom to top. Example 1 has capacity 3, a push onto a full stack, and an increment whose `k` is larger than the stack: ```text 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] Explanation: stack = CustomStack(3) # maxSize = 3 stack.push(1) # stack: [1] stack.push(2) # stack: [1, 2] stack.pop() # -> 2 (stack: [1]) stack.push(2) # stack: [1, 2] stack.push(3) # stack: [1, 2, 3] stack.push(4) # full, ignored; stack: [1, 2, 3] stack.increment(5, 100) # only 3 elements, all get +100; stack: [101, 102, 103] stack.increment(2, 100) # bottom 2 get +100; stack: [201, 202, 103] stack.pop() # -> 103 (stack: [201, 202]) stack.pop() # -> 202 (stack: [201]) stack.pop() # -> 201 (stack: []) stack.pop() # -> -1 (empty) ``` Example 2: ```text Input: ["CustomStack", "pop", "push", "push", "increment", "pop", "pop"] [[2], [], [10], [20], [1, 5], [], []] Output: [null, -1, null, null, null, 20, 15] Explanation: stack = CustomStack(2) # maxSize = 2 stack.pop() # -> -1 (empty) stack.push(10) # stack: [10] stack.push(20) # stack: [10, 20] stack.increment(1, 5) # adds 5 to the bottom 1; stack: [15, 20] stack.pop() # -> 20 (stack: [15]) stack.pop() # -> 15 (stack: []) ``` ```hint Where the cost goes A direct loop makes `increment` cost O(k). Ask when anyone actually observes an element's value, and whether the addition has to happen before that moment. ``` ```hint Pending amounts Try recording each increment at a single position and settling it later. Work out what has to happen to that pending amount when the element holding it is popped. ``` ### Constraints & Assumptions - `1 <= maxSize <= 1000` - `1 <= x <= 1000` - `1 <= k <= 1000` - `0 <= val <= 100` - At most `1000` calls are made to each of `increment`, `push` and `pop`. ### What a Strong Answer Covers - Correct behavior at the boundaries: a push on a full stack, a pop on an empty stack, an increment on an empty stack, and a `k` larger than the current size. - The direct O(k) increment and its cost under the given limits, plus a design that makes every operation O(1). - An argument for why each popped value includes exactly the increments that covered it, and none made before it was pushed. - Time and space complexity of the chosen design. ### Follow-up Questions - How would you add a `peek()` that returns the top value, with increments applied, without removing it? - How would you change the design if `increment` had to add `val` to the top `k` elements instead of the bottom `k`? - What changes if the stack has no fixed capacity and can grow without limit?

Overview: Design a fixed-capacity stack with push, pop and an increment that adds a value to the bottom k elements, following exact rules for full and empty stacks. It tests whether you can replace a per-element loop with pending increments and argue that every popped value stays correct.

General logo
General
Nov 24, 2026
mediumSoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design a stack that supports an increment operation on its elements.

The stack has a maximum size. Pushing onto a full stack does nothing. Popping from an empty stack returns -1. The increment operation adds a value to the bottom k elements of the stack.

Implement the class CustomStack with these methods:

  • CustomStack(maxSize: int) : Creates an empty stack that holds at most maxSize elements.
  • push(x: int) : Adds x to the top of the stack if the stack has not reached maxSize .
  • pop() -> int : Pops and returns the top of the stack, or -1 if the stack is empty.
  • increment(k: int, val: int) : Adds val to the bottom k elements of the stack. If there are fewer than k elements, adds val to all of them.

In the explanations below, stack contents are listed bottom to top.

Example 1 has capacity 3, a push onto a full stack, and an increment whose k is larger than the stack:

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]

Explanation:
stack = CustomStack(3)      # maxSize = 3
stack.push(1)               # stack: [1]
stack.push(2)               # stack: [1, 2]
stack.pop()                 # -> 2    (stack: [1])
stack.push(2)               # stack: [1, 2]
stack.push(3)               # stack: [1, 2, 3]
stack.push(4)               # full, ignored; stack: [1, 2, 3]
stack.increment(5, 100)     # only 3 elements, all get +100; stack: [101, 102, 103]
stack.increment(2, 100)     # bottom 2 get +100; stack: [201, 202, 103]
stack.pop()                 # -> 103  (stack: [201, 202])
stack.pop()                 # -> 202  (stack: [201])
stack.pop()                 # -> 201  (stack: [])
stack.pop()                 # -> -1   (empty)

Example 2:

Input:
["CustomStack", "pop", "push", "push", "increment", "pop", "pop"]
[[2], [], [10], [20], [1, 5], [], []]

Output: [null, -1, null, null, null, 20, 15]

Explanation:
stack = CustomStack(2)      # maxSize = 2
stack.pop()                 # -> -1   (empty)
stack.push(10)              # stack: [10]
stack.push(20)              # stack: [10, 20]
stack.increment(1, 5)       # adds 5 to the bottom 1; stack: [15, 20]
stack.pop()                 # -> 20   (stack: [15])
stack.pop()                 # -> 15   (stack: [])

Constraints & Assumptions

  • 1 <= maxSize <= 1000
  • 1 <= x <= 1000
  • 1 <= k <= 1000
  • 0 <= val <= 100
  • At most 1000 calls are made to each of increment , push and pop .

What a Strong Answer Covers Guidance

  • Correct behavior at the boundaries: a push on a full stack, a pop on an empty stack, an increment on an empty stack, and a k larger than the current size.
  • The direct O(k) increment and its cost under the given limits, plus a design that makes every operation O(1).
  • An argument for why each popped value includes exactly the increments that covered it, and none made before it was pushed.
  • Time and space complexity of the chosen design.

Follow-up Questions Guidance

  • How would you add a peek() that returns the top value, with increments applied, without removing it?
  • How would you change the design if increment had to add val to the top k elements instead of the bottom k ?
  • What changes if the stack has no fixed capacity and can grow without limit?