0% found this document useful (0 votes)
10 views

Stack.dsa

The document contains multiple programming tasks involving data structures such as stacks and queues, with specific coding challenges for each task. Key topics include converting expressions between prefix and postfix notations, maximizing sums from a stack, categorizing numbers into prime and composite, and implementing a stack using a queue. Each task is accompanied by C++ code snippets demonstrating the required functionality.

Uploaded by

jayvishal0000
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Stack.dsa

The document contains multiple programming tasks involving data structures such as stacks and queues, with specific coding challenges for each task. Key topics include converting expressions between prefix and postfix notations, maximizing sums from a stack, categorizing numbers into prime and composite, and implementing a stack using a queue. Each task is accompanied by C++ code snippets demonstrating the required functionality.

Uploaded by

jayvishal0000
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

1.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 Prefix
to Postfix Conversion for a given expression. Can you help him?

Coding:

#include <iostream>

#include <stack>

using namespace std;

bool isOperator(char x)

switch (x) {

case '+':

case '-':

case '/':

case '*':

return true;

return false;

string preToPost(string pre_exp)

stack<string> s;

int length = pre_exp.size();

for (int i = length - 1; i >= 0; i--)

if (isOperator(pre_exp[i]))

{
string op1 = s.top();

s.pop();

string op2 = s.top();

s.pop();

string temp = op1 + op2 + pre_exp[i];

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.

The front of the line is represented by the bottom of the stack.

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>

using namespace std;

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];

int maxs = sum;

while(k-- > 1){

sum -= st2.top();

st2.pop();

sum += st.top();

st.pop();

if(sum > maxs) maxs = sum;

cout<<maxs;
return 0;
}

3. You are given an array A of n integers.

You have to make a queue and stack the given integers.

The queue should contain only prime numbers and the stack should contain only composite
numbers.

All numbers in the array will be > 1.


The rule to form the stack and queue is that you should be able to generate the array using the pop
and dequeue operations.
Note : Please read this explanation carefully

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 :

dequeue from the queue: 7


pop from stack: 7 , 21
pop from stack: 7 , 21 , 18
dequeue from queue : 7 , 21 , 18 , 3
pop from stack : 7 , 21 , 18 , 3 , 12

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>

using namespace std;

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;

cout<<"int read_int() void push(int stack[],int data) top++;";

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.

You need to generate the result in form of an output list.

Method to be followed at each step to build the output list is:

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.

Output the built output list.

Coding:

#include <bits/stdc++.h>

using namespace std;

int main()

int n;

cin>>n;

vector<int>v(n);

for(int i = 0;i < n; i++)

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},

then the span values for corresponding 7 days are {1, 1, 1, 2, 1, 4, 6}

Coding:

#include <bits/stdc++.h>

using namespace std;

void calculateSpan(int price[],int n,int S[])

for(int i=0;i<n;++i){

int count_ =1;

for(int j=i-1;j>=0;--j){

if(price[j] <= price[i]){


++count_;

} else {

break;

S[i]=count_;

void printArray(int arr[],int n){

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>

using namespace std;

bool isOperator(char x)

switch (x) {

case '+':

case '-':

case '/':

case '*':

return true;

return false;

string postToPre(string post_exp)

stack<string> s;

int length = post_exp.size();

for (int i = 0; i < length; i++)

if (isOperator(post_exp[i]))

{
string op2 = s.top();

s.pop();

string op1 = s.top();

s.pop();

string temp = post_exp[i] + op1 + op2;

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>

using namespace std;

class Stack{

queue<int> q;

public:

void push(int val);

void pop();

int top();

bool empty();

};

void Stack::push(int val)

int s = q.size();

q.push(val);

for (int i = 0; i < s; i++){

q.push(q.front());

q.pop();

void Stack::pop() {

if (q.empty()){

cout<<"No elements\n";

} else {

q.pop();

}
int Stack::top()

return (q.empty()) ? -1 : q.front();

bool Stack::empty() {

return (q.empty());

int main() {

Stack s;

int n, m;

cin >> n >> m;

for (int i = 0; i < n; i++){

int val;

cin >> val;

s.push(val);

cout << "top of element " << s.top() << endl;

for (int i=0;i<m;i++) {

s.pop();

cout << "top of element " << s.top() << endl;

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]);

while (top!=-1 && stck[top]<a[i]) {

top--;

count++;

if (top!=-1) {

count++;

stck[++top]=a[i];

printf("%d",count);

return 0;

You might also like