Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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.Example 1:
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 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 2, 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], Do not add another elements 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 103, stack becomes [201, 202] stk.pop(); // return 202 --> Return top of the stack 202, stack becomes [201] stk.pop(); // return 201 --> Return top of the stack 201, stack becomes [] stk.pop(); // return -1 --> Stack is empty return -1.
Constraints:
1 <= maxSize, x, k <= 10000 <= val <= 1001000 calls will be made to each method of increment, push and pop each separately.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Stack · Design
["CustomStack","push","push","pop","push","push","push","increment","increment","pop","pop","pop","pop"] [[3],[1],[2],[],[2],[3],[4],[5,100],[2,100],[],[],[],[]]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1381: Design a Stack With Increment Operation
class CustomStack {
private int[] stk;
private int[] add;
private int i;
public CustomStack(int maxSize) {
stk = new int[maxSize];
add = new int[maxSize];
}
public void push(int x) {
if (i < stk.length) {
stk[i++] = x;
}
}
public int pop() {
if (i <= 0) {
return -1;
}
int ans = stk[--i] + add[i];
if (i > 0) {
add[i - 1] += add[i];
}
add[i] = 0;
return ans;
}
public void increment(int k, int val) {
if (i > 0) {
add[Math.min(i, k) - 1] += val;
}
}
}
/**
* Your CustomStack object will be instantiated and called as such:
* CustomStack obj = new CustomStack(maxSize);
* obj.push(x);
* int param_2 = obj.pop();
* obj.increment(k,val);
*/
// Accepted solution for LeetCode #1381: Design a Stack With Increment Operation
type CustomStack struct {
stk []int
add []int
i int
}
func Constructor(maxSize int) CustomStack {
return CustomStack{make([]int, maxSize), make([]int, maxSize), 0}
}
func (this *CustomStack) Push(x int) {
if this.i < len(this.stk) {
this.stk[this.i] = x
this.i++
}
}
func (this *CustomStack) Pop() int {
if this.i <= 0 {
return -1
}
this.i--
ans := this.stk[this.i] + this.add[this.i]
if this.i > 0 {
this.add[this.i-1] += this.add[this.i]
}
this.add[this.i] = 0
return ans
}
func (this *CustomStack) Increment(k int, val int) {
if this.i > 0 {
this.add[min(k, this.i)-1] += val
}
}
/**
* Your CustomStack object will be instantiated and called as such:
* obj := Constructor(maxSize);
* obj.Push(x);
* param_2 := obj.Pop();
* obj.Increment(k,val);
*/
# Accepted solution for LeetCode #1381: Design a Stack With Increment Operation
class CustomStack:
def __init__(self, maxSize: int):
self.stk = [0] * maxSize
self.add = [0] * maxSize
self.i = 0
def push(self, x: int) -> None:
if self.i < len(self.stk):
self.stk[self.i] = x
self.i += 1
def pop(self) -> int:
if self.i <= 0:
return -1
self.i -= 1
ans = self.stk[self.i] + self.add[self.i]
if self.i > 0:
self.add[self.i - 1] += self.add[self.i]
self.add[self.i] = 0
return ans
def increment(self, k: int, val: int) -> None:
i = min(k, self.i) - 1
if i >= 0:
self.add[i] += val
# Your CustomStack object will be instantiated and called as such:
# obj = CustomStack(maxSize)
# obj.push(x)
# param_2 = obj.pop()
# obj.increment(k,val)
// Accepted solution for LeetCode #1381: Design a Stack With Increment Operation
struct CustomStack {
stack: Vec<i32>,
inc: Vec<i32>,
n: usize,
max_size: usize,
}
impl CustomStack {
fn new(max_size: i32) -> Self {
let max_size = max_size as usize;
let stack = vec![];
let inc = vec![0; (1 + max_size) as usize];
let n = 0;
CustomStack {
stack,
inc,
n,
max_size,
}
}
fn push(&mut self, x: i32) {
if self.n != self.max_size {
self.stack.push(x);
self.n += 1;
}
}
fn pop(&mut self) -> i32 {
if let Some(mut top) = self.stack.pop() {
self.inc[self.n - 1] += self.inc[self.n];
top += self.inc[self.n];
self.inc[self.n] = 0;
self.n -= 1;
top
} else {
-1
}
}
fn increment(&mut self, k: i32, val: i32) {
let k = k as usize;
if k > self.n {
self.inc[self.n] += val;
} else {
self.inc[k] += val;
}
}
}
#[test]
fn test() {
let mut stack = CustomStack::new(3);
stack.push(1);
stack.push(2);
assert_eq!(stack.pop(), 2);
stack.push(2);
stack.push(3);
stack.push(4);
stack.increment(5, 100);
stack.increment(2, 100);
assert_eq!(stack.pop(), 103);
assert_eq!(stack.pop(), 202);
assert_eq!(stack.pop(), 201);
assert_eq!(stack.pop(), -1);
}
// Accepted solution for LeetCode #1381: Design a Stack With Increment Operation
class CustomStack {
private stk: number[];
private add: number[];
private i: number;
constructor(maxSize: number) {
this.stk = Array(maxSize).fill(0);
this.add = Array(maxSize).fill(0);
this.i = 0;
}
push(x: number): void {
if (this.i < this.stk.length) {
this.stk[this.i++] = x;
}
}
pop(): number {
if (this.i <= 0) {
return -1;
}
const ans = this.stk[--this.i] + this.add[this.i];
if (this.i > 0) {
this.add[this.i - 1] += this.add[this.i];
}
this.add[this.i] = 0;
return ans;
}
increment(k: number, val: number): void {
if (this.i > 0) {
this.add[Math.min(this.i, k) - 1] += val;
}
}
}
/**
* Your CustomStack object will be instantiated and called as such:
* var obj = new CustomStack(maxSize)
* obj.push(x)
* var param_2 = obj.pop()
* obj.increment(k,val)
*/
Use this to step through a reusable interview workflow for this problem.
For each element, scan left (or right) to find the next greater/smaller element. The inner scan can visit up to n elements per outer iteration, giving O(n²) total comparisons. No extra space needed beyond loop variables.
Each element is pushed onto the stack at most once and popped at most once, giving 2n total operations = O(n). The stack itself holds at most n elements in the worst case. The key insight: amortized O(1) per element despite the inner while-loop.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Pushing without popping stale elements invalidates next-greater/next-smaller logic.
Usually fails on: Indices point to blocked elements and outputs shift.
Fix: Pop while invariant is violated before pushing current element.