Implement a Queue using two stack s1 and s2.
Example 1:
Input:
enqueue(2)
enqueue(3)
dequeue()
enqueue(4)
dequeue()
Output: 2 3
Explanation:
enqueue(2) the queue will be {2}
enqueue(3) the queue will be {3 2}
dequeue() the poped element will be 2 
the stack will be {3}
enqueue(4) the stack will be {4 3}
dequeue() the poped element will be 3.  
Example 2:
Input:
enqueue(2)
dequeue()
dequeue()
Output: 2 -1
Your Task:
Since this is a function problem, you don't need to take inputs. You are required to complete the two methods enqueue() which takes an integer 'x' as input denoting the element to be pushed into the queue and dequeue() which returns the integer poped out from the queue.
Expected Time Complexity: O(1) for enqueue() and O(n) for dequeue()
Expected Auxiliary Space: O(1) for both enqueue() and dequeue()
Constraints:
1 <= Number of queries <= 100
1 <= values of the stack <= 100

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Starter code: #User function Template for python3

class Queue:
    def enqueue(self,X):
        # code here
    def dequeue():
        # code here

Solutions:
class Queue:

	def __init__(self):
		self.s1 = []
		self.s2 = []

	def enqueue(self, X):
		self.s1.append(X)

	def dequeue(self):
		if len(self.s2) != 0:
			return self.s2.pop()
		while len(self.s1) != 0:
			self.s2.append(self.s1.pop())
		return self.s2.pop()


class Queue:

	def __init__(self):
		self.s1 = []

	def enqueue(self, X):
		self.s1.append(X)

	def dequeue(self):
		return self.s1.pop(0)


class Queue:

	def __init__(self):
		self.s1 = []
		self.s2 = []

	def enqueue(self, X):
		self.s1.append(X)

	def dequeue(self):
		if not self.s2:
			while self.s1:
				self.s2.append(self.s1.pop())
		return self.s2.pop()


class Queue:

	def __init__(self):
		self.stack = []

	def enqueue(self, X):
		self.stack = [X] + self.stack

	def dequeue(self):
		if len(self.stack) == 0:
			return -1
		return self.stack.pop()


class Queue:

	def __init__(self):
		self.input = []
		self.output = []
		self.res = 0

	def isEmpty(x):
		if len(x) == 0:
			return True
		else:
			return False

	def enqueue(self, X):
		self.input.append(X)

	def dequeue(self):
		if len(self.output) == 0:
			while len(self.input) != 0:
				self.output.append(self.input.pop())
		self.res = self.output.pop()
		return self.res


from collections import deque

class Queue:

	def __init__(self):
		self.stack1 = deque()
		self.stack2 = deque()

	def enqueue(self, X):
		self.stack1.append(X)

	def dequeue(self):
		self.peek()
		return self.stack2.pop()

	def peek(self):
		if not self.stack2:
			while self.stack1:
				self.stack2.append(self.stack1.pop())
		return self.stack2[-1]


class Queue:

	def __init__(self):
		self.queue = []

	def enqueue(self, X):
		self.queue.append(X)

	def dequeue(self):
		if len(self.queue) == 0:
			return -1
		else:
			return self.queue.pop(0)


class Queue:

	def __init__(self):
		self.stack1 = []
		self.stack2 = []

	def enqueue(self, X):
		self.stack1.append(X)

	def dequeue(self):
		res = -1
		if len(self.stack2) != 0:
			res = self.stack2.pop()
		elif len(self.stack1) != 0:
			while len(self.stack1) != 0:
				self.stack2.append(self.stack1.pop())
			res = self.stack2.pop()
		return res


class Queue:

	def __init__(self):
		self.st1 = []
		self.st2 = []

	def enqueue(self, X):
		while self.st2:
			self.st1.append(self.st2.pop())
		self.st2.append(X)
		while self.st1:
			self.st2.append(self.st1.pop())

	def dequeue(self):
		if self.st2:
			return self.st2.pop()
		else:
			return -1


class Queue:

	def __init__(self):
		self.s1 = []
		self.s2 = []

	def enqueue(self, X):
		self.s1.append(X)
		return self.s1

	def dequeue(self):
		n = len(self.s2)
		if n != 0:
			return self.s2.pop(n - 1)
		else:
			while self.s1:
				a = self.s1.pop(n - 1)
				self.s2.append(a)
			return self.s2.pop(n - 1)


class Queue:

	def __init__(self):
		self.s1 = []
		self.s2 = []

	def enqueue(self, X):
		self.s1.append(X)

	def dequeue(self):
		while len(self.s1) != 1:
			self.s2.append(self.s1.pop())
		element = self.s1.pop()
		while len(self.s2) != 0:
			self.s1.append(self.s2.pop())
		return element


class Queue:

	def __init__(self):
		self.stk1 = []
		self.stk2 = []

	def enqueue(self, X):
		self.stk1.append(X)

	def dequeue(self):
		if self.stk2:
			return self.stk2.pop()
		while self.stk1:
			self.stk2.append(self.stk1.pop())
		if self.stk2:
			return self.stk2.pop()
		return -1


global arr

class Queue:

	def __init__(self):
		self.arr = []

	def enqueue(self, X):
		self.arr.append(X)

	def dequeue(self):
		if len(self.arr) > 0:
			return self.arr.pop(0)
		else:
			return -1


first = []
second = []

class Queue:

	def __init__(self):
		self.first = []
		self.second = []

	def enqueue(self, X):
		self.first.append(X)

	def dequeue(self):
		if len(self.second):
			return self.second.pop()
		else:
			y = len(self.first)
			if y == 0:
				return -1
			while y != 0:
				self.second.append(self.first.pop())
				y -= 1
			return self.second.pop()


class Queue:

	def __init__(self):
		self.s1 = []
		self.s2 = []

	def enqueue(self, x):
		self.s1.append(x)

	def dequeue(self):
		if not self.s2:
			while self.s1:
				self.s2.append(self.s1.pop())
		if not self.s2:
			return -1
		return self.s2.pop()


class Queue:

	def __init__(self):
		self.queue = []

	def enqueue(self, X):
		self.queue.append(X)

	def dequeue(self):
		if not self.queue:
			return -1
		return self.queue.pop(0)


class Queue:

	def __init__(self):
		self.s1 = []
		self.s2 = []

	def enqueue(self, X):
		self.s1.append(X)

	def dequeue(self):
		if len(self.s1) == 0 and len(self.s2) == 0:
			print('Q is Empty')
			return
		elif len(self.s2) == 0 and len(self.s1) > 0:
			while len(self.s1):
				temp = self.s1.pop()
				self.s2.append(temp)
			return self.s2.pop()
		else:
			return self.s2.pop()


class Stack:

	def __init__(self):
		self.arr = []
		self.top = -1

	def push(self, data):
		self.arr.append(data)
		self.top += 1

	def pop(self):
		if self.isEmpty():
			return None
		else:
			answer = self.arr[self.top]
			del self.arr[self.top]
			self.top = self.top - 1
			return answer

	def isEmpty(self):
		if self.top == -1:
			return True

	def peek(self):
		if self.top == -1:
			return None
		else:
			return self.arr[self.top]

class Queue:

	def __init__(self):
		self.stack1 = Stack()
		self.stack2 = Stack()

	def enqueue(self, X):
		self.stack1.push(X)

	def dequeue(self):
		while not self.stack1.isEmpty():
			self.stack2.push(self.stack1.pop())
		answer = self.stack2.pop()
		while not self.stack2.isEmpty():
			self.stack1.push(self.stack2.pop())
		return answer


class Queue:

	def __init__(self):
		self.s1 = []
		self.s2 = []

	def enqueue(self, x):
		self.s1.append(x)
		while self.s2:
			self.s1.append(self.s2[0])
			self.s2.pop(0)
		(self.s1, self.s2) = (self.s2, self.s1)

	def dequeue(self):
		if self.s2:
			return self.s2.pop()
		else:
			return -1


class Queue:

	def __init__(self):
		self.s1 = []
		self.s2 = []

	def enqueue(self, X):
		self.s1.append(X)

	def dequeue(self):
		while self.s1:
			new = self.s1.pop()
			self.s2.append(new)
		new1 = self.s2.pop()
		while self.s2:
			self.s1.append(self.s2.pop())
		return new1


class Queue:

	def __init__(self):
		self.s1 = []
		self.s2 = []

	def enqueue(self, X):
		self.s1.append(X)

	def dequeue(self):
		while len(self.s1) != 0:
			top = self.s1.pop()
			self.s2.append(top)
		ans = self.s2.pop()
		while len(self.s2) != 0:
			top1 = self.s2.pop()
			self.s1.append(top1)
		return ans


class Queue:

	def __init__(self):
		self.stack1 = []
		self.stack2 = []

	def enqueue(self, X):
		self.stack1.append(X)

	def dequeue(self):
		if not self.stack2:
			while self.stack1:
				self.stack2.append(self.stack1.pop())
			return self.stack2.pop()
		else:
			return self.stack2.pop()


class Queue:

	def __init__(self):
		self.s = []

	def enqueue(self, data):
		self.s.append(data)

	def dequeue(self):
		if len(self.s) <= 0:
			return -1
		x = self.s[len(self.s) - 1]
		self.s.pop()
		if len(self.s) <= 0:
			return x
		item = self.dequeue()
		self.s.append(x)
		return item


class Queue:

	def __init__(self):
		self.q = []

	def enqueue(self, X):
		self.q.append(X)
		return self.q

	def dequeue(self):
		if len(self.q) != 0:
			return self.q.pop(0)
		return -1


class Queue:

	def __init__(self):
		self.stack1 = []
		self.stack2 = []

	def enqueue(self, X):
		while len(self.stack1) > 0:
			popped = self.stack1.pop()
			self.stack2.append(popped)
		self.stack1.append(X)
		while len(self.stack2) > 0:
			popped = self.stack2.pop()
			self.stack1.append(popped)

	def dequeue(self):
		if len(self.stack1) > 0:
			return self.stack1.pop()
		else:
			return -1


class Queue:

	def __init__(self):
		self.input = []
		self.output = []

	def enqueue(self, X):
		self.input.append(X)

	def dequeue(self):
		self.peek()
		t = self.output[-1]
		self.output.pop()
		return t

	def peek(self):
		if len(self.output) == 0:
			while len(self.input):
				self.output.append(self.input[-1])
				self.input.pop()
		return self.output[-1]


class Queue:

	def __init__(self):
		self.inputStack = []
		self.outputStack = []

	def enqueue(self, X):
		self.inputStack.append(X)

	def dequeue(self):
		if len(self.inputStack) == 0:
			return -1
		while len(self.inputStack) > 0:
			self.outputStack.append(self.inputStack.pop())
		top = self.outputStack.pop()
		while len(self.outputStack) > 0:
			self.inputStack.append(self.outputStack.pop())
		return top


class Queue:

	def __init__(self):
		self.__s1 = []

	def enqueue(self, X):
		self.__s1.append(X)

	def dequeue(self):
		x = self.__s1[0]
		del self.__s1[0]
		return x


class Queue:

	def __init__(self):
		self.l = []

	def enqueue(self, X):
		self.l.append(X)

	def dequeue(self):
		if not len(self.l):
			return -1
		return self.l.pop(0)


class Queue:

	def __init__(self):
		self.stack1 = []
		self.stack2 = []

	def enqueue(self, X):
		self.stack1.append(X)

	def dequeue(self):
		while len(self.stack1) != 1:
			self.stack2.append(self.stack1.pop())
		c = self.stack1.pop()
		while len(self.stack2):
			self.stack1.append(self.stack2.pop())
		return c


class Queue:

	def __init__(self):
		self.stack = []

	def enqueue(self, X):
		stack1 = []
		if len(self.stack) == 0:
			self.stack.append(X)
		else:
			while len(self.stack):
				stack1.append(self.stack.pop())
			self.stack.append(X)
			while len(stack1):
				self.stack.append(stack1.pop())

	def dequeue(self):
		item = self.stack.pop()
		return item


class Queue:

	def __init__(self):
		self.input = list()
		self.output = list()

	def enqueue(self, X):
		self.input.append(X)

	def dequeue(self):
		if len(self.output) != 0:
			return self.output.pop()
		while len(self.input) != 0:
			self.output.append(self.input.pop())
		return self.output.pop()


class Queue:

	def __init__(self):
		self.s1 = []
		self.s2 = []

	def enqueue(self, x):
		self.s1.append(x)

	def dequeue(self):
		while len(self.s1) > 1:
			self.s2.append(self.s1.pop())
		x = self.s1.pop()
		while len(self.s2) > 0:
			self.s1.append(self.s2.pop())
		return x


class Queue:

	def __init__(self):
		self.stk1 = []
		self.stk2 = []

	def enqueue(self, X):
		self.stk1.append(X)

	def dequeue(self):
		while len(self.stk1) != 0:
			top = self.stk1.pop()
			self.stk2.append(top)
		ans = self.stk2.pop()
		while len(self.stk2) != 0:
			top1 = self.stk2.pop()
			self.stk1.append(top1)
		return ans


class Queue:

	def __init__(self):
		self.a = []

	def enqueue(self, x):
		self.a.append(x)

	def dequeue(self):
		k = self.a[0]
		self.a.remove(k)
		return k


class Queue:

	def __init__(self):
		self.a = []
		self.b = []

	def enqueue(self, X):
		self.a.append(X)

	def dequeue(self):
		self.b = self.a[::-1]
		h = self.b.pop()
		self.a = []
		self.a = self.b[::-1]
		self.b = []
		return h


class Queue:

	def __init__(self):
		self.a = []
		self.b = []

	def enqueue(self, x):
		self.a.append(x)

	def dequeue(self):
		self.b = self.a[::-1]
		k = self.b.pop()
		self.a = self.b[::-1]
		return k


class Queue:

	def __init__(self):
		self.s1 = []
		self.s2 = []

	def enqueue(self, X):
		while len(self.s1) != 0:
			self.s2.append(self.s1[-1])
			self.s1.pop()
		self.s1.append(X)
		while len(self.s2) != 0:
			self.s1.append(self.s2[-1])
			self.s2.pop()

	def dequeue(self):
		if len(self.s1) == 0:
			print('Q is Empty')
			return None
		x = self.s1[-1]
		self.s1.pop()
		return x