A tribute to Aphyr’s Typing the technical interview and Richard Towers’s Typescripting the technical interview. You should read those first, if you have not already. The implementation was inspired by konstin’s delightful sudoku-in-python-packaging.

Criss’s smartwatch buzzed. A technical interview appointment in five minutes.

He sighed. Interviews used to excite him, but lately the candidates had been bewildering. Grabbing a can of water from the fridge, he stepped into the meeting room.

“Hi, nice to meet you!” Better to avoid rhetorical questions.

The candidate looked up and smiled back.

“Well, let’s get into it. I’d like do a little excersize with you, to understand how you solve problems.”

“Of course. Can I use any language?”

“Well, normally yes, but I saw you have Python on your resume.” With Python, Criss could be certain the candidate would not be able to do anything sophisticated with the type system.

“Yes, and adder too.”

“Ada?”

“Adder. A flavor of viper.”

“Never heard of it. But never mind, let’s use Python. Try to be ideomatic. Are you familiar with the n-queens puzzle?”

The candidate nodded and started typing.

1from nqueens import Queen

“Hold on… I want you to do the implementation yourself.”

“You said ideomatic Python.”

“Ok, but what I meant was you should do the implementation in Python, without using anyone else’s packages.”

“May I define my own packages?”

“Yes, of course.”

 1from itertools import product
 2
 3def get_exclusions(column: int, row: int, grid_size: int):
 4  return (
 5    exclude(c, r)
 6    for c, r in product(range(grid_size), repeat=2)
 7    if (
 8      # column exclusion (row exclusion is implicit)
 9      (r == row and c != column)
10      # diagonal exclusion
11      or ((d_c := abs(c - column)) == (d_r := abs(r - row)) and (d_c or d_r))
12    )
13  )

This was promising! Or, at least, it was a relief to see some functions operating on actual integers.

14def to_name(column: int):
15  return f"queen_{chr(ord('a') + column)}"
16
17def to_version(row: int):
18  return f"{row + 1}"
19
20def exclude(column: int, row: int):
21  return f"{to_name(column)} != {to_version(row)}"

What was this about versions?

Criss made a mental note to circle back, because the candidate was moving quickly.

22from pathlib import Path
23from zipfile import ZipFile
24
25PACKAGE_DIR = Path("packages")
26WHEEL_FILE_CONTENTS = """
27Wheel-Version: 1.0
28Generator: nqueens (1.0.0)
29Root-Is-Purelib: true
30Tag: py3-none-any
31""".strip()
32
33def generate_package(column: int, row: int, grid_size: int):
34  name = to_name(column)
35  version = to_version(row)
36  metadata = [
37    f"Name: {name}",
38    f"Version: {version}",
39    "Metadata-Version: 2.2",
40    *(f"Requires-Dist: {d}" for d in get_exclusions(column, row, grid_size)),
41  ]
42  filename = f"{name}-{version}-py3-none-any.whl"
43  with ZipFile(PACKAGE_DIR.joinpath(filename), "w") as writer:
44    writer.writestr(f"{name}-{version}.dist-info/METADATA", "\n".join(metadata))
45    writer.writestr(f"{name}-{version}.dist-info/WHEEL", WHEEL_FILE_CONTENTS)
46    writer.writestr(f"{name}-{version}.dist-info/RECORD", "")

“It looks like you just added a function to generate a package?” Criss could hear his optimisim deflating. Or maybe that hiss was coming from the air vent?

“Several packages, yes. We will feed every position on the board to this impure function.”

47PACKAGE_DIR.mkdir()
48GRID_SIZE = 8
49(
50  *_, # the void which can never satisfied
51) = (
52  generate_package(column, row, GRID_SIZE)
53  for column, row in product(range(GRID_SIZE), repeat=2)
54)

“Aren’t you worried about computational complexity?” Criss was desperate to bring things back to familiar territory.

“It only needs to run once, and it already did. Criss, we are ready to Ssssolve.”

“Ok. Well, let’s keep things tangible. Let’s say we have a Queen at c1, f8, and h4. On which positions are the other five queens?”

“Of course. I just need to install some of my packages.”

$ pip install --find-links packages/ -r /dev/stdin << EOL
queen-a
queen-b
queen-c==1
queen-d
queen-e
queen-f==8
queen-g
queen-h==4
EOL
Successfully installed queen-a-5 queen-b-3 queen-c-1 queen-d-7 queen-e-2 queen-f-8 queen-g-6 queen-h-4

“Crissss… Criss? See, the other Queens are at a5, b3, d7, e2, and g6.”

Criss thought he could feel his hoodie tightening around him, constricting his neck.

“Crissss… You asked for ideomatic Python… What could be more Python than a packaging problem?”