- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
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.
Admin
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.
Admin
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.
Admin
Nothing Unicode is trivial
Admin
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.
Admin
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.
Admin
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.
Admin
Admin
including this one:
[spoiler][image] [/spoiler]
Admin
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.
Admin
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.
Admin
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".
Admin
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.
Admin
(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:
Admin
.... what purse? i wear pants with pockets big enough to store my stuff in. i don't need no stinking purse!
Admin
It's much easier in Java. 'N I winz, becauze I knowz recurzion!
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.
Admin
Not only that, but it won't work. :P
Admin
Awww, come on. I tested it all the way up to strings of length 10. :cry:
Admin
Guess you failed this test: :passport_control:
Admin
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.
Admin
The Perl way
$reversed = reverse $string;
The Perl way with Unicode$reversed = join '', reverse $string =~ /(\X)/g;
Admin
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.
Admin
There's an app for that...
Admin
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.
Admin
Absolutely not. It means:
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.
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.
Admin
Your code is complicated, slow, and incorrect, since Java uses UTF-16, like Objective-C.
Admin
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
Admin
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.
Admin
You guys are reading way too much into my WTF joke.