Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

08 Stack --> 03 Implement two stacks in an array #88

Open
FazeelUsmani opened this issue Jul 20, 2021 · 0 comments
Open

08 Stack --> 03 Implement two stacks in an array #88

FazeelUsmani opened this issue Jul 20, 2021 · 0 comments

Comments

@FazeelUsmani
Copy link
Owner

  1. Implement two stacks in an array

Your task is to implement 2 stacks in one array efficiently.

Example 1:

Input:
push1(2)
push1(3)
push2(4)
pop1()
pop2()
pop2()

Output:
3 4 -1

Explanation:
push1(2) the stack1 will be {2}
push1(3) the stack1 will be {2,3}
push2(4) the stack2 will be {4}
pop1() the poped element will be 3
from stack1 and stack1 will be {2}
pop2() the poped element will be 4
from stack2 and now stack2 is empty
pop2() the stack2 is now empty hence -1 .

Your Task:
You don't need to read input or print anything. You are required to complete the 4 methods push1, push2 which takes one argument an integer 'x' to be pushed into stack one and two and pop1, pop2 which returns the integer poped out from stack one and two. If no integer is present in the array return -1.

Expected Time Complexity: O(1) for all the four methods.
Expected Auxiliary Space: O(1) for all the four methods.

Constraints:
1 <= Number of queries <= 100
1 <= value in the stack <= 100
The sum of elements in both the stacks < size of the given array

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant