# SUDBOOKS - CRACK-A-CODE 2.0 Editorial

Author: Uttkarsh Bawakar
Tester: Priyanshu Damodhare Mayuresh Patle
Editorialist: Uttkarsh Bawankar

CAKEWALK

# PREREQUISITES:

Stack, Basic I/O operations

# PROBLEM:

Given a stack, you have to perform two types of queries on it. Push and Pop. Whenever the pop operation is performed you have to print the element which is being removed. If the stack is empty and no element can be removed then you have to print “kuchbhi?”

# QUICK EXPLANATION:

This is a straight forward stack implementation with no twist in it. You just have to create a stack and while taking the input check if you have to push or pop. If pop then print the last element in the stack and remove it and if the stack is already empty empty print “kuchbhi?”

# EXPLANATION:

There are many approaches to this problem in different languages. Lets discuss the simplest one in C++ using vector from STL.
First take input T and enter a loop of T iterations
For each test case:
First define a vector of int.
Then take interger input Q
For each Q take input input operation
if operation is 1 then take another input number and push_back in the vector
else if operation is -1 then first check if the stack is empty
if the stack is empty, print “kuchbhi?”
else if the stack is not empty print last element of the stack and then pop the last element.

# SOLUTIONS:

Setter's Solution
``````#include<bits/stdc++.h>
using namespace std;
int main()
{
int testCases;
int operation, number;
vector<int> stck;
cin>>testCases;
while(testCases--)
{

cin>>operation;
if(operation==-1)
{
if(stck.size() ==0)cout<<"kuchbhi?\n";
else
{
cout<<stck.back()<<"\n";
stck.pop_back();
}
}
else
{
cin>>number;
stck.push_back(number);
}
}
}
``````
Tester's Solution
``````q = int(input())
l = []
for _ in range(q):

n = list(map(int,input().split()))
if n[0]==1:
l.append(n[1])
else:
if(len(l)!=0):
print(l[-1])
del l[-1]
else:
print("kuchbhi?")
``````
1 Like

its a nice question on stack