educative.io

The code is confusing and counter intutive

There are many concerns for the code

  1. Why code is written in counter intuitive manner? For instance the for loop is iterated from 0 to <= length of array (it should be <= length -1).
    for (int start = 0, end = 0; end <= strLen; ++end)
  2. Hacks has been made to get it working for instance in the below condition if you change the order of conditions it would not work. This is really confusing code and not really clean
    if (end == strLen || charArray[end] == ’ ')
    If the above line is written if (charArray[end] == ’ ’ || end == strLen ) it would start throwing error. These are not neat coding practices where statement is dependent on order of conditions.

Course: Grokking Coding Interview Patterns in Java - AI-Powered Learning for Developers
Lesson: Solution: Reverse Words in a String - Grokking Coding Interview Patterns in Java


Course: Grokking Coding Interview Patterns in Java - AI-Powered Learning for Developers
Lesson: Solution: Reverse Words in a String - Grokking Coding Interview Patterns in Java