Note: me telling you up front that these are trick questions rather gives the answers away in most cases, but oh well.
What's the fastest way to sort a jumbled array, given that the array has length N and contains all the integers from 1 to N inclusive?
What's the fastest way to test whether a 32-bit integer is a perfect number?
How do you reverse a Unicode string?
Just repopulate the array with integers from 1 to N. Running time is O(N).
return x == 6 || x == 28 || x == 496 || x == 8128 || x == 33550336;
For 64-bit integers you need only three additional terms.
You can't. The Unicode standard does not have a concept of string reversal.
There are numerous semi-obvious ways in which a programmer could rapidly/spontaneously define "string reversal", but all of them run into serious problems when presented with arbitrary Unicode strings which may contain non-ASCII characters, combining characters, ligatures, joined "words", bidirectional text in multiple languages, surrogate pairs and so on. As the problem becomes more complex, we discover that defining reversal on an arbitrary Unicode string in a meaningful, practical way is, though probably not completely impossible, extremely difficult. The algorithm we would eventually define would be complex and highly non-obvious, taking months of work to create. Additionally, it would have to be created by the same sort of highly informed language specialists who came up with Unicode's character-combining, normalization or bidirectionality algorithms — namely, the Unicode Consortium. And then, suitable metadata might need to be added to all 100,000+ Unicode characters to facilitate the algorithm.
This has not been done, because there is no genuine real-world need for such a thing. The algorithm doesn't exist. You can't reverse a Unicode string.
A corollary is that Unicode also has no concept of palindromes (strings which read the same after being "reversed"). When encountering programming questions relating to palindromes, it's important to ascertain exactly what a "string" is considered to be, and what "reversed" is considered to mean.
This discussion is closed.