Challenge: Implement Two Stacks Using One List
Try to solve the Implement Two Stacks Using One List problem.
We'll cover the following
Statement
Design a data structure TwoStacks
, that represents two stacks using a single list, where both stacks share the same list for storing elements.
The following operations must be supported:
push1(value)
: Takes an integervalue
and inserts it into the first stack.push2(value)
: Takes an integervalue
and inserts it into the second stack.pop1()
: Removes the top element from the first stack and returns it.pop2()
: Removes the top element from the second stack and returns it.
Note: Perform all operations in-place without resizing the underlying list, maintaining a fixed size throughout.
Constraints:
list.length
value
,value2
Examples
Level up your interview prep. Join Educative to access 80+ hands-on prep courses.