Computer Science I / Debugging Structural Invariants
Practice question · Put in order

Order the steps of debugging a data structure with an invariant checker.

Hints
  1. A property has to be stated before it can be turned into code.
  2. The fix belongs to the operation the checker caught, not to whatever read the structure later.
Show the answer
  1. Write down, in words, the property the structure must always satisfy
  2. Write a function that walks the structure and verifies that property
  3. Call the checker after every operation while reproducing the bug
  4. Note the first operation after which the checker fails
  5. Fix that operation, which is the one that corrupted the structure
Why

Stating the invariant, coding it, running it after every operation, and stopping at the first failure localises the bug to one operation. Without the checker you would find the damage far downstream, where the visible symptom has nothing to do with the code that caused it.

Read the lesson: Debugging Structural Invariants →

Practise Debugging Structural Invariants

The app has 6 more questions on this lesson, and keeps your place in the course. Computer Science I is free to start.

More questions on Debugging Structural Invariants