Recursion Interview Question - "Kth Symbol in Grammar" (Data Structures & Algorithms)

แชร์
ฝัง
  • เผยแพร่เมื่อ 18 พ.ย. 2020
  • 0:12 - Problem description
    1:02 - Problem examples
    1:37 - Algorithm Explanation
    4:24 - Pseudocode (explains idea behind algorithm)
    5:55 - Full code solution (explains idea behind implementation)
    7:04 - Time + Space Complexity
    Here's the explanation on how to solve popular Data Structure & Algorithms question on Leetcode, "Kth Symbol in Grammar". This is a classic question that we get from technical interviews at Google, Facebook, Amazon, Microsoft, and so on. Let’s try this question to learn more about the basic concept of recursion.
    Please like and subscribe! Comment any of your questions / feedback down below. 😃
    #interview #data_structures # algorithm #algorithms #recursion #job #google #amazon #facebook #faang #data_structures #recursive #leetcode
    Facebook: / potatocoders
    Linkedin: / potato-coders
  • วิทยาศาสตร์และเทคโนโลยี

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

  • @chithyFly
    @chithyFly 4 หลายเดือนก่อน

    Great explaination with beautiful graph!!! Love it with your DSA explaination.

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

    So well explained. Thanks!

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

    this helped me understand easily with the recurrence relation, thanks. amazing how it will just work itself out eventually..

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

    Excellent Explanation, Thank you

  • @surajbaranwal56.
    @surajbaranwal56. ปีที่แล้ว

    Great explain, helpful visuals. Thnaks

  • @harshit-jain.
    @harshit-jain. 11 หลายเดือนก่อน

    Really great explanation. Couldn't understand it earlier but now got it. Thank you.

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

    great solution!

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

    Nice Explanation

  • @rustam-z
    @rustam-z 2 ปีที่แล้ว +6

    There must be k//2 in self.kthGrammar(n-1, k/2 + k%2)

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

    It seemed impossible before your video, but not now. Thank you so much.

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

    simple and clear, thank you

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

    Best explanation please please keep it up

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

    Best explanation in this question

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

    really kool I have same approach in mind but wasn't able to solve thanks for the video

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

    Please make more videos on DS and Algorithms. Great explanation and visuals. Loved it

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

      Thanks, will definitely do once we find the available time!

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

    I do not know why anyone has disliked it. It is a great solution.

  • @baro-kv8he
    @baro-kv8he 3 ปีที่แล้ว

    I like your wording, concise. I do like and subscribe.

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

    🙏🙏Very nice . love to see more 👍👍videos . 😍😍 thanks for uploading👌👌

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

    dood this is amazing please do more videos man , like dp problems

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

    Great way of visualizing the problem as a tree

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

      Thank you! 😀 We are happy that you like the video

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

    kudos!!

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

    This is O(n) time complexity the modulo is a constant operation, also since were only modding 2 here we can just check the first bit in k to see if its a 1 or 0

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

    Bit late but...i watched many videos but ur explanation is the best 🤘

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

    Wow, loved this visualization. I solved it using another method, but this method of yours is good too!!

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

      Thanks for the feedback! ☺️ There are many ways to solve this problem, but we thought this method will be instructive of the recursion algorithm

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

      @@potatocoders2028 may ik why k%2 operation takes O(log k) time complexity ?
      & what if i use (k+1)/2 instead of " k/2 + k%2 " , inorder to figure out the ceil of k/2 . so that we reduce the complexity from O(n * log K ) to O (n * 1 ) . correct me if I am wrong ?
      appreciate your help :)

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

    Why do we need to take ceil for k/2 i.e. k/2+k%2? Why just k/2 does not work, can someone explain, please? Thank you.

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

    this solution is more understandble

  • @sauravnegi8858
    @sauravnegi8858 5 หลายเดือนก่อน

    is there any other way to do it?

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

    That was so easy i spent an hour on this😭😭😭

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

    It wasasked in which company interview??

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

      Leetcode questions like these are just questions that may show up in your coding interview

  • @vietjs-music
    @vietjs-music 3 ปีที่แล้ว +1

    I thought the time complexity will be O(n)? Cause the modulo operation only takes O(1) time right.

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

      Hi Viet! Thank you for your comment. The modulo operation performs a long division, which takes a number of steps on the order of the number of digits. The number of digits of a number K scales as log(K), so the modulo operation has a time complexity of O(log K).

    • @vietjs-music
      @vietjs-music 3 ปีที่แล้ว +1

      @@potatocoders2028 Thanks for the detailed explanation

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

      No problem :)

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

      @@potatocoders2028 for odd/even you can just do k&1 and in fact it's parent xnor k&1, which is 1-(parent ^ (k&1) and that's O(1). for the recursive call you can just do (k+1) // 2 no need for % at all.

  • @SahilJSawant
    @SahilJSawant 7 หลายเดือนก่อน +1

    But why are you doing log K I think for every level you are just doing one operation so the time complexity is just O(N)

    • @ben94_
      @ben94_ 5 หลายเดือนก่อน

      this is my question too. It looks like the operations are constant for each level?

  • @roberto-rodriguez
    @roberto-rodriguez 2 ปีที่แล้ว

    Accepted solution in Java:
    class Solution {
    public int kthGrammar(int n, int k) {
    n -= 1;
    k -= 1;

    if(n == 0){
    return 0;
    }

    return process(n, k);
    }

    public int process(int n, int k) {
    if(n == 1){
    return k % 2 == 0 ? 0 : 1;
    }

    Double two_pow_n = Math.pow(2, n);
    int half = two_pow_n.intValue() / 2;

    if(k < half){
    return process(n - 1, k);
    }else{
    return invert( process(n - 1, k - half) );
    }

    }

    public int invert(int i){
    return i == 0 ? 1 : 0;
    }

    }