AlgoViz

Verifying and extending a design

medium 40 min

Before you say done, run your own code with real values and write down what each field holds.

Video lesson · Chapter 1 of 6

Make a prediction

0:00 / 4:30

See it run

Run the code by hand: every call, every field, every rule.

Operations

Trace a usual ride

Asha takes bike 1 and returns it after 45 minutes.

Step 1 of 6

Write the start

cityBikeShareclock0ridesnonebikes found3 of 3northStationdocks2bikes1 2southStationdocks1bikes3

North has bikes 1 and 2. South has bike 3 and is full. Three bikes in all.

Step 1: Write the start

def rent(self, rider, station_id):    if rider in self._rides:        raise AlreadyRiding(rider)    bike = self._stations[station_id].take()    self._rides[rider] = Ride(bike, self._clock())    return bikedef give_back(self, rider, station_id):    ride = self._rides.get(rider)    if ride is None:        raise NotRiding(rider)    self._stations[station_id].put(ride.bike)    del self._rides[rider]    return self._fare(self._clock() - ride.started)
Good for
The first trace
Watch out
It proves little alone

Say what the line does, not what it should do.

In the real world

The same verifying and extending a design doing real jobs. Pick one and step through it.

Six calls that nobody planned. After each one, the rules are checked.

Step 1 of 6

Call 1

CallAnswerState
1rent lee southbike 3ok
2
3
4
5
6

Lee takes the only bike in south.

Step 1: Call 1

Common mistakes

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

  • It should work

    major

    That sentence is about what you meant. The trace is about what you wrote.

    Wrong
    # done, I think
    Right
    # north 1 2, south 3, rides none# rent(asha, north) -> north 2, rides asha: 1
  • Only the error is tested

    critical

    The draft raises an error too, and loses a bike.

    Wrong
    refused(NoDock, lambda: city.give_back("asha", "south"))
    Right
    before = city.snapshot()refused(NoDock, lambda: city.give_back("asha", "south"))assert city.snapshot() == before
  • The test repeats the formula

    major

    Both are wrong in the same way, and the test passes.

    Wrong
    assert fare_cents(45) == (15 // 15 + 1) * 100
    Right
    assert fare_cents(45) == 100
  • Random without a seed

    major

    It fails at night and never again.

    Wrong
    rng = random.Random()
    Right
    rng = random.Random(7)
  • Every extension is free

    minor

    Nobody believes it. Name the edit and its cost.

    Wrong
    "My design supports that already."
    Right
    "Fare and one call change. Stations stay."

Interview tips

  • Trace

    • Two or three objects
    • One row for each call
    • Say the rules after each row
  • Break it

    • Empty and full
    • Both sides of a limit
    • The same call twice
  • Name tests

    • A sentence about behaviour
    • Values from the requirement
    • Snapshot after a refusal
  • Extend

    • What stays, what is new
    • Which rule is edited
    • What it costs