Ad
  • Custom User Avatar

    I made the first change (size in the ArrayList constructor). I'm not too worried about the second change, since this code isn't seen by the user. It's just to verify the tests are correct.

  • Custom User Avatar

    minor feedback - instead of while(true) you could do this

    while(!node.equals(node.next)) {
      ...
    }
    return next;
    
  • Custom User Avatar

    2 suggestions

    • Specify the size of the ArrayList in its constructor
    • Inspired by some of the JS solutions, there's this idea using StringBuilder
       public String[] rotate(final String text) {
          final int len = text.length();
          final List<String> rotations = new ArrayList<>(len);
          final StringBuilder lastRotation = new StringBuilder(text);
          for (int i = 0; i < len; i++) {
             lastRotation.append(lastRotation.charAt(0)).deleteCharAt(0);
             rotations.add(lastRotation.toString());
          }
          return rotations.toArray(new String[len]);
       }
    
  • Custom User Avatar

    I'll bite - does it really matter that much in this case? lastRotation and i are both hoisted to function scope so I guess you could access it ahead of time - is that the concern?