Design four in a row
medium 75 minModel a game in which balls fall into tubes and four in a line win.
Video lesson · Chapter 1 of 6
Make a prediction
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
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
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | |
|---|---|---|---|---|---|---|---|
| h 5 | B | ||||||
| h 4 | R | ||||||
| h 3 | B | ||||||
| h 2 | R | ||||||
| h 1 | B | ||||||
| h 0 | R |
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 spotCommon mistakes
The bugs interviewers watch for, with the fix beside each.
A grid and heights
majorTwo answers to how full a tube is. Undo, loading and tests must repair both.
Wrongself.grid[self.heights[tube]][tube] = ballself.heights[tube] += 1Rightself._tubes[tube].append(ball)One way only
criticalA line that closes in the middle is missed.
Wrongwhile rack.at(next_spot) is ball: ...Rightfor sign in (1, -1): while rack.at(next_spot(sign)) is ball: ...Through the floor
criticalIn Python index -1 is the top ball. Three become six.
Wrongreturn tube[h] if h < len(tube) else NoneRightreturn tube[h] if 0 <= h < len(tube) else NoneExactly four
majorA gap closed between two and three makes six, and six is not four.
Wrongif len(line) == need:Rightif len(line) >= need:Four copies of a loop
majorOne of them has a wrong sign, and nobody sees it.
Wrongcheck_row(); check_col(); check_up(); check_down()Rightfor dt, dh in DIRECTIONS: ...A winner that stays
majorUndo forgets the field, and a running game has a winner.
Wrongself.winner = ballRightreturn 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.