Stack Coding Interview Questions | Data Structures Learning Series Ep-1

preview_player
Показать описание
QUEUE

***Phase 1 : Theory and basic concepts **********

We searched for the most easy to understand yet complete material on stack and found this beautiful source .Highly recommended.

For solving the problems you should know how to use the inbuilt queue library in the language you code , highly recommended (this makes life much easier as you can directly use inbuilt functions of the language)

There are some variations of queue description of which you can find here.

**** Phase 2: Basic Problems ***********
First person commenting with the correct solution of any of the below problems on this video will get a special mention by us in our next video.

1. Implement Queue with 2 stacks

2. Implement stack using queue

3. Given a stream of integers and a window size, calculate the moving average of all integers in the sliding window.
Example:
MovingAverage m = new MovingAverage(3);

**** More interesting problems on stack one must solve ***********

**** Phase 3: Interview Questions ***********

Will shortly post a video on handpicked Interview Questions.

For doubts and suggestions fill the form and help us improve.

Also check out our LinkedIn for such amazing opportunities and updates:-

If you want to encourage us for more such content do like and subscribe to our channel.

Also, comment in with all your doubts.

Video Featured: Naman Vidyabhanu

Edited by: Shardul Nimbalkar

#datastructures #algorithms #placement #ds #dsandalgo #datastructuresandalgorithms #placementpreparation #dsforplacements #dsinterview #datastructuresinterview #datastructuresplacement #getplaced #stack #queue #stackinterview #queueinterview #stackcodingproblems #queuecodingproblems
Рекомендации по теме
Комментарии
Автор

queue using 2 stacks:

import java.util.Scanner;

/**
*
* @author HP
*/
public class Stack{
public int MaxSize;
int arr[];
public Stack(int n){
MaxSize=n;
arr=new int[MaxSize];
}
int top;
}
class StackFunctions{
Stack stack;
int size=0;
public StackFunctions(Stack stack){
this.stack=stack;
}
public void CreateStack(Stack stack){
stack.top=-1;

}
public void push(Stack stack, int data){
if (isStackFull(stack))
System.out.println("Stack is Full!!, Cannot push more elements");
else{
stack.top+=1;
stack.arr[stack.top]=data;
size+=1;
}
}
public boolean isStackFull(Stack stack){
if (stack.top==stack.MaxSize-1)
return true;
else
return false;
}
public boolean isStackEmpty(Stack stack){
if (stack.top==-1)
return true;
else
return false;
}
public int pop(Stack stack){
if (stack.top==-1){
System.out.println("Stack is empty no pop can be done!!");
return 0;
}
else
{
int ele=stack.arr[stack.top];
stack.top-=1;
size-=1;
return ele;
}
}
public void display(Stack stack){
for (int i=0;i<size;i++){
");
}
System.out.println();
}
}
class Demo1{
public static void main(String[] args) {
stack
Stack st=new Stack(5);
StackFunctions obj=new StackFunctions(st);
obj.CreateStack(st);
stack
Stack st2=new Stack(5);
StackFunctions obj2=new StackFunctions(st2);
obj2.CreateStack(st2);
System.out.println("Please enter the data of queue operations in order you want(write number which you want to enqueue and write dequeue if you want to perform dequeue) : ");
Scanner sc=new Scanner(System.in);
String input=sc.nextLine();
String[] inputarr=input.split(" ");
int arrsz=-1;
int element=0;
int counterdeque=0, counterele=0;
for (int i=0;i<inputarr.length;i++){
if
{
if (counterdeque>=counterele)
System.out.println("No elelemts to dequeue!!");
else{
for (int j=arrsz;j>=0;j--){
element=obj.pop(st);
if (j!=0)
obj2.push(st2, element);
}
System.out.println("The dequeue operation will remove element : "+element);
int v=arrsz;
arrsz=-1;
for(int k=v-1;k>=0;k--){
arrsz+=1;
element=obj2.pop(st2);
obj.push(st, element);
}

//obj.display(st);

//obj2.display(st2);
}
counterdeque+=1;
}
else{
counterele+=1;
obj.push(st, Integer.parseInt(inputarr[i]));
arrsz+=1;
}
}
}

}

OUTPUT:
Please enter the data of queue operations in order you want(write number which you want to enqueue and write dequeue if you want to perform dequeue) :
1 2 3 dequeue 4 dequeue
The dequeue operation will remove element : 1
The dequeue operation will remove element : 2

AdityaSharma-hhlz
visit shbcf.ru