This will be the shortest answer in my life SO: a lookup table.
Apparently, I need to clarify a little: "if you have enough memory for the game," then we have all the memory we need (technical ability is unimportant). Now you do not need to store the lookup table for more than a byte or two. Although it will technically be Ω (log (n)) and not O (1), just reading the number you need is Ω (log (n)), so if this is a problem then the answer will be impossible , which is even shorter .
Which of the two answers they expect from you at the interview, no one knows.
There’s another trick: while engineers can take a number and talk about Ω (log (n)), where n is a number, computer scientists will say that we actually have to measure the runtime as a function of input length, so engineers call Ω (log (n)) actually Ω (k), where k is the number of bytes. However, as I said, just reading the number is equal to Ω (k), so we cannot do it better.
alf Jan 15 '12 at 16:20 2012-01-15 16:20
source share