Geek and Books | Practice | GeeksforGeeks

Geek has several competitive programming books and he wants to place books one on another (like a stack). There are total Q queries of two types.

Type 1: This query is represented by "place x", where x is an integer represents a number of the book geek wants to place on the top of the stack.
Type 2: This query is represented by "remove". After this query, print a book number which is at the top of the stack (If the stack is empty then print -1) and remove it from the stack.

Note: There can be multiple books with the same number.   
Input: 
1. The first line of the input contains a single integer  T denoting the number of test cases. The description of T test cases follows.
2. The first line of each test case contains a single integer Q.
3. The next Q lines contain queries

Output: For each test case, for each query of type 2, print the top book number of the stack (space separated)

Constraints:
1. 1 <= T <= 100
2. 1 <= Q <= 104
3. 1 <= x <= 109

Example:
Input:
2
5
place 6
place 2
remove
place 3
remove
3
remove
place 5
remove
Output:
2 3
-1 5

Explanation:
Test Case 1: 
First Consider an empty satck: []
1st operation: Geek place a book number 6. So now Stack: [6]
2nd operation: Geek place a book number 2. So now Stack [6,2]
3rd operation: Geek wants to remove book which is top of the stack.
i.e. Book no 2. So now Stack: [6]
4th operation: Geek place a book number 3. So now Stack [6,3]
5th operation: Geek wants to remove book which is top of the stack.
i.e. Book no 3. So now Stack: [6]
Hence the output will be 2, 3
0
mohankrishnapeddineni5 months ago
JAVA SOLUTION
 
import java.util.*;import java.lang.*;import java.io.*;
class GFG {public static void main (String[] args) {Scanner scan = new Scanner (System.in);int t= scan.nextInt();while(t-->0){    int q = scan.nextInt();    Stack <Integer> stack = new Stack<Integer>();    while(q-->0){        String str = scan.next();        if(str.equals("place")){            int a= scan.nextInt();            stack.push(a);        }        else if(stack.size()>0){            System.out.print(stack.pop()+" ");        }        else{            System.out.print("-1"+" ");        }    }    System.out.println();}scan.close();}}
0
imranwahid7 months ago
Easy C++ solution
0
Panyam Chaitanya Reddy10 months ago
Panyam Chaitanya Reddy
for _ in range(int(input())):    s=[]    n=int(input())    for i in range(n):        x=input()        p=x.split(" ")        if p[0]=='place':            s.append(p[1])        else:            if len(s)==0:                print("-1",end=" ")            else:                print(s.pop(),end=" ")    print()
0
MD MAHDOOD AHMAD1 year ago
MD MAHDOOD AHMAD
0
MD MAHDOOD AHMAD
This comment was deleted.
0
MD MAHDOOD AHMAD
This comment was deleted.
0
MD MAHDOOD AHMAD
This comment was deleted.
0
MD MAHDOOD AHMAD
This comment was deleted.
0
MD MAHDOOD AHMAD
This comment was deleted.
0
MD MAHDOOD AHMAD
This comment was deleted.
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.