Ad
  • Default User Avatar

    I kumited it to Java and included a randomized test case. Thank you!

  • Default User Avatar

    Agreeing. Both issues take O(n^2) time, and both operations (checking for multiples vs. single occurrence and appending a result string) are doable in O(n) time.

    If indexOf(x) and lastIndexOf(x) cached their values (since String is immutable), the result would be O(n), but that's not how String is coded as of Java 8.

    The Java compiler optimizes String x += y to the bytecode equivalent of

    x = new StringBuffer(x).append(y).toString();
    

    However, it does not take the optimization a step further to automatically create a single StringBuffer in a loop. That is, it does NOT perform the optimization

    String result = "";
    for (char c : charCollection) { result += c; }
    

    ...to...

    // This optimization DOES NOT HAPPEN
    StringBuffer internalStringBufferForResult = new StringBuffer("");
    for (char c : charCollection) { internalStringBufferForResult.append(c); }
    String result = internalStringBufferForResult.toString();
    

    Prior to doing some research, I thought that's what happened under the covers -- but it's clear that I was wrong.

  • Default User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Default User Avatar

    I did not solve this, and my hacky little attempts were pretty minor-league. When I gave up and revealed the solution, I didn't feel so bad that I couldn't solve it. Definitely "advanced language features" here!

  • Default User Avatar
    I think this is an interesting problem as it's so simple in theory, and the solutions can be really elegant.
    

    Agree completely!

  • Default User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Default User Avatar

    +1 on the "clever" side for using recursion instead of a stack. But isn't a stack more transparent?

  • Default User Avatar

    Hi, @biswajit86.

    For your first situation, I solved this with an algorithm that returns false.

    int[] A2 = {21, 22, 23, 24, 11, 12, 13, 14, 1, 2, 3, 4 };
    assertEquals(false, calc.isCircleSorted(A2)); // This test case passed and my solution was accepted

    Are you sure that this is really a problem?

    For your second situation (lists can contain duplicate elements), that's an important thing for programmers to learn when thinking about sorting. If they don't already know that from experience or intution, this Kata is good for reinforcing it.

  • Default User Avatar

    The requirements say that the last return value is "mocha_missing!" (with exclamation point at the end).

  • Default User Avatar

    Advice that seasoned developers have given me: Habitually write code that clearly expresses intent, rather than optimizing performance. The compiler and JVM often perform optimizations for you, and profiling tools can be used to expose bottlenecks. But more importantly, premature optimization leads to obscurification of intent, sometimes without any actual performance benefit.