Reverse a String - Javascript DSA Interview Question

Поділитися
Вставка
  • Опубліковано 21 сер 2024

КОМЕНТАРІ • 66

  • @omkarjagtap2212
    @omkarjagtap2212 2 місяці тому +14

    return str.split("").reverse().join("")

  • @brokeloser
    @brokeloser 7 місяців тому +8

    It may be more optimized but higher JS payload size compared to the array approach.

  • @gauravmasalkar3546
    @gauravmasalkar3546 7 місяців тому +14

    We can convert string to array then reverse it and then again convert it into string like following str.split("").reverse().join("")

    • @RoadsideCoder
      @RoadsideCoder  7 місяців тому +2

      but it wont be optimised

    • @aneesh1701
      @aneesh1701 7 місяців тому +5

      ​@@RoadsideCoderyou're writing in javascript, if you want things to be optimised code in a real language

    • @ompandey4595
      @ompandey4595 7 місяців тому

      ​@@RoadsideCoder idiot take a loop and reverse for i = string.length-1 i>=0 i-- ;

    • @SweepAndZone
      @SweepAndZone 7 місяців тому +2

      You made it 100x harder than it needed to be lmao ​@@RoadsideCoder

    • @armorkinggaming1933
      @armorkinggaming1933 7 місяців тому

      ​@@aneesh1701
      Dude 😂😂😂 who told you JS is not a real language.
      Real coders just code the ideas, they don't make excuses.

  • @xtraszone
    @xtraszone 7 місяців тому +7

    Recursive functions is never considered as optimised approach

    • @paritoshchauhan3266
      @paritoshchauhan3266 7 місяців тому +1

      Why is that?? Recursion is a great way of solving problems with recurring sub-problem

  • @venkatsai4617
    @venkatsai4617 7 місяців тому +3

    function Arr(val){
    if(val===''){
    return '';
    }else{
    return Arr(val.substr(1)+val.charAt(0))
    }
    }
    console.log(Arr('hello'))
    it showing maximum call stack size exceed

    • @gourabmukherjee4848
      @gourabmukherjee4848 7 місяців тому

      cause the code is wrong in the video

    • @gourabmukherjee4848
      @gourabmukherjee4848 7 місяців тому +2

      function Arr(val) {
      if (val === '') {
      return '';
      } else {
      return Arr(val.substr(1)) + val.charAt(0);
      }
      }
      console.log(Arr('hello'));

    • @paritoshchauhan3266
      @paritoshchauhan3266 7 місяців тому +1

      ​@@gourabmukherjee4848exactly!! In the video he mistyped the return statement

    • @gourabmukherjee4848
      @gourabmukherjee4848 7 місяців тому

      @@paritoshchauhan3266 yeah 👍

  • @devascript
    @devascript Місяць тому

    Love you mere bhaii❤

  • @siyahulhaq7062
    @siyahulhaq7062 7 місяців тому +2

    Use 2 pointer and swap like a pro 😎

  • @sachintendulkar1122
    @sachintendulkar1122 7 місяців тому

    Using pointer and using while loop we can solve this right?
    By swapping, which will reduce memory space as well

    • @RoadsideCoder
      @RoadsideCoder  7 місяців тому +2

      can you enter that code here?

    • @armorkinggaming1933
      @armorkinggaming1933 7 місяців тому +2

      let newStr= ' ';
      for (let i = str.length -1; i>=0;i--){
      newStr += str[i];
      }
      return newStr;

    • @paritoshchauhan3266
      @paritoshchauhan3266 7 місяців тому

      ​@@armorkinggaming1933you increased time complexity by using a new string. TC now is O(n)

  • @vijaykumarjee
    @vijaykumarjee 7 місяців тому

    We can use reverse() method as well

    • @RoadsideCoder
      @RoadsideCoder  7 місяців тому +3

      there's no such method as reverse() for a string

    • @vijaykumarjee
      @vijaykumarjee 7 місяців тому +3

      @@RoadsideCoder I did it as shown below, but your method always rocks. Thanks!
      let str = "Hello, World!";
      let reversedString = str.split('').reverse().join('');
      console.log(reversedString);

    • @SagarKumar-xo5qj
      @SagarKumar-xo5qj 7 місяців тому +2

      @@vijaykumarjeeyeah there is no reverse method, u converted it into array and then reversed array.

    • @RoadsideCoder
      @RoadsideCoder  7 місяців тому +1

      This is correct, but using recursion will be more optimised

    • @undergraduate6050
      @undergraduate6050 7 місяців тому

      ​@@RoadsideCoder
      How?

  • @officialpage6191
    @officialpage6191 25 днів тому

    Good one but not optimised use one loop for this o(n)

  • @quickSilverXMen
    @quickSilverXMen 7 місяців тому

    It's working

  • @kartikxisk
    @kartikxisk 11 днів тому

    What if string is null. It will throw error

  • @armorkinggaming1933
    @armorkinggaming1933 7 місяців тому +10

    It's pointless to use all these methods. Readalibilty is poor.
    Just run a for loop in reverse.
    let newStr= ' ';
    for (let i = str.length -1; i>=0;i--){
    newStr += str[i];
    }
    return newStr;
    Very simple way. Why trying all this?

    • @paritoshchauhan3266
      @paritoshchauhan3266 7 місяців тому

      Your algo: TC O(n), SC O(n)
      Algo in video: TC O(n) SC O(1)
      You just increased space complexity to O(n) by using a new string. The mentioned way in video has space complexity O(1) and time complexity O(n)

    • @furyzenblade3558
      @furyzenblade3558 7 місяців тому +4

      @@paritoshchauhan3266no the recursive solution is not of space complexity O(1) its also O(n) because for a string of length n there will be n recursive calls to the function which all individually add a layer to the call stack which uses up memory

    • @paritoshchauhan3266
      @paritoshchauhan3266 7 місяців тому +2

      @@furyzenblade3558 ohh yes, got it, my bad. I did not count that in.

    • @soumyadeepdash4805
      @soumyadeepdash4805 6 місяців тому

      ​@@paritoshchauhan3266not only that when returning it creates a new string....which also takes O(n) space

    • @paritoshchauhan3266
      @paritoshchauhan3266 6 місяців тому

      @@soumyadeepdash4805 bro if question asks to return a string then that is not considered in space complexity.

  • @terminator6505
    @terminator6505 6 місяців тому

    So why does the code stope running when it reaches “Olleh” why doesn’t it just run forever I don’t understand

    • @RoadsideCoder
      @RoadsideCoder  6 місяців тому

      because we gave it the stopping case or base case

  • @PremKumar-ky5zl
    @PremKumar-ky5zl 7 місяців тому

    str.split('').reverse().join('')

  • @IllIIIIIIllll
    @IllIIIIIIllll 7 місяців тому

    Sir are you using computer glass or a power glass?

  • @k.balamugundanbala8981
    @k.balamugundanbala8981 7 місяців тому

    Brother i have typed the same code but it gave the o/p like --> Maximum call stack size exceeded....why ????

    • @RoadsideCoder
      @RoadsideCoder  7 місяців тому

      did u write the base case?

    • @k.balamugundanbala8981
      @k.balamugundanbala8981 7 місяців тому

      @RoadsideCoder i'm just a beginner in javascript so may I know what is meant by base case ????

    • @dpasupuleti4540
      @dpasupuleti4540 7 місяців тому +1

      The “str.charAt(0)” should be outside of function call
      return reverseString(str.substr(1)) + str.charAt(0);

  • @tariqahmed2526
    @tariqahmed2526 6 місяців тому

    Can't we use split and reverse

  • @sonuaryan5287
    @sonuaryan5287 3 місяці тому

    No need to right else

  • @tawan475
    @tawan475 7 місяців тому

    this is a bruh moment

  • @undergraduate6050
    @undergraduate6050 7 місяців тому

    When will recurrsion stop?

    • @RoadsideCoder
      @RoadsideCoder  7 місяців тому +1

      when it hits the base case i.e., when string becomes empty

    • @ThanHtutZaw3
      @ThanHtutZaw3 7 місяців тому

      I am here to write in comment why it is working without loop. Now I understand it is recurrsion.@@RoadsideCoder

  • @LeagueJeffreyG
    @LeagueJeffreyG 7 місяців тому

    This is overcomplicating something very simple. Even if you had 10mil characters in one string and passed it into this function it would make such a minimal difference

  • @CodeWithObservations
    @CodeWithObservations Місяць тому

    No need to write else

  • @giantbush4258
    @giantbush4258 7 місяців тому

    The else is unnecessary.

  • @keeganfargher6418
    @keeganfargher6418 7 місяців тому

    Don't ever write code like this. No company would accept this code in a pull request. It's very difficult to understand what it's doing

    • @RoadsideCoder
      @RoadsideCoder  7 місяців тому +2

      This is how senior devs write the code, more optimised way. Every company will accept it!