AlgoViz

Design four in a row

medium 75 min

Model a game in which balls fall into tubes and four in a line win.

Video lesson · Chapter 1 of 6

Make a prediction

0:00 / 4:35

See it run

A ball falls, a walk starts where it landed, and everything else is derived.

Operations

A winning drop

The caller names a tube. The rack answers with a spot.

Step 1 of 5

Tube 4

samcallergameGamedrops12turnREDstatusPLAYINGrackRacktube 2BRtube 3RBRBRBtube 4BRBline_throughfunctiondirectionnonelength0drop(RED, 4)

Red is to drop: twelve drops, so an even number.

Step 1: Tube 4

def drop(self, ball, tube):    if self._status is not Status.PLAYING:        raise IllegalDrop("the game is over")    if ball is not self.turn:        raise IllegalDrop("not your turn")    spot = self._rack.drop(tube, ball)    self._drops.append(tube)    self._line = line_through(self._rack, spot, self._need)    if self._line:        self._status = Status.WON    elif not self._rack.open_tubes():        self._status = Status.DRAW    return spot
Cost
At most 20 looks
Guarantee
One ball
All 69 lines
276 looks

No height in the arguments: nobody may choose it.

In the real world

The same four in a row doing real jobs. Pick one and step through it.

The rack refuses, and nothing has changed.

Step 1 of 3

Six balls

0123456
h 5B
h 4R
h 3B
h 2R
h 1B
h 0R

Tube 3 is full. Red asks for it all the same.

Step 1: Six balls

def drop(self, ball, tube):    if self._status is not Status.PLAYING:        raise IllegalDrop("the game is over")    if ball is not self.turn:        raise IllegalDrop("not your turn")    spot = self._rack.drop(tube, ball)    self._drops.append(tube)    self._line = line_through(self._rack, spot, self._need)    if self._line:        self._status = Status.WON    elif not self._rack.open_tubes():        self._status = Status.DRAW    return spot

Common mistakes

The bugs interviewers watch for, with the fix beside each.

  • A grid and heights

    major

    Two answers to how full a tube is. Undo, loading and tests must repair both.

    Wrong
    self.grid[self.heights[tube]][tube] = ballself.heights[tube] += 1
    Right
    self._tubes[tube].append(ball)
  • One way only

    critical

    A line that closes in the middle is missed.

    Wrong
    while rack.at(next_spot) is ball: ...
    Right
    for sign in (1, -1):    while rack.at(next_spot(sign)) is ball: ...
  • Through the floor

    critical

    In Python index -1 is the top ball. Three become six.

    Wrong
    return tube[h] if h < len(tube) else None
    Right
    return tube[h] if 0 <= h < len(tube) else None
  • Exactly four

    major

    A gap closed between two and three makes six, and six is not four.

    Wrong
    if len(line) == need:
    Right
    if len(line) >= need:
  • Four copies of a loop

    major

    One of them has a wrong sign, and nobody sees it.

    Wrong
    check_row(); check_col(); check_up(); check_down()
    Right
    for dt, dh in DIRECTIONS: ...
  • A winner that stays

    major

    Undo forgets the field, and a running game has a winner.

    Wrong
    self.winner = ball
    Right
    return self.turn.other if self._status is Status.WON else None

Interview tips

  • Open with

    • Sizes open?
    • At least or exactly?
    • Show the line?
  • Reject aloud

    • An empty cell is nothing.
    • A ball is a value.
    • The check has no state.
  • Prove it

    • No line before.
    • One place changed.
    • So the line contains it.
  • Never

    • Height as an argument.
    • Four loops.
    • A stored turn.