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
- Ask what the dominant operation on a phone book is.
- 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.
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
- Select every practice that the lesson recommends when modelling a problem.
- Choosing a fitting data representation is half the battle. Sort each quantity by the type that best models it.
- Order the steps of modelling a problem before writing real code.
- Storing money as a floating-point number is a well-known mistake. What actually goes wrong?