This is a verified interview question from Aquera-labs. Candidates reporting seeing this problem in recent Online Assessments (OAs) and onsite rounds. Mastering "Design a Stack With Increment Operation" covers key patterns like Other.
"The problem involves designing a stack with a custom increment operation. The stack should support basic stack operations like push and pop, and an additional increment operation that increments the bottom elements of the stack by a given value. ### Problem Implement a stack with the following operations: - `push(int x)`: Push element x onto stack. - `pop()`: Removes the element on top of the stack and returns it. - `increment(int k, int val)`: Increments the bottom k elements of the stack by val. ### Input - `int[] ops`: An array of operations where each operation is either "PUSH x", "POP", or "INCREMENT k val". - `int k` and `int val`: Parameters for the increment operation. ### Output - The top element of the stack after all operations are performed. ### Constraints - `1 <= n <= 100` where n is the number of operations. - `-100 <= val <= 100`. - `1 <= k <= n`. - `1 <= x <= 100` for push operations. ### Test Cases - Example 1: - Input: n = 5, ops = ["PUSH 1", "PUSH 2", "PUSH 3", "POP", "POP"] - Output: 2 - Example 2: - Input: n = 4, ops = ["PUSH 1", "PUSH 2", "PUSH 3", "POP"] - Output: 1"
Join thousands of developers practicing for Aquera-labs.