How Linked List will screw your Interview - Do this in O(1) space!

แชร์
ฝัง
  • เผยแพร่เมื่อ 20 พ.ย. 2018
  • ✅New TH-cam Account - Developer Bhaiya 👉🏻bit.ly/developer-bhaiya-youtube Gaurav challenges Rachit with a hard interview question on Linked Lists.
    The problem becomes extremely difficult when the interviewer asks to solve this in O(1). Watch till end to see how to tackle that part also.
    Watch the video to see how Rachit tackles this problem.
    Want more such collabs? Let us know in comments :)
    ================
    Important Playlists:
    ================
    Famous Interview Questions You Must Know ► • Bit Xor - Watch till e...
    Tips & Tricks for Success in Career ► • Mentorship - My Life L...
    How to Improve at Data Structures ► • Playlist
    OOPS Interview Questions ► • If you don't understan...
    Gaurav Challenges Rachit ► • AlgoWorkout - Coding I...
    Lectures & Tutorials
    ================
    Dynamic Programming: Zero to Hero ► • Dynamic Programming: F...
    Dynamic Programming on Trees ► • Dynamic Programming on...
    Graph Theory for Beginners ► • Graph Theory for Begin...
    My Code Library
    ==============
    Github ► github.com/rachitiitr/DataStr...
    Follow me:
    ==========
    Instagram ► / rachitjain2095
    LinkedIn ► / rachitiitr
    Facebook ► AlgorithmsWithRachitJain
    Programming Blog ► rachitiitr.blogspot.com
    Twitter ► / rachitj55307787
    Programming Profiles:
    ===================
    CodeForces ► www.codeforces.com/profile/rac...
    CodeChef ► www.codechef.com/users/rachitiitr

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

  • @RachitJain
    @RachitJain  5 ปีที่แล้ว +72

    Watch video description for links to important playlists ;)

    • @yashsonone1203
      @yashsonone1203 5 ปีที่แล้ว

      V

    • @kiraneminem2
      @kiraneminem2 4 ปีที่แล้ว

      Hello gaurav and rachit,
      I starting my life with a low salary in a startup company as a developer. Whether it is going to be good or not?

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

      Hey Rachit... How is this O(1) space . It's O(n) right?

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

      @@shubhmishra66 The solution without the map was O(1) extra space

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

      Will this solution holds true for lists having 1000 and lakhs of nodes?.

  • @vineethsai1575
    @vineethsai1575 5 ปีที่แล้ว +159

    1:56 Literally looking backwards, Saying "looking backwards"

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

    In first solution, instead of creating 2 separate maps and then using position to identify the newly created node, we can simply create single map of …..where key would be old node and value would be new node with same value.

  • @wulymammoth
    @wulymammoth 5 ปีที่แล้ว +201

    What a clever solution/trick. I’m not sure how someone would ever be able to come up with this without a hint or having seen this done before

    • @RachitJain
      @RachitJain  5 ปีที่แล้ว +134

      Disclaimer: I was asked this in one of my interviews... And I struggled for a loooooot of time...

    • @wulymammoth
      @wulymammoth 5 ปีที่แล้ว +20

      Algorithms With Rachit Jain 😮

    • @AakashBasuakkytherocker
      @AakashBasuakkytherocker 5 ปีที่แล้ว +8

      Which company was it, if you'd like to share the name... :)

    • @CodingMachine007
      @CodingMachine007 5 ปีที่แล้ว +50

      @@RachitJain I appreciate this comment..coz no one ever tell that they struggled...they just make their viewers think that they solved the problem in one go like Tony Stark in Marvels. You gained a subscriber buddy...😊😊😊

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

      @@RachitJain I was able to in O(1) space but i didnt use this approach. But glad to know the trick.

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

    How someone can get this trick on the spot without prior hint 🤯🤯

  • @mranonymous5830
    @mranonymous5830 4 ปีที่แล้ว +57

    While I was giving an interview for amazon on campus internship interviewer asked the exact question which I couldn't answer efficiently but this solution was awesome

  • @morning5tarr
    @morning5tarr 4 ปีที่แล้ว +141

    You need To be on tons of ganjaa before starting this at your own.

  • @edgarsanmartinjr.4278
    @edgarsanmartinjr.4278 3 ปีที่แล้ว +10

    Linked List questions are the kind of questions that once you realize the O(1) trick, you’ll never forget it. You can eventually come up with the solution if you never seen certain problems, just know that you have a high level of comfortability of traversing a linked list and manipulating their pointer references. Also LL problems can almost certainly be done in O(1) time

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

      O(1) time ? waah bhaiiii

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

      @@desiqna5223 he mean space.. Not time

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

    That moment this explaination hit me, my mind was blown. Great question.

  • @yashverma2110
    @yashverma2110 4 ปีที่แล้ว +7

    Easy solution but quite counter intuitive. I loved it!!

  • @patrickzhang1961
    @patrickzhang1961 4 ปีที่แล้ว +5

    Well explained man! Appreciate your teaching!

  • @RAJATTHEPAGAL
    @RAJATTHEPAGAL 5 ปีที่แล้ว +12

    That was an "ohhhh ... Snap that's how it can be done" wow great question liked it 😀

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

    Great job on explaining! And great job on clarifications ! Keep making sure the concepts you present are clear! Great job

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

    I am really excited for this one.

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

    Hey ,Can you please tell how can we create a map pointer and corresponding random pointer position.

  • @riturajjain5301
    @riturajjain5301 5 ปีที่แล้ว

    Wow!! What an approach. Simple to understand and yet effective.

  • @karthikeyayadav1132
    @karthikeyayadav1132 5 ปีที่แล้ว +31

    good question with very good explanation...

  • @simrankaur-pp5kh
    @simrankaur-pp5kh 5 ปีที่แล้ว

    literally awesome concept sir!

  • @SiddharthMaurya7
    @SiddharthMaurya7 4 ปีที่แล้ว

    This series is fun to watch!! Thnx!

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

    Rachit and Gaurav I have became a fan of both of u
    Amazing trick rachit, think out of the box solution

  • @revanthreddy3247
    @revanthreddy3247 5 ปีที่แล้ว

    Hats off to your logic... Smart logic

  • @bhavukgarg3619
    @bhavukgarg3619 5 ปีที่แล้ว

    Thank you for explanation.Finally it is crystal clear to me

  • @mridul1161
    @mridul1161 5 ปีที่แล้ว +14

    I have to say it was amazing.. Literally

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

    YT recommended me this video yesterday, Today this is the Leetcode Daily Change problem. 🤫

  • @rns10
    @rns10 4 ปีที่แล้ว +20

    When you are high af !!!
    I was high till you put that line in middle of this video. XD

  • @RS-cy5lf
    @RS-cy5lf 5 ปีที่แล้ว

    This is a standard gfg problem rachit... please share with us more of some tricky or complex problems thanks. You doing great job man

  • @0xskadoosh
    @0xskadoosh 5 ปีที่แล้ว +1

    Awesome logic loved it

  • @ashlesharock4419
    @ashlesharock4419 5 ปีที่แล้ว +6

    Nicely explained!! Plz also explain coding for this in c++

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

    Loved it gurus, thank you for this awesome one.

  • @sk-tx6jw
    @sk-tx6jw 4 ปีที่แล้ว

    Hey Rachit preetty amazing. Learnt something new. Glad to learn something new

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

    Mind blowing 🔥🔥🔥
    Miss those days🙈

  • @mongaslanguagetechnologies58
    @mongaslanguagetechnologies58 4 ปีที่แล้ว

    Excellent and simpler!

  • @pranjalidwivedi8665
    @pranjalidwivedi8665 5 ปีที่แล้ว

    good explanation! thanx

  • @trishalarora6619
    @trishalarora6619 5 ปีที่แล้ว +6

    Happy birthday to you Bhaiya 🎂🎂🍻🍻. Thanks for nice video

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

      It was yesterday but thanks

  • @abhishek-kapoor
    @abhishek-kapoor 5 ปีที่แล้ว

    simply love it mam perfect

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

    The final solution is bloody brilliant!

  • @MOhan-ur4ei
    @MOhan-ur4ei 3 ปีที่แล้ว +5

    this one's national treasure ✨

  • @piyushsingh178
    @piyushsingh178 4 ปีที่แล้ว

    enjoyed it thoroughly !!

  • @localtraveller1573
    @localtraveller1573 5 ปีที่แล้ว +10

    Awesome explaination really. You approach was crystal clear in your voice. Thanks by the way.

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

    Super video and excellent explanation, very clear and concise. Thank you so much.

  • @Yash-mk1qe
    @Yash-mk1qe 4 ปีที่แล้ว

    Very very clever. Thumbs up!!!

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

    This is Amazing 👌👌😊 great approach!! ✌️✌️

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

      But he is using extra space for storing duplicate ....?

  • @bhaskargarai8371
    @bhaskargarai8371 4 ปีที่แล้ว

    The 2nd solution was literally exploiting the links between the linked lists,while creating the new linked list while keeping in mind about the random pointers....awesome....

  • @anubhavaron718
    @anubhavaron718 5 ปีที่แล้ว +4

    Same question was asked to my friend for codenation placement interview R1 round.

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

    Good explaination.Were you able to come up with second approach on your own totally?

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

    I was thinkg like a BFS traversal. But that will use extra memory (queue) , this was an amazing trick. Thanks

  • @rahullagisetty3072
    @rahullagisetty3072 5 ปีที่แล้ว

    That was a good explanation and solution was very excellent thanks bro

  • @pixelated-loser
    @pixelated-loser 5 ปีที่แล้ว

    very simple and straight explanation 👌👌👌

  • @adarshshinde4969
    @adarshshinde4969 5 ปีที่แล้ว +16

    Wow the solution was just awesome 😍

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

      Thanks Adarsh :D

  • @mr.mystiks9968
    @mr.mystiks9968 3 ปีที่แล้ว +1

    This solution is 200 IQ. Really enjoyed the explanation.

  • @shreeram_kulkarni
    @shreeram_kulkarni 4 ปีที่แล้ว

    So cool .. that was really fun !

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

    I could get to map approach but this approach is goddamn amazing man

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

    Quite a nice explanation , thanks a lot .

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

    Deep copy LinkedList with random pointer, I got asked this question on Bloomberg’s sde interview!

  • @anirudhatalmale5575
    @anirudhatalmale5575 4 ปีที่แล้ว

    Outstanding explanation

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

    THIS IS AMAZING !!

  • @rahilraikwar7981
    @rahilraikwar7981 4 ปีที่แล้ว

    very Nice trick and explanation

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

    Hey Rachit, this is a really brilliant solution...

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

      But he is using extre space for storing duplicate ?

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

    Wooow!!! Great video !!!

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

    In the map solutions
    How about
    map
    And then it reduce the space ... I know it doesn't take it to O(1) but it saves the space used for indexing the first linked list ...

  • @murike
    @murike 5 ปีที่แล้ว

    Good job, you did well

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

    The end of this video was something like the end of a really good movie.

  • @PradeepSingh-ov3bt
    @PradeepSingh-ov3bt 5 ปีที่แล้ว +4

    can you please provide us with the code for better understanding

  • @FilmerWilliam
    @FilmerWilliam 4 ปีที่แล้ว

    Great content!

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

    That was nice didn't understand question at first😂😂 . Elegant solution 🔥🔥.

  • @antaratewary9645
    @antaratewary9645 4 ปีที่แล้ว

    the dirty trick is so simple and ...feels like eureka...thanks

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

    A linked list is stored in potentially arbitrary order because the physical order doesn't matter. However as long as it's in the same contiguous area of memory no matter the order what I'd do is just copy that contiguous area with a simple memcpy thus duplicating the full list then go through and perform very basic arithmetic on the pointers in list adding or subtracting the exact same offset to all the pointers so they all point correctly to the new list. By far the quickest and simplest approach to me and one that can be the most easily optimized. ---- Downside it will only work on mid-level languages like C or C++ and it can't use the heap but I do embedded programming anyways so I'd fit the requirements and thus my solution seems the simplest and most straightforward.

  • @rahuldeb3223
    @rahuldeb3223 5 ปีที่แล้ว

    Dude ive prolly hit the like button like 10 times! Love your work man and keep posting your videos!

    • @RachitJain
      @RachitJain  5 ปีที่แล้ว

      Thanks Rahul, means a lot

  • @kbagoli29
    @kbagoli29 5 ปีที่แล้ว

    Excellent explanation Rachit bhaiya

  • @MayankSharma-wx4mf
    @MayankSharma-wx4mf 5 ปีที่แล้ว

    Nyc question

  • @harshbhardwaj8165
    @harshbhardwaj8165 5 ปีที่แล้ว

    Well explained man

  • @AyushSharma-ww6vd
    @AyushSharma-ww6vd 4 ปีที่แล้ว

    I loved it...😍

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

    Done this problem, it took me a day to understand the solution

  • @nagrajullasgokarnkar6366
    @nagrajullasgokarnkar6366 5 ปีที่แล้ว

    Sir please make videos on how to prepare on datasructure and algorithms

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

    I was thinking of a different approach. Let me know if this will work.
    Create new set of linked list from next pointers as usual. Also, connect random pointer to previous nodes when doing this process. Then, start from the last node. Search in the entire linked list using previous pointers where the random should point and point the random there. Move 1 step back. Search the entire linked list forward (using next) and backward (using prev, i.e. random) until you find the actual random pointer and point it. Repeat this for all nodes till first node !

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

    We want more collabs!!

  • @clinton11994
    @clinton11994 4 ปีที่แล้ว

    Amazing!!!!!

  • @Manish_Kumar_Bihar
    @Manish_Kumar_Bihar 5 ปีที่แล้ว

    Nice explanation 👌👌

  • @Shivam22.1.97
    @Shivam22.1.97 5 ปีที่แล้ว +2

    In episode 6...What if the maximum element has the least cost and if we remove it then array length will change? please explain that case also

  • @holyshit922
    @holyshit922 5 ปีที่แล้ว

    Yes but position may be out of range for integers
    We have finite number of integers in our machine

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

    Wowwwwweeeeeee. This trickk is fabbbb

  • @saketsinha2975
    @saketsinha2975 5 ปีที่แล้ว

    awesome explaination

  • @rohitpawar4096
    @rohitpawar4096 5 ปีที่แล้ว

    I am just started learning data structure basics stack linked list etc can you give some suggestions to achieve the level in DS which you have

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

    Amazing!!

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

      Are you really Ashish Gupta , Senpai?

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

      @@shubhmishra66 what is senpai?

  • @tirthjayswal9895
    @tirthjayswal9895 4 ปีที่แล้ว

    That laugh......I can feel that situation...LOL

  • @jack16041
    @jack16041 5 ปีที่แล้ว

    It is similar to b+ tree .... like we have duplicate nodes at lowest level of the tree pointing to the records respectively.....

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

    You are beauty , nice explanation

  • @abhilashreddyy1
    @abhilashreddyy1 5 ปีที่แล้ว

    really good explanation

  • @gouravraghuwanshi
    @gouravraghuwanshi 4 ปีที่แล้ว

    Thanks bro

  • @saurabhkumarseth861
    @saurabhkumarseth861 4 ปีที่แล้ว

    Awesome trick

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

    Hey, I get the first approach and I could come up with this myself. But the 2nd approach is still going over my head. Can someone help?

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

    Kaise karlete ho Rachit bhaiyya 😂🎉

  • @odiadavid6957
    @odiadavid6957 4 ปีที่แล้ว

    Wow. I wish I had watched this earlier

  • @Clashtoons
    @Clashtoons 5 ปีที่แล้ว

    Please send me some resource to this.. I think I didn't get the question right. Or please tell me the topic name

  • @thetear2849
    @thetear2849 4 ปีที่แล้ว

    hello in the last part of the video didn't you change the structure of the linked list that it had two next node?

  • @m13m
    @m13m 4 ปีที่แล้ว

    Mind blown away

    • @RachitJain
      @RachitJain  4 ปีที่แล้ว

      I can relate to it.

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

    Is there a way to do this in constant space without modifying the initial list in the process?

  • @sasuke-pb9dt
    @sasuke-pb9dt 4 ปีที่แล้ว

    So basically inserting a node between original linked list nodes preserves the path if we decide to travel original using just random pointers

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

    7:37 how?

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

    I am good at python and I would like to learn DSA so which language will you suggest do you want me to stick with python or you want me to learn c++.I also learned basics of c++ so that I can some understand about of other codes in c++ and write it in python. Please suggest me whether to stick with python or learn c++