Challenge: Implement Two Stacks Using One Array

Can you implement two stacks using a single array? A solution is placed in the "solution" section to help you, but we would suggest you try to solve it on your own first

Problem Statement

In this problem, using a single array to store elements, you have to implement the class TwoStacks<V>, having the following methods to generate two stacks. An illustration is also provided for your understanding.

Method Prototypes

void push1(V value)
void push2(V value)
public V pop1()
public V pop2()

Input/Output

  • push1(input)

    • Input: an integer
    • Output: inserts the given value in the first stack i.e. stack1
  • push2(input)

    • Input: an integer
    • Output: inserts the given value in the second stack i.e stack2
  • pop1()

    • Output: returns and remove the top value of stack1
  • pop2()

    • Output: returns and remove the top value of stack2

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.