Queue Implementation using Stack | O(1) Push and Pop Operations

แชร์
ฝัง
  • เผยแพร่เมื่อ 20 ม.ค. 2025
  • Check our Website:
    In case you are thinking to buy courses, please check below:
    Link to get 20% additional Discount at Coding Ninjas: bit.ly/3wE5aHx
    Code "takeuforward" for 15% off at GFG: practice.geeks...
    Code "takeuforward" for 20% off on sys-design: get.interviewr...?_aff=takeuforward
    Crypto, I use the Wazirx app: wazirx.com/inv...
    Take 750 rs free Amazon Stock from me: indmoney.oneli...
    Earn 100 rs by making a Grow Account for investing: app.groww.in/v...
    Linkedin/Instagram/Telegram: linktr.ee/take...
    ---------------------------------------------------------------------------------------------------------------------------------------------------- SDE sheet: bit.ly/takeUfor...
    Watch at 1.25x for better experience ..
    ---------------------------------------------------------------------------------------------------------------------------
    ✅Visit Relevel at: relvl.co/8u8
    Use "TAKEUFORWARD" coupon to get 10% discount.
    ---------------------------------------------------------------------------------------------------------------------------
    Problem Link: leetcode.com/p...
    C++/Java Code: takeuforward.o...
    ---------------------------------------------------------------------------------------------------------------------------
    ✅Coding Ninjas Discount Link: aff.codingninj...
    ✅Use coupon-code "TAKEUFORWARD" for getting 10% for all CN courses: bit.ly/CNCourses45
    Please Please SUBSKRIIIBEEEEE the new channel: / @striver_79
    ---------------------------------------------------------------------------------------------------------------------------
    ✅Use coupon-code "TAKEUFORWARD" for getting 10% for all GFG courses: bit.ly/tuf_gfgC...
    ✅This is where I teach: ----- Use coupon-code "TAKEUFORWARD" for getting 10% on my course at GFG: bit.ly/striver_...
    ---------------------------------------------------------------------------------------------------------------------------
    SDE sheet: bit.ly/takeUfor...
    ✅Placement Series: • Let's give back to the...
    ✅Placement Series (Arrays, Sorting..): • C++ and Java |Brute-Be...
    ✅Hashing Playlist: • Two Sum Problem | Leet...
    ✅Greedy Playlist: • N meetings In One Room...
    ✅Recursion Playlist: • L8. Combination Sum | ...
    ✅Graph Playlist: • 3 MAJOR ANNOUNCEMENTS ...
    ✅Two Pointer Playlist: • 3 SUM | Brute | Better...
    ✅Binary Search Playlist: • Nth Root of a Number U...
    ✅LinkedList Playlist: • Reverse a Linked List ...
    ✅Advanced DS playlist: • Fenwick Tree Tutorial ...
    ✅Stack&Queue Playlist: • Implementation of Stac...
    ✅Greedy Algorithms: • N meetings In One Room...
    ---------------------------------------------------------------------------------------------------------------------------
    If you appreciate the channel's work, you can join the family: bit.ly/joinFam...
    ✅Thumbnail Creator: / rikonakhuli
    ✅ Striver's Linkedin Profile: / ​
    ✅ Instagram: / ​
    ✅Connect with us: www.google.com... (Use Link in Mobile only, if not works search "takeUforward" in telegram)..
    #dsa​ #striver #leetcode

ความคิดเห็น • 121

  • @takeUforward
    @takeUforward  3 ปีที่แล้ว +33

    Complete DSA playlists in description..
    Instagram: striver_79

    • @Amritanjali
      @Amritanjali 3 ปีที่แล้ว +3

      please bhaiya low level Designing ka course start kigiye you tube pr koi aaisa accha course nahi hai mai almost sare standard problem ka videos dekhi hu
      mera Uber ka coding interview round nikal gya tha uske bad LLD puch diye usme mai ye jaisa kuch youtuber design kra hai waise ki fir v Rejection aagya
      is liye Company ko LLD me kaisa Design chahiye hota use hisab se 1 course bnayayi please bhaiya please

    • @anilyadav-ky1bg
      @anilyadav-ky1bg 3 ปีที่แล้ว

      bhayia please release it next two days if it possible because I have couple of coding test in end the of this month. please bro it will great help to me... Thanks for doing this great work.

  • @adityachauhan1182
    @adityachauhan1182 3 ปีที่แล้ว +215

    Today at 6:00 am I watched this video and at 2:00 PM in my 2nd interview round the interviewer asked the same question and now I am typing this message after getting a intern at Service Now

    • @ytg6663
      @ytg6663 2 ปีที่แล้ว +2

      Which company

    • @utsavgupta746
      @utsavgupta746 2 ปีที่แล้ว +1

      Mere se bhi same hi pucha service noww mae bataya mae pehla wala solution but not optimized one and rejected.

    • @megaltugal
      @megaltugal ปีที่แล้ว

      @@ytg6663 bhosdike he has written it there

    • @ytg6663
      @ytg6663 ปีที่แล้ว

      @@megaltugal भाई तेरी मां कोठे पे धंधा करती है?? कि तेरी बहन ?? 😂😂

    • @cartoonsondemand_
      @cartoonsondemand_ ปีที่แล้ว +2

      Too lucky

  • @Lakshya_Raj07
    @Lakshya_Raj07 7 วันที่ผ่านมา +2

    I am in IIT and tomorrow morning, I will be having CSO lab and this vid helped me. I was suffering from 2 hours and this 11 min vid saved my day. Thanks a lot bhaiya❤

  • @shivangisrivastava1158
    @shivangisrivastava1158 3 ปีที่แล้ว +13

    Best simple explanation on TH-cam!!🙌

  • @pratheekhebbar2677
    @pratheekhebbar2677 2 ปีที่แล้ว +2

    bro ur explanation is too easy to understand and its helpful for those who are learning coding.

  • @anshumaan1024
    @anshumaan1024 ปีที่แล้ว +11

    For, *empty()* use, *return !(input.size() | output.size());*
    If anyone of the stacks has still some element, means queue is not empty and return false
    I hope it helps 🙂🙂

  • @stith_pragya
    @stith_pragya 9 หลายเดือนก่อน +1

    UNDERSTOOD.........Thank You So Much for this wonderful video.........🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻

  • @vkdude1209
    @vkdude1209 ปีที่แล้ว +1

    best teacher in the entire world♥♥♥♥

  • @VaasaviPasupulati
    @VaasaviPasupulati 3 ปีที่แล้ว +10

    Just discovered your channel 2 days ago...awesome explanation🙌🏼
    Following your SDE Sheet😇

    • @fuehrercheem
      @fuehrercheem 3 ปีที่แล้ว +5

      Kisine pucha?

    • @arindamdutta7369
      @arindamdutta7369 3 ปีที่แล้ว +3

      @@fuehrercheem hahahhahahhahaahaaha good point

    • @09_himanshusingh44
      @09_himanshusingh44 3 ปีที่แล้ว +3

      @@fuehrercheem hot hai to kuch bhi bolegi kya

    • @fuehrercheem
      @fuehrercheem 3 ปีที่แล้ว +2

      @@09_himanshusingh44 just discovered your reply 2 minutes ago ... awesome explanation 🙌 liking your reply

    • @rickseth9207
      @rickseth9207 11 หลายเดือนก่อน

      @@09_himanshusingh44 disgusting people

  • @advait2943
    @advait2943 3 ปีที่แล้ว +8

    guys if u don't get the intuition at first don't give up try watching the video twice or thrice u will surely get it.

  • @elanchezian4452
    @elanchezian4452 3 ปีที่แล้ว +29

    When I think of logic, I am getting only the brute force logic. But if I see the answer I am understanding the logic and if any similar kind of problems occurs, I am able to use this logic in some other problems.
    But I am never getting the optimal logic so far by not seeing the solution. I am only getting the brute force logic.
    Now I feel like memorizing the logics but problem solving means deducing the solution on the spot, right? without having seen the similar problem before.
    I dont understand what I am doing, is it problem solving or memorizing the logics?
    However in any interviews, the questions will be somehow new only. It is not guaranteed that I will get only the questions which I have practised.
    Please suggest me on this.

    • @ananddash6229
      @ananddash6229 3 ปีที่แล้ว +15

      same condition happens with beginners bro

    • @kasyapdharanikota8570
      @kasyapdharanikota8570 3 ปีที่แล้ว +6

      same is happening with me

    • @mukulverma9375
      @mukulverma9375 2 ปีที่แล้ว +5

      Same... I wonder when I'll be able to achieve this skill of "Problem Solving"

  • @Neo-mx2yf
    @Neo-mx2yf 7 หลายเดือนก่อน

    For anyone confused.. Amortised cost is basically the average time complexity in worst case.In last algorithm, u can think of it like every element will undergo 4 operations in the worst case :(1 push, 1 pop, 1 push, 1 pop)... For eg, say u do n operations: worst case will be u do n/2 pushes and n/2 pops... total time taken will be: (n/2+n/2x3)=2n.. Then if we find avg time complexity here = (2n)/2=2... hence it is O(1) amortised cost...

  • @Saurav_Kumar514
    @Saurav_Kumar514 2 ปีที่แล้ว +2

    Best explanation ever sir ! 👏👏

  • @anchitbhushan6172
    @anchitbhushan6172 9 หลายเดือนก่อน

    Depending on how frequent the push and pop calls are , we have a choice to either make push operation in constant time or make pop and peek operation in constant time. we cannot have both.

  • @paragroy5359
    @paragroy5359 3 ปีที่แล้ว +4

    Nice explanation your videos are really good...please keep on making such videos.

  • @phanisriimmalaraju4742
    @phanisriimmalaraju4742 ปีที่แล้ว

    Thankyou!!!! This video helped me in my exams

  • @kamyakamya1874
    @kamyakamya1874 2 ปีที่แล้ว +3

    Great Explanation . Thanks for sharing , but could u help in finding the ans of question-
    Why this implementation is used when we have Queue as Data Structure , what is the use case for this implementation? It would be helpful if u could help in finding it's answer.

    • @your_name96
      @your_name96 2 ปีที่แล้ว +3

      chill, it's another silly interview question, but on a serious note, at the low-level every thing is implement using stacks(from data structures to loops, loops are done using recursion which is made possible using stacks)

  • @shivamingole2472
    @shivamingole2472 2 ปีที่แล้ว +2

    in second explanation (10:22)we cant put the while loop inside the else because as at last the top gives 6 as ans but the top should give 2 as ans as it was the first element

  • @MdArif-yc3yj
    @MdArif-yc3yj 3 ปีที่แล้ว +1

    bhaiya kyaa kahu aapko mainn schh kah rha hu bhaiii aap mere teacher hote to main 10 lpa se upar crack krta maine late start kiyaa hu padhai ..bhai mere yaha 1 mahine bad cimpanies aane wali haii..main aapke questions follow kr rha hu aurt bhaiii sb smjh aa rha haii,,bas tress ke sre vedios bana do bhaiii,,jaldi se..aur poore sde sheeta ka jaldi se ..shukriyaa bhaii is madad ke liye ..love you bhaiiii

    • @SaurabhSingh-ph2ry
      @SaurabhSingh-ph2ry 3 ปีที่แล้ว

      Kya hua bhai placement ka kesa madad aa rha SDE sheet

  • @5091PandeyAditi-bc8ht
    @5091PandeyAditi-bc8ht ปีที่แล้ว

    Why r we not cecking if input stack is full in push function as we do in array implementation(even if we use dynamic vector arrays)?

  • @csea_37_shayoribhowmick53
    @csea_37_shayoribhowmick53 ปีที่แล้ว

    Understood completely thank you striver

  • @sampathkodi6052
    @sampathkodi6052 2 ปีที่แล้ว

    Why the space complexity is O(n) for the first example? We are not creating stacks of specified length right?

  • @padmaja577
    @padmaja577 8 หลายเดือนก่อน

    Shouldnt the tc of first approach be O(N)?

  • @zaidshaikh8111
    @zaidshaikh8111 3 ปีที่แล้ว +1

    how do we do bool empty() with this approach. I wrote input.empty() && output.empty(),but its wrong can someone tell me why ?

    • @mohitratanpal27
      @mohitratanpal27 2 ปีที่แล้ว

      in stack class tere is a func called isEmpty,so make an object input of stack class in queue class,and do if(input.isEmpty()).

    • @gyaneshjha19
      @gyaneshjha19 2 ปีที่แล้ว

      Yaar samajh ni aaya, code hi likh de

    • @anshumaan1024
      @anshumaan1024 ปีที่แล้ว +1

      bool empty() {
      return !(input.size() | output.size());
      }
      If anyone of the stacks has still some element, means queue is not empty and *return false*
      I hope it helps 🙂🙂

  • @sarangkumarsingh7901
    @sarangkumarsingh7901 6 หลายเดือนก่อน

    Loving it 😇

  • @thomasfrank132
    @thomasfrank132 ปีที่แล้ว

    Very clear. Thanks

  • @Unplugged_Aryan
    @Unplugged_Aryan 3 หลายเดือนก่อน

    what about edge cases f underflow and overflow??

  • @rakeshkumarsm9900
    @rakeshkumarsm9900 ปีที่แล้ว

    Hi bro here pop() will also take O(N) time complexity whenever the outputstack is empty right can anyone clarify my doubt. Thanks in advance.

  • @rishabhkumar8115
    @rishabhkumar8115 3 ปีที่แล้ว +1

    great video boss

  • @Conor-nu5pi
    @Conor-nu5pi ปีที่แล้ว

    great explanation

  • @HGAMES69
    @HGAMES69 2 ปีที่แล้ว +4

    Tomorrow I have mid exams for data structures and here I am watching this hoping to ace the exam.

  • @deweshjha8120
    @deweshjha8120 3 ปีที่แล้ว +1

    Thankyou bhaiyaaa..... Great content hamesha ki tarah

  • @Bongbeatcode
    @Bongbeatcode 7 หลายเดือนก่อน

    but bro leetcode not accepting O(n) complexity for push

  • @exoticme4760
    @exoticme4760 2 ปีที่แล้ว +1

    thanks

  • @prithvichetty15
    @prithvichetty15 3 ปีที่แล้ว +1

    Best explanation ♥️😍

  • @kartiksaini5619
    @kartiksaini5619 2 ปีที่แล้ว

    Wonderful ☺️

  • @gautamsuthar4143
    @gautamsuthar4143 3 ปีที่แล้ว +1

    1:56 can we push x to s2 instead of pushing to s1?

    • @anuragkumargupta8313
      @anuragkumargupta8313 3 ปีที่แล้ว +1

      Yes, but since again you are going to pop each element to s1, there will be same operation twice on element x which will add time complexity of 1.
      But the answer will be same in both the cases

  • @shubham6215
    @shubham6215 3 ปีที่แล้ว

    Thanks bro 👍👍

  • @ashutoshpanigrahy7326
    @ashutoshpanigrahy7326 2 ปีที่แล้ว

    great vid!

  • @lakshmiprasanna7058
    @lakshmiprasanna7058 ปีที่แล้ว

    Understood 💯💯💯

  • @aniruddhachunne3011
    @aniruddhachunne3011 2 ปีที่แล้ว

    what is the first did you say "alien" or something else

    • @Jonathan-ng4vw
      @Jonathan-ng4vw 2 ปีที่แล้ว

      watch again you will know what he say

  • @mrrealnobody4382
    @mrrealnobody4382 2 หลายเดือนก่อน

    this topic is the second hardest thing in this room 🗿

  • @paragroy5359
    @paragroy5359 3 ปีที่แล้ว

    You are doing a really great job.....

  • @ishaankulkarni49
    @ishaankulkarni49 2 ปีที่แล้ว

    what does amortized mean? like in this questions context.

    • @vanshsehgal9475
      @vanshsehgal9475 2 ปีที่แล้ว

      When the worst case Time complexity occurs very less, lets say once in a while among many cases.

  • @satyamgupta6030
    @satyamgupta6030 ปีที่แล้ว

    thanks buddy.

  • @aasthajha1051
    @aasthajha1051 3 ปีที่แล้ว

    understooooooooooooooood!!!!!!!!!!!!!!!!!!!!!

    • @madhabkafle8072
      @madhabkafle8072 3 ปีที่แล้ว

      understood to ek baat bolo ...pop operation me output me push krne k baad input.pop() q kiya he ?

  • @hustler516
    @hustler516 2 ปีที่แล้ว

    Why space complexity is O(2N)?

  • @souravjoshi2293
    @souravjoshi2293 2 ปีที่แล้ว +3

    You can keep a variable to keep track of top element and it will always be O(1) then. My code below (Accepted)
    class MyQueue {
    public:
    stack st1;
    stack st2;
    int peekEl = -1;
    /** Initialize your data structure here. */
    MyQueue() {
    }
    /** Push element x to the back of queue. */
    void push(int x) {
    if(st1.empty())
    peekEl = x;
    st1.push(x);
    }
    /** Removes the element from in front of queue and returns that element. */
    int pop() {
    //O(n) only when st2 is empty()
    if(st2.empty()) {
    while(!st1.empty()) {
    st2.push(st1.top());
    st1.pop();
    }
    }
    int x = st2.top();
    st2.pop();
    return x;
    }
    /** Get the front element. */
    int peek() {
    return st2.empty() ? peekEl : st2.top();
    }
    /** Returns whether the queue is empty. */
    bool empty() {
    return st1.empty() && st2.empty();
    }
    };

  • @rahulsharmaah
    @rahulsharmaah 3 ปีที่แล้ว +2

    its great contect but it will be great if you do a live class where student can also ask doubts

    • @fancoy3673
      @fancoy3673 2 ปีที่แล้ว

      Go to university amk

    • @rahulsharmaah
      @rahulsharmaah 2 ปีที่แล้ว

      @@fancoy3673 what is university amk?

  • @harshkaneria
    @harshkaneria 2 ปีที่แล้ว +1

    Great

  • @reassume4826
    @reassume4826 3 ปีที่แล้ว

    Awesome!

  • @anuragyadav_8572
    @anuragyadav_8572 2 ปีที่แล้ว

    No offense intended but @striver you need to use a better Mike, voice crackles in the end, anyhow thanks for content

    • @takeUforward
      @takeUforward  2 ปีที่แล้ว +1

      Yes updated in the new content

  • @rachanasingh2231
    @rachanasingh2231 2 ปีที่แล้ว

    brilliant

  • @kunal_dubey
    @kunal_dubey 3 ปีที่แล้ว

    Really Gd bro... Loved it ..Thank you so much

  • @gyaneshjha19
    @gyaneshjha19 2 ปีที่แล้ว +4

    If someone is stucking at leetcode in empty() function just try with this piece of code
    Return input.isEmpty()&&output.isEmpty()

  • @oqant0424
    @oqant0424 2 ปีที่แล้ว

    Understood!!!!!!!

  • @_-6912
    @_-6912 3 ปีที่แล้ว

    We can also use the stack using single queue algorithm too here.

  • @hitheshpk6030
    @hitheshpk6030 2 ปีที่แล้ว

    UNDERSTOOD

  • @urrahman196
    @urrahman196 3 ปีที่แล้ว

    ad begins 3:58, ends: 5:09

  • @roopeshn3301
    @roopeshn3301 2 ปีที่แล้ว

    Understood

  • @md_arfin_cse
    @md_arfin_cse 3 ปีที่แล้ว

    Best Explanation bro

  • @satyampande684
    @satyampande684 3 ปีที่แล้ว

    understood!!

    • @madhabkafle8072
      @madhabkafle8072 3 ปีที่แล้ว

      understood to ek baat bolo ...pop operation me output me push krne k baad input.pop() q kiya he ?

  • @utkarshgupta3869
    @utkarshgupta3869 3 ปีที่แล้ว +1

    Hello bhaiya,
    I have one question... please reply...
    I am a B.Sc(c.s) Student.
    Please help me to know can I get a job(above 5 lpa).
    I learnt enough C++ and completed DSA and going to start web development and also 3 star at codechef.
    I can learn anything for job after Graduation.
    Please help to me tell that can I get or I have to do MCA.

    • @kumarsamaksha7207
      @kumarsamaksha7207 3 ปีที่แล้ว

      Better jobs require MCA, you are a good coder you can get a job after BCA

  • @roughuse.jaiganesh
    @roughuse.jaiganesh 2 ปีที่แล้ว

    understood.

  • @shreyashachoudhary480
    @shreyashachoudhary480 2 ปีที่แล้ว

    As always!

  • @aps8874
    @aps8874 8 หลายเดือนก่อน

    💙

  • @_AmbujJaiswal
    @_AmbujJaiswal 11 หลายเดือนก่อน

    today's POTD for leetocde 29th jan 2024..

  • @201sairohithadavala9
    @201sairohithadavala9 6 หลายเดือนก่อน

    6-07-24

  • @HalfDeadme
    @HalfDeadme 2 ปีที่แล้ว

    🥰

  • @andiamakaza24
    @andiamakaza24 3 ปีที่แล้ว +2

    🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥

  • @vivekgautam9766
    @vivekgautam9766 3 ปีที่แล้ว

    Oh Man... you are GOD!

  • @subhamengine1143
    @subhamengine1143 3 ปีที่แล้ว

    mast

  • @kasyapdharanikota8570
    @kasyapdharanikota8570 3 ปีที่แล้ว

    understood , thank you very much!

    • @madhabkafle8072
      @madhabkafle8072 3 ปีที่แล้ว

      understood to ek baat bolo ...pop operation me output me push krne k baad input.pop() q kiya he ?

    • @kasyapdharanikota8570
      @kasyapdharanikota8570 3 ปีที่แล้ว

      @@madhabkafle8072 so that we remove element from the input stack

  • @daredevilxrage
    @daredevilxrage 3 ปีที่แล้ว +1

    Not helpful at all , please explain in detail .

  • @ujjwalkumar7297
    @ujjwalkumar7297 3 ปีที่แล้ว

    ha ji sir

  • @deveshagarwal660
    @deveshagarwal660 2 ปีที่แล้ว

    Poor explaination. You just gave the algorithm but never told how it was deduced.

  • @MADAI28
    @MADAI28 3 ปีที่แล้ว

    Finally striver on board

  • @ashokdurunde1814
    @ashokdurunde1814 2 ปีที่แล้ว

    Best explanation ♥

  • @rishabhkumar8115
    @rishabhkumar8115 3 ปีที่แล้ว +1

    great video boss

  • @hardikpatel352
    @hardikpatel352 7 หลายเดือนก่อน

    Thanks

  • @himanshidafouty347
    @himanshidafouty347 6 หลายเดือนก่อน

    Understood

  • @chiragbansod8252
    @chiragbansod8252 9 หลายเดือนก่อน

    understood