Computer Science I / Simple Modelling with Code
Practice question · Multiple choice

Modelling a phone book as a dictionary rather than a list of pairs changes almost nothing about what the program can do, and a great deal about what it costs. Why does the representation matter so much when the information is identical?

Hints
  1. Ask what the dominant operation on a phone book is.
  2. Both structures hold the same pairs. What differs when you look someone up?
Show the answer

C. Because the representation decides which operations are cheap.

Why

The two hold identical information and differ in what the structure makes easy, and since lookup by name is essentially all a phone book does, the choice decides whether the program is fast at the one thing it does. Note dictionaries preserve insertion order and are not sorted, so a sorted listing needs a sort either way. Get the representation wrong and no later optimisation recovers it.

Read the lesson: Simple Modelling with Code →

Practise Simple Modelling with Code

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 Simple Modelling with Code