For just five interview questions, it surely ended up as a long read. Nevertheless worth the read:

The Five Essential Phone-Screen Questions — Steve Yegge

I just might translate some of his examples to Python:

Example 1: Write a function to reverse a string

```
def reverse(text):
....result = list(text)
....return ''.join(result[::-1])
```

or, just

```
def reverse(text):
....return text[::-1]
```

Example 2: Write function to compute Nth fibonacci number

```
def fib(n):
...."""Calculate fibonacci value for given n
....Doctests:
....>>> [fib(i) for i in xrange(0, 10)
....[0, 1, 1, 2, 3, 5, 8, 12, 21, 34, 55]
...."""
....if n
```

```
```Example 3: Print out the grade-school multiplication table up to 12x12

```
def multiplication_table(n):
...."""Generate an n*n multiplication table
....>>> len(multiplication_table(10))
....10
....>>> len(multiplication_table(5)[0])
....5
...."""
....# Create matrix
....table = [[x*y for x in xrange(1, n+1)] for y in xrange(1, n+1)]
....return table
def print_matrix(matrix):
...."""Expects a list of lists """
....for row in matrix:
....|...print "%4d" * len(row) % tuple(row)
if __name__ == "__main__":
...."""Run actual program code """
....print_matrix(multiplication_table(12))
```

Example 4: Write a function that sums up integers from a text file, one int per line

```
def sum_file(filename):
...."""TODO: catch exceptions """
....fp = file(filename)
....try:
....|...numbers = [int(line.strip()) for line in fp]
....finally:
....|...fp.close()
....return sum(numbers)
```

Example 5: Write function to print the odd numbers from 1 to 99

```
def print_odds():
....for i in xrange(0, 100, 2):
....|...print i
```

Example 6: Find the largest int value in an int array

```
def largest(sequence):
...."""Find the largest occurrence in a sequence ('array')
....>>> largest([0])
....0
....>>> largest(range(100))
....99
....>>> largest([1, 0, -1])
....1
...."""
....return max(sequence)
```

Example 7: Format an RGB value (three 1-byte numbers) as a 6-digit hexadecimal string

```
def format_rgb(red, green, blue):
...."""Convert integer RGB to hex RGB string
....
....>>> format_rgb(0, 0, 0)
....'000000'
....>>> format_rgb(255, 0, 0)
....'FF0000'
....>>> format_rgb(0, 255, 0)
....'00FF00'
....>>> format_rgb(0, 0, 255)
....'0000FF'
....>>> format_rgb(255, 255, 255)
....'FFFFFF'
...."""
....return "%02X%02X%02X" % (red, green, blue)
```

Test solutions:

```
def _test():
...."""Run tests on functions in module """
....import doctest
....doctest.testmod()
```

Please ignore points and pipes. WordPress can't handle code layout well. Python is white-space sensitive. This is the best I could come up with.

## Recent Comments