Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 5761 |

@blackshadows | 5715 |

@akhayrutdinov | 5111 |

@mb1973 | 4989 |

@Quandray | 4944 |

@saiujwal13083 | 4506 |

@sanjay05 | 3762 |

@marius_valentin_dragoi | 3516 |

@sushant_a | 3459 |

@verma_ji | 3341 |

@KshamaGupta | 3318 |

Complete Leaderboard | |

Handle | Score |

@aroranayan999 | 1115 |

@bt8816103042 | 739 |

@SherlockHolmes3 | 447 |

@codeantik | 441 |

@SHOAIBVIJAPURE | 430 |

@shalinibhataniya1097 | 408 |

@ShamaKhan1 | 392 |

@neverevergiveup | 381 |

@amrutakashikar2 | 355 |

@mahlawatep | 353 |

@murarry3625 | 352 |

Complete Leaderboard |

Implement a Queue using 2 stacks** s1** and** s2** .

A Query **Q** is of 2 Types

**(i)** 1 x (a query of this type means pushing **'x'** into the queue)

**(ii)** 2 (a query of this type means to pop element from queue and print the poped element)

**Example 1:**

Input:5 1 2 1 3 2 1 4 2Output:2 3Explanation:In the first testcase 1 2 the queue will be {2} 1 3 the queue will be {2 3} 2 poped element will be 2 the queue will be {3} 1 4 the queue will be {3 4} 2 poped element will be 3.

**Example 2:**

Input:4 1 2 2 2 1 4Output:2 -1Explanation:In the second testcase 1 2 the queue will be {2} 2 poped element will be 2 and then the queue will be empty 2 the queue is empty and hence -1 1 4 the queue will be {4}.

**Your Task:**

You are required to complete the two methods **push** which take one argument an integer **'x'** to be pushed into the queue and **pop** which returns a integer poped out from other queue(-1 if the queue is empty). The **printing** is done **automatically **by the** driver code**.

**Expected Time Complexity** : O(1) for **push() **and O(N) for **pop() **or O(N) for **push() **and O(1) for **pop() **

**Expected Auxilliary Space **: O(1).

**Constraints:**

1 <=** **Q <= 100

1 <= x <= 100

Note:The **Input/Ouput** format and **Example** given are used for system's internal purpose, and should be used by a user for **Expected Output** only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes
Queue using two Stacks

...