Stack.dsa
Stack.dsa
Training is great but they will give you a programming task every day in three months. Hassan must
finish it in the allotted time. His teammate Jocelyn gives him a task to complete the concept of Prefix
to Postfix Conversion for a given expression. Can you help him?
Coding:
#include <iostream>
#include <stack>
bool isOperator(char x)
switch (x) {
case '+':
case '-':
case '/':
case '*':
return true;
return false;
stack<string> s;
if (isOperator(pre_exp[i]))
{
string op1 = s.top();
s.pop();
s.pop();
s.push(temp);
else {
s.push(string(1, pre_exp[i]));
return s.top();
int main()
string pre_exp;
cin>>pre_exp;
cout<<"Postfix:"<<preToPost(pre_exp);
return 0;
2. You’re given a stack of N numbers, with the first component representing the stack’s top and the
final component being the stack’s bottom.
At least one piece from the stack must be removed. You can turn the stack into a queue at any time.
You cannot convert the queue back into a stack. Your task is to remove exactly K elements such that
the sum of the K removed elements is maximized.
Coding:
#include <bits/stdc++.h>
int main()
int n,k,i;
cin>>n>>k;
int sum = 0;
int arr[n];
stack<int>st, st2;
for(i=0;i<n;i++){
cin>>arr[i];
st.push(arr[i]);
for(i=0;i<k;i++){
st2.push(arr[i]);
sum += arr[i];
sum -= st2.top();
st2.pop();
sum += st.top();
st.pop();
cout<<maxs;
return 0;
}
The queue should contain only prime numbers and the stack should contain only composite
numbers.
Let the array A contains 5 integers: 7 , 21 , 18 , 3 , 12 then the content of queue and stack will be:
Queue : 7 , 3
Stack : 12 , 18 , 21
Now if you follow the rules of stack and queue then you see that you can generate the array using
the pop operations of stack and dequeue operation of the queue as follows :
Thus for every array A you have to print the contents of the queue in the first line and contents of
the stack in the second line.
Coding:
#include <bits/stdc++.h>
bool isPrime(int n)
if(n<=1)
return false;
for(int i=2;i<n;i++)
if(n%i==0)
return false;
return true;
int main()
stack<int> stack;
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++)
cin>>a[i];
if(isPrime(a[i]))
cout<<a[i]<<" ";
else
stack.push(a[i]);
cout<<endl;
while(!stack.empty()) {
cout<<stack.top()<<" ";
stack.pop();
return 0;
4. A and B are playing a game. In this game, both of them are initially provided with a list of n
numbers. (Both have the same list but their own copy).
Now, they both have a different strategy to play the game. A picks the element from start of his list.
B picks from the end of his list.
1. If the number picked by A is bigger than B then this step’s output is 1. B removes the number
that was picked from their list.
2. If the number picked by A is smaller than B then this step’s output is 2. A removes the
number that was picked from their list.
3. If both have the same number then this step’s output is 0. Both A and B remove the number
that was picked from their list.
This game ends when at least one of them has no more elements to be picked i.e. when the list gets
empty.
Coding:
#include <bits/stdc++.h>
int main()
int n;
cin>>n;
vector<int>v(n);
cin>>v[i];
int a=0,b=n-1;
while(a<n&&b>=0){
if(v[a]==v[b]){
b--;
a++;
cout<<"0 ";
}
else if (v[a]>v[b]){
b--;
cout<<"1 ";
else {
a++;
cout<<"2 ";
return 0;
cout<<"if(a[i]>a[j])";
5. The stock span problem is a financial problem where we have a series of n daily price quotes for a
stock and we need to calculate span of stock’s price for all n days.
The span Si of the stock’s price on a given day i is defined as the maximum number of consecutive
days just before the given day, for which the price of the stock on the current day is less than or
equal to its price on the given day.
For example, if an array of 7 days prices is given as {100, 80, 60, 70, 60, 75, 85},
Coding:
#include <bits/stdc++.h>
for(int i=0;i<n;++i){
for(int j=i-1;j>=0;--j){
} else {
break;
S[i]=count_;
for(int i=0;i<n;++i){
cout<<arr[i]<<" ";
cout<<endl;
int main()
int n;
cin>>n;
int price[n];
for(int i=0;i<n;++i){
cin>>price[i];
int S[n];
calculateSpan(price,n,S);
printArray(S, n);
return 0;
}
6. Hassan gets a job in a software company in Hyderabad. The training period for the first three
months is 20000 salary. Then incremented to 25000 salaries.
Training is great but they will give you a programming task every day in three months. Hassan must
finish it in the allotted time. His teammate Jocelyn gives him a task to complete the concept of
Postfix to Prefix Conversion for a given expression. Can you help him?
Coding:
#include <iostream>
#include <stack>
bool isOperator(char x)
switch (x) {
case '+':
case '-':
case '/':
case '*':
return true;
return false;
stack<string> s;
if (isOperator(post_exp[i]))
{
string op2 = s.top();
s.pop();
s.pop();
s.push(temp);
else {
s.push(string(1, post_exp[i]));
return s.top();
int main()
string post_exp;
cin>>post_exp;
cout<<postToPre(post_exp);
return 0;
7. Rathik organized technical round interview in Microsoft for the set of computer science
candidates.
The problem is to perform Implement a stack using single queue. You have to use queue data
structure , the task is to implement stack using only given queue data structure.
Rathik have given the deadline of only 5 minutes to complete the problem.
Can you Help the candidates to complete the problem within the specified time limit ?
Coding:
#include <bits/stdc++.h>
class Stack{
queue<int> q;
public:
void pop();
int top();
bool empty();
};
int s = q.size();
q.push(val);
q.push(q.front());
q.pop();
void Stack::pop() {
if (q.empty()){
cout<<"No elements\n";
} else {
q.pop();
}
int Stack::top()
bool Stack::empty() {
return (q.empty());
int main() {
Stack s;
int n, m;
int val;
s.push(val);
s.pop();
return 0;
8. Given a permutation of numbers from 1 to N. Among all the subarrays, find the number of unique
pairs (a,b) such that a ≠ b and a is maximum and b is the second maximum is that subarray.
Coding:
#include <stdio.h>
int main()
int num,i,count=0,a[100001],stck[100001],top=-1;
scanf("%d",&num);
for(i=0;i<num;i++){
scanf("%d",&a[i]);
top--;
count++;
if (top!=-1) {
count++;
stck[++top]=a[i];
printf("%d",count);
return 0;