Multiplication tables
Steve Yegge’s “five essential phone-screen questions” contains lots of good advice about phone screens. In particular, I love how even the simplest questions have basically unlimited possibilities for exploration.
For example, taking his multiplication table question, I could ask a candidate to take in the size of the table as an argument. Once that’s done, they could calculate the maximum size of a table entry, for formatting. Then they could present the user with a random quiz question before showing the table.
Just for fun, here’s my sample answer to this extended question.
import math
import random
import readline
import sys
LEFT_SPACES = 2
def askQuestion(n):
a, b = random.randint(1, n), random.randint(1, n)
answer = input("{} * {} = ".format(a, b))
if a * b == int(answer):
print("Correct!\n")
else:
print("Not quite. Check the table!\n")
def printTable(n):
pad = LEFT_SPACES + 1 + math.floor(math.log(n*n, 10))
fmtstr = '{:>' + str(int(pad)) + '}'
for i in range(1,n+1):
for j in range(1,n+1):
print(fmtstr.format(i*j), end='')
print()
n = 12
if len(sys.argv) >= 2:
n = int(sys.argv[1])
askQuestion(n)
printTable(n)
And a sample run:
$ ./multTable.py 15
10 * 15 = 150
Correct!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30
3 6 9 12 15 18 21 24 27 30 33 36 39 42 45
4 8 12 16 20 24 28 32 36 40 44 48 52 56 60
5 10 15 20 25 30 35 40 45 50 55 60 65 70 75
6 12 18 24 30 36 42 48 54 60 66 72 78 84 90
7 14 21 28 35 42 49 56 63 70 77 84 91 98 105
8 16 24 32 40 48 56 64 72 80 88 96 104 112 120
9 18 27 36 45 54 63 72 81 90 99 108 117 126 135
10 20 30 40 50 60 70 80 90 100 110 120 130 140 150
11 22 33 44 55 66 77 88 99 110 121 132 143 154 165
12 24 36 48 60 72 84 96 108 120 132 144 156 168 180
13 26 39 52 65 78 91 104 117 130 143 156 169 182 195
14 28 42 56 70 84 98 112 126 140 154 168 182 196 210
15 30 45 60 75 90 105 120 135 150 165 180 195 210 225