Stacks : Interview Questions | Leetcode Problems | Lecture 55 | Java and DSA Course

preview_player
Показать описание
In this video, Raghav Sir will teach you TOP INTERVIEW questions of Stacks. This is Lecture 55 of the JAVA & DSA series and the second video of Stack. Various questions and patterns of questions are covered in this lecture. Infix, Prefix, Postfix evaluations and conversions and related questions of Stacks will be covered in the next Lecture in detail, coming VERY SOON.

Annotated Notes Link:

00:00 Introduction
02:26 Q1 : Balanced Brackets
23:57 HW 2 : Minimum No. of brackets removed to make sequence balanced
26:01 HW 3 : Valid bracket sequence
29:54 Q4 : Remove consecutive subsequences
56:52 Q5 : Next Greater Element
1:30:42 HW 6 : Stock span problem
1:47:12 Q7 : Next Greater Element - Method 2
1:55:04 Q8 : Largest area Histogram
2:39:02 Q9 : Min Stack

For any Batch Related Queries Please Connect - 7019243492

1) Data science Masters 2.0

2) Full Stack Web Development 2.0

3) Java with DSA and System Design 2.0

📌 𝐏𝐇𝐘𝐒𝐈𝐂𝐒 𝐖𝐀𝐋𝐋𝐀𝐇 𝐎𝐓𝐇𝐄𝐑 𝐘𝐎𝐔𝐓𝐔𝐁𝐄 𝐂𝐇𝐀𝐍𝐍𝐄𝐋𝐒 :

📌 𝐏𝐇𝐘𝐒𝐈𝐂𝐒 𝐖𝐀𝐋𝐋𝐀𝐇 𝐒𝐎𝐂𝐈𝐀𝐋 𝐌𝐄𝐃𝐈𝐀 𝐏𝐑𝐎𝐅𝐈𝐋𝐄𝐒 :

#Stack #InterviewQuestion #Leetcode #Java #DSACourse #CollegeWallah #PWSkills #PhysicsWallah
Рекомендации по теме
Комментарии
Автор

1:14:57 in ques 5 it shoud be while(st.size()>0 && st.peek()<arr[i]){
to avoid error

AnanyaShukla-zr
Автор

I hope one day, I can also think whole logic on my own.

ShachiSinghal-fiuh
Автор

1:27:38
public class NextGreatestElement {

private static int[] nextGreatest(int[] arr) {
int n = arr.length;
int[] res = new int[n];
Stack<Integer> st = new Stack<>();
res[n - 1] = -1;
st.push(arr[n - 1]);
for (int i = n - 2; i >= 0; i--) {
while (!st.isEmpty() && st.peek() < arr[i]) {
st.pop();
}
if (st.isEmpty()) res[i] = -1;
else res[i] = st.peek();
st.push(arr[i]);
}
return res;
}

public static void main(String[] args) {
int[] arr = {1, 5, 3, 2, 1, 6, 3, 4};
int[] res = nextGreatest(arr);

}
}

starzenno
Автор

1:47:02 gfg
class Solution {
// Function to calculate the span of stock’s price for all n days.
public static int[] calculateSpan(int price[], int n) {
// Your code here
Stack<Integer> st=new Stack<>();
int res[]=new int[price.length];
res[0]=1;
st.push(0);
for(int i=1;i<price.length;i++){
while(st.size()>0 && price[st.peek()]<=price[i]){
st.pop();
}
if(st.size()==0){
res[i]=i+1;
}else{
res[i]=i-st.peek();
}
st.push(i);
}
return res;
}
}

tricksforsolving
Автор

Sir please trees krana ab queqe ke baad, aapki wjh se DSA me mja aa rha h

jeetukumar
Автор

Stock Span Problem 1:31:00
public static int[] stockSpan(int[] arr){
int[] res= new int[arr.length];
Stack<Integer> st = new Stack<>();
res[0]=1;
st.push(0);
for(int i=1;i<arr.length;i++){
while(!st.isEmpty() && arr[st.peek()]<arr[i]){
st.pop();
}
if(st.isEmpty()) res[i]=1;
else res[i]=i-st.peek();
st.push(i);
}
return res;
}

magicbullet
Автор

MY SOLUTION OF 29:54 Q4 : Remove consecutive subsequences

private static int[] solve(int[] arr){
Stack<Integer> st = new Stack<>();
boolean need = false;
for(int i=0;i<arr.length;i++){
if(i==0){
st.push(arr[i]);
}else if(st.peek()!=arr[i] && need){
st.pop();
st.push(arr[i]);
need = false;

}else if(st.peek()!=arr[i] && !need){
st.push(arr[i]);
}else{
need = true;
}
}


// System.out.println(st);
int[] newArr = new int[st.size()];
for(int i=0;i<st.size();i++){
newArr[i] = st.get(i);
}

return newArr;

}

prithbi
Автор

HW 2 solution
static int wrongPara(String s) {
Stack<Character> stack= new Stack<>();
for(int i=0;i<s.length();i++) {
char ch=s.charAt(i);
if(ch=='(') {
stack.push(ch);
}
else {
if(!stack.isEmpty() && stack.peek()=='(') {
stack.pop();

}
else {
stack.push(ch);
}
}

}

return stack.size();
}

Raj-ejej
Автор

Q5) In while condition write like this

-> while(st.size()>0 && st.peek()<arr[i])

so that you can avoid some error like exception thread

deevyanshutiwari
Автор

min space withput extra space also comes with space complexity with O(n);

VikrantSingh-sbml
Автор

int arr[] = {1, 2, 3, 3, 3, 3, 4, 4, 4, 2, 2, 5, 5, 7, 8} is not working. it's give ans only 1, 7, 8 but according to questions ans should be 1, 2, 7, 8.. Please Discuss in next lecture..

Lecture is awesome...❤
Please reduce the time gap between uploading two consecutive lectures...

akashgautam
Автор

Are koi c++ ki series bhi complete kra do

aryansrivastava
Автор

Sir etne hards questions interview me aate hai ?? kya

SnehaTechTalks
Автор

hi sir, for you code on remove Consecutive subsequence which you have given at time stamp around 55-56 minutes in your lecture... if we put the input of array 123332445, then the solution should be 1225, but your code will give output 15.... WHY??... because after you pop 3 because of its repitition, your top of stack is 2 from before. then you are again going to the element 2 that lies after the 3's sequence. And your code compares it will top of stack via peek function and thus removes that 2 also. But according to logic it shouldn't be removed as it is not in a consecutive sequence of 2's... PLEASE THINK OVER IT AND LET ME KNOW. Anyways very nice presentation sir. Thanks.

PhysicsSwami
Автор

h3
class Solution {
public boolean isValid(String s) {
Stack<Character> st=new Stack<>();
for(int i=0;i<s.length();i++) {
char ch=s.charAt(i);
if(ch=='('|| ch=='{' || ch=='[') st.push(ch);
else if(st.size()>0) {
char dh=st.peek();
if(ch==')') {
if(dh=='(') st.pop();
else return false;
}
if(ch==']') {
if(dh=='[') st.pop();
else return false;
}
if(ch=='}') {
if(dh=='{') st.pop();
else return false;
}
}
else return false;
}
if(st.size()>0) return false;
return true;
}
}

by_me
Автор

55:10 I tried coding it first and did the same mistake😂😅

keyurchandrikapure
Автор

HW3 solution
class Solution {
public boolean isValid(String s) {
Stack<Character> st=new Stack<>();
for(int i=0;i<s.length();i++) {
char ch=s.charAt(i);
if(ch=='('|| ch=='{' || ch=='[') st.push(ch);
if(st.size() == 0) return false;
if(ch==')') {
if(st.peek()=='(') st.pop();
else return false;
}
if(ch==']') {
if(st.peek()=='[') st.pop();
else return false;
}
if(ch=='}') {
if(st.peek()=='{') st.pop();
else return false;
}
}
if(st.size()>0) return false;
return true;
}
}

by_me
Автор

Q3 sol=>public static boolean isValid(String s) {
Stack<Character> st = new Stack<>();
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
if (ch == '(' || ch == '[' || ch == '{') {
st.push(ch);
}
else {
if (st.isEmpty()) {
return false;
}
else {
char top = st.pop();
if ((ch == ')' && top != '(') || (ch == ']' && top != '[') || (ch == '}' && top != '{')) {
return false;
}
}
}
}
return st.isEmpty();
}

NehaTyagi-sz
Автор

Wrong Answer
Runtime: 0 ms
Case 1
Case 2
Input
nums =
[1, 2, 1]
Output
[2, -1, 1]
Expected
[2, -1, 2]
for nextgreaterelement2

priyanshgupta
Автор

Why always directly jumping to solution! ??? Will never tell How to think ? What should be the approach??

anassaif
welcome to shbcf.ru