• (disco) in reply to Ragnax
    Ragnax:
    So? You ask them: "Is that in Unicode or basic 1 byte ASCII?

    That's what I did when I had that question (some years ago...). Since they were simply interested in whether I could do an in-place reversal (c/c++), 1-byte ascii was the answer. And I probably replied "Oh good" to that.

  • (disco) in reply to Ragnax
    Ragnax:
    On the other hand, you could politely ask about remaining requirements because the question has some ambiguity to it that could affect the way you'd choose to solve the problem.

    When I've given interviews, I've purposely left ambiguity to see what questions the person asked to clarify it. To me, asking those questions revels a hell of lot more than regurgitating some secret algorithm.

  • (disco) in reply to accalia

    That doesn't work if the string is already right to left (i.e. Arab text).

    Come think of it, I don't know if "properly" reversing Unicode text is a trivial thing.

  • (disco) in reply to anonymous234

    Nothing Unicode is trivial

  • (disco) in reply to anonymous234
    anonymous234:
    Come think of it, I don't know if "properly" reversing Unicode text is a trivial thing.

    it isn't. but my trick will probably get me past the interview question as the interviewer is unlikely to test my solution with already RTL text.

  • (disco) in reply to gnasher729
    gnasher729:
    A UTF-8 string must never contain invalid UTF-8 characters

    Isn't that kind of a useless rule? It's like saying "an XML document must not contain invalid XML syntax". Well duh. But it will still happen all the time so you better have a way to handle it.

  • (disco) in reply to anonymous234
    anonymous234:
    Isn't that kind of a useless rule?

    if you don't have that rule then there will be people who say "just parse it anyway. i don't care if it's malformed" and then you have the fuster cluck that was HTML in the late ninties and most of that aughts.

    HTML is still a fuster cluck, but most of the worst offenses have been ironed out now.

  • (disco) in reply to accalia
    accalia:
    fuster cluck
    Googling that yields some interesting (SFW) results :smile:
  • (disco) in reply to RaceProUK

    including this one:

    [spoiler][image] [/spoiler]

  • (disco)

    I actually wrote a similar WTF once while interviewing for my first job - simply because I didn't quite get what kind of answer the company was looking for. Felt stupid afterwards.

  • (disco) in reply to anonymous234
    anonymous234:
    Come think of it, I don't know if "properly" reversing Unicode text is a trivial thing.

    No, but following a link someone upthread posted, there is a JS library, esrever, that claims to handle it properly, including RTL overrides, combining characters, etc.

  • (disco) in reply to anonymous234
    anonymous234:
    Isn't that kind of a useless rule?

    Instead of "invalid" characters, it would probably have been more useful to use a word like "normalized" or something, that would have the same connotation as "proper fractions".

  • (disco) in reply to anonymous234
    anonymous234:
    That doesn't work if the string is already right to left (i.e. Arab text).

    Unicode specifies that the characters in the string (as opposed to as shown on the screen) are in logical order. Human languages tend to have a pretty clear idea about what that is, even if they vary wildly in how that direction is mapped to a rendering technology.

  • (disco)

    (Warning very sexist remark, please don't be offended) Question: Reverse a string? Answer: Do you have a mirror in your purse? Use that. :smile:

  • (disco) in reply to herby
    herby:
    Do you have a mirror in your purse?

    .... what purse? i wear pants with pockets big enough to store my stuff in. i don't need no stinking purse!

  • (disco)

    It's much easier in Java. 'N I winz, becauze I knowz recurzion!

    package stringservices;
    
    public class StringServices {
    
        private StringServices() {}
    
        public static String reverseString(String s)
        {
                if (s == null) return s;
                switch (s.length())
                {
                case 0:
                case 1: return s;
                case 2: return s.substring(1) + s.substring(0,1);
                }
                int half = (s.length() >> 1) + 1;
                return  reverseString(s.substring(half)) + 
                        reverseString(s.substring(0,half));
        }
    }
    

    Yes, I know this amazing entry contains every known WTF in the universe. It's a joke, son! Nothing is ever so bad but what it can't be worse.

  • (disco) in reply to CoyneTheDup
    CoyneTheDup:
    Yes, I know this amazing entry contains every known WTF in the universe. It's a joke, son! Nothing is ever so bad but what it can't be worse.

    Not only that, but it won't work. :P

  • (disco) in reply to abarker
    abarker:
    Not only that, but it won't work.

    Awww, come on. I tested it all the way up to strings of length 10. :cry:

  • (disco) in reply to CoyneTheDup

    Guess you failed this test: :passport_control:

    JBert:
    The nice thing is that it immediately gives you leverage for the "there's a bug in your code" gambit and see how they react when they are proven wrong. Should they make no obvious bug, this is far harder to pull with a straight face. Meanwhile Joel Spolsky tries to pull it anyway:
  • (disco) in reply to Martijn_Hoekstra

    Please, by all means, sound like a pedant. I've asked so many simple-yet-ambiguous questions in interviews without anyone asking clarifying questions that seeing someone actually think through them would be fantastic.

  • (disco) in reply to Maciejasjmj

    The Perl way $reversed = reverse $string; The Perl way with Unicode $reversed = join '', reverse $string =~ /(\X)/g;

  • (disco) in reply to operagost

    You should read more about their interview process before you diss it. According to Joel's articles on recruitment, the in-person interview is the last step of the process. At that point, it's somewhat likely you will get an offer, they fly you over and put you up in a fancy hotel for the interview, which I think is fairly decent of them.

    If you can't bear the thought of discussing about programming with your potential co-workers and boss for 6 hours, perhaps you aren't a good fit for a company looking for people who are passionate about programming.

    Anyway, the average recruitment process at Google is 37 days long. Microsoft is also a multi-day process with the interview continuing during the lunch "break"...

    A company willing to pay top dollar and who considers its programmers as a serious investment is the kind of company I would be interested in if I were on the market.

  • (disco) in reply to herby
    herby:
    Do you have a mirror in your purse?

    There's an app for that...

  • (disco)

    The code's not very legible, but I'll attribute that to the choice of Objective C as a language. Reversing a string has always come across as a trick question to me.
    When I do pointer math and swap char for char, the interviewer asks me why I didn't just use string.Reverse() or whatever your language has. When I do string.Reverse(), I get blasted for doing it the lazy way.

    On to the original question... doesn't this thing choke on non-ASCII characters? it encodes everything in UTF-8 and allocated sizeof(char), which I assume is 8 bits. so a string like AB© turns into \x41\x42\xC2\xA9, which is 4 bytes, but you're only allocating 3. So you'll end up with \xA9\xC2\x42, which isn't valid UTF-8, then you'll segfault.

    I think from now on I'm going to just use string.Reverse() and cite the fact that I don't want to deal with encoding as the reason. I'd accept that.

  • (disco) in reply to anonymous234
    anonymous234:
    Isn't that kind of a useless rule? It's like saying "an XML document must not contain invalid XML syntax". Well duh. But it will still happen all the time so you better have a way to handle it.

    Absolutely not. It means:

    1. If you are given bytes that are supposed to be a UTF-8 string, but not guaranteed (for example the contents of a file that is supposed to contain UTF-8 characters), you can check it, and if it isn't UTF-8 then either you reject it, or you apply a simple algorithm given in the Unicode standard which drops some of the bytes and turns everything into valid UTF-8.

    2. If you process UTF-8 strings, you must make sure that under all circumstances the result consists of a sequence of valid UTF-8 characters. Which means strncpy cannot be used to copy UTF-8 strings.

  • (disco) in reply to CoyneTheDup

    Your code is complicated, slow, and incorrect, since Java uses UTF-16, like Objective-C.

  • (disco) in reply to gnasher729
    gnasher729:
    Your code is complicated, slow, and incorrect, since Java uses UTF-16, like Objective-C.

    you're right there. let's be honest, for any time you are likely to need the code at all. this will be more than sufficient

    package stringservices;
    
    public class StringServices {
    
        private StringServices() {}
    
        public static String reverseString(String s)
        {
                if (s == null) return s;
                return "\u202E" + s;
        }
    }
    
  • (disco) in reply to mruhlin
    mruhlin:
    I think from now on I'm going to just use string.Reverse() and cite the fact that I don't want to deal with encoding as the reason. I'd accept that.

    I have never in my life actually had any need to reverse a string. But after reading this discussion, if the language I used had a string.Reverse () function I wouldn't trust that it meets whatever requirements I have.

  • (disco) in reply to gnasher729
    gnasher729:
    Your code is complicated, slow, and incorrect, since Java uses UTF-16, like Objective-C.

    You guys are reading way too much into my WTF joke.

Leave a comment on “Reversing the String, Belaboring the Point”

Log In or post as a guest

Replying to comment #:

« Return to Article