For the record these would make terrible interview questions, but they seem like really fun questions to annoy your programmer friends with.
nerd-sniping that’s the term you are looking for i believe.
I got the top one as an interview question from the manager of my now-former team.
I will admit, the unicode one got me. I was expecting to be able to reverse the glyphs, but I suppose that probably isn’t that simple.
Translate it to Arabic or Hebrew ;-)
I don’t know any Arabic, but to give a Hebrew example, reversing the letters of חיים should give you מייח . The mem glyph changes.
It was a joke answer to a trick question. I meant that if the string was in English (a left-to-right language) you could translate it into a right-to-left language to “reverse” it.
There’s a crate for extended grapheme cluster-aware string reversal: https://crates.io/crates/unicode-reverse
I have a use case for string reversal using this crate (though the use case doesn’t really need to be extended grapheme cluester-aware, since ISO-8859-8 doesn’t support vocalized Hebrew): Synthesizing visual Hebrew from a corpus of logical Hebrew text for testing visual Hebrew detection.
Yeah that completely wooshed me
Consider: how does one reverse “ijk”? Next: how does one reverse “Dijkstra”?
Fun example, but falls flat if the person answering the question doesn’t know a damn about the Dutch “ij”. I think it’s a regular diphthong, as far as diphthongs can be regular :P
Now you can take this as proving or disproving your point, but if you give me a word/name in a language I don’t speak I don’t have any qualms about reversing it and pretending to not know (or really don’t know) if there’s a diphthong in it.
for the sorting question I assumed the answer was going to be counting sort, which also runs in O(n) time and is more general in the sense that it can handle duplicates and arbitrary objects with small integer keys. It is strictly slower in this specific case though as it requires three passes and some arithmetic
You saw the part where it said they were trick questions, right? An array A of length N containing the integers 1 to N inclusive can be “sorted” like this in C:
for (i=0; i<N; i++)
A[i] = i+1;
Update: Oops, just finished reading the post and found the site also has the answers, so I assume you knew this by now and just reported your experience. Thank you for doing that :-)
For the record why did you code it like that instead of making it much more readable?
for(i=1; i<=N; i++)
A[i] = i;
It’s just more natural to read, in my opinion.
It’s been a while since I wrote in C, so I was as defensive as I could in the example. I may I have internalised over time to take more notice of the array positions written to than the values.
I agree that counting from 1 to N feels more natural, but the two versions are not equivalent. Your version will likely segmentation fault, because it will attempt to write to A[N], which is beyond the bounds of an array of length N. It also doesn’t write to A, the first position in the array.
ops, you are right.
Forgot i - 1
If the array has N elements, your code will fail to compile, fail to run, or be nondeterministic depending on the language.
Yeah, I made a mistake on A[i-1] and now I can’t fix it. Only thing I can do is to own up to my mistake 😅