News for hackers. Stuff that matters.
Re:Typical programming interview questions. by barrkel on Monday February 21, 2011 @02:36AM (#2244659)
A general "solution" to these kinds of sequences, if the sequences are mathematically related, is the method of common differences (e.g. http://www.purplemath.com/modules/nextnumb.htm). It would give 31 in this case, but it works for many other straightforward sequence-type problems. The basic idea is you calculate the difference between every two adjacent numbers to create a new sequence, then do the same for that; often the pattern becomes very clear then.

Ironically, the "problem" at hand appears to have come straight from that tutorial:

http://www.purplemath.com/modules/nextnumb7.htm

> Find the next number in the following sequence: 1, 11, 21, 1211, 111221,....

> This looks like a "math" sequence, but it isn't really. Instead, each term is a description of the preceding term. The first term is just one "1": 11. The second term is two "1's": 21. And so forth:

    one 1: 11
    two 1's: 21
    one 2 and one 1: 1211
    one 1, one 2, and two 1's: 111221
    three 1's, two 2's, and one 1: 312211

    So the next term is "312211".
Re:Typical programming interview questions. by sesqu on Monday February 21, 2011 @06:34AM (#2245074)
This sequence, by the way, is called the look-and-say sequence. If you like it, you might be interested in this blog post on the length of the terms:

http://www.nathanieljohnston.com/2010/10/a-derivation-of-con...