For Java programmers: What's the point of this problem?


  • 6
    H

    There are basically two ways to solve this problem.

    One is to use a StringBuilder, another is to reverse the underlying char array.

    For C/C++ programmers, they may want to modify the input string if it's not constant, so to achieve O(1) space.

    However, for Java programmers, I don't see any improvement beyond O(n) time and space. This is no different from reversing an integer array...What's the point here?


  • 10
    T

    Just look at the following questions in this forum and it should shed some light on the answer:

    Failed attempts:

    Remember:

    LeetCode OJ is a platform for preparing technical coding interviews.

    There're many ways you can trip this up on an interview (or even just practising here). The simplest questions are the most lethal IMO. It's not always about going from O(n!) to O(log n) via incomprehensible cleverness (there are algorithms that you can't invent at an interview, you either know it or you don't; it doesn't test how good a developer you are). Think about how many ways there are just to do the char[] reversal. Here are some decision points one has to make during answering this question:

    • Use Java API or implement it low level?
    • Call reverse Java API?
    • += between Strings in core loop (see, not all solutions are O(n) ;)
    • StringBuilder VS StringBuffer (synchronized)
    • Construct a StringBuilder from input and do mutating operations: charAt and setCharAt
    • Use toCharArray() and read from char[], or use String.charAt()
    • Do preallocate builder capacity or let it grow?
    • Use append(char) or setCharAt?
    • How to iterate?
    • loop based on input length
      • two pointers (start | end) or loop all items
      • start from front or back
      • while or for
      • while(true) or while(start < end) or while(start <= end)
      • cache length in for loop or query each time
    • recursion
      • +=
      • Collecting argument (char[] + index, StringBuilder)
    • Create a method for iteration or leave it inline?
    • shortcut for "", "x"?
    • specialize for "xy" and "xyz"?
    • What about null input?
    • How do you format your code?

    I'm sure I've missed some and also included some that are so bad they don't even come to mind. If you look around there are some people who do it, and when they do it here, they learn instead of failing an interview spectacularly.

    @hukun01 (and other people who upvoted) really curious about your reaction after reading this.


  • 0
    T

    +1 for using the discussion forum to start a discussion instead of sharing a code snippet without description.


  • 0
    H

    First of all, thanks for your time.

    One thing I want to point out here is that all the related questions you listed are directly related to String manipulation, but not String reverse.

    The listed questions can be asked in ANY interview problem that involves string operations. Namely, without asking how to reverse a string, we can still ask the above followup in other questions.

    Now you see, this question becomes non-sense. BTW, some of the API questions are not that good to test the candidate, such as the difference between charAt() and read from char[]. This can be searched easily, and should not be memorized.


  • 0
    T

    No, I didn't mean these questions should be asked by the interviewer. I meant that the candidate should make these decisions alone and be able to reason why did they go either way.

    I agree though, it's about string manipulation, but if someone can't choose the best approach here, they probably wouldn't choose the best approach where working with strings is just a minuscule part of the problem.

    There are also ways to go nuts with this question, like "reverse 100GB String that doesn't fit in memory and you can only read sequentially forward".

    Which (type of) problem do you think is a better question for a face to face coding exercise? Btw, I still think this problem has it's place, for example for junior devs or just a quicky among many other questions.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.