Design a Stack With Increment Operation

MediumStackdesign

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Design a bounded stack of integers that also supports adding a value to its **bottom** elements. - CustomStack(maxSize) creates an empty stack that can hold at most maxSize elements. - push(x) pushes x on top if the stack holds fewer than maxSize elements; otherwise it does nothing. - pop() removes and returns the top element, or returns -1 if the stack is empty. - increment(k, val) adds val to each of the bottom k elements. If the stack holds fewer than k elements, every element is incremented. All three operations can be done in O(1).

Examples

Input: ["CustomStack","push","push","push","increment","pop","pop","pop"] [[2],[4],[5],[6],[1,10],[],[],[]]
Output: [null,null,null,null,null,5,14,-1]
The stack is full after two pushes, so 6 is ignored. The increment only touches the bottom element 4, making it 14.
Input: ["CustomStack","push","push","push","increment","pop","push","increment","pop","pop"] [[4],[1],[2],[3],[10,5],[],[7],[2,1],[],[]]
Output: [null,null,null,null,null,8,null,null,7,8]
Incrementing 10 elements with only 3 present changes all of them to 6, 7, 8. After popping 8 and pushing 7, the bottom two become 7 and 8.

Constraints

  • `1 <= maxSize, x, k <= 1000`
  • `0 <= val <= 100`
  • At most 1000 calls to each method.

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More design problems