Computer Science I / Integrated Programming Synthesis
Practice question · Multiple choice

A word-frequency counter needs to split text, normalise case, count occurrences and report the top results. Why is choosing a dictionary for the counting step the decision that shapes the whole program?

Hints
  1. For each word in the text, what operation does the counter perform?
  2. Ask what that operation costs in a dictionary and in a list of pairs.
Show the answer

C. Because every word needs a lookup, and a dictionary makes that O(1).

Why

The counting step performs one lookup per word, so its cost is multiplied by the length of the text. O(n) with a dictionary and O(n²) with a list of pairs, fine on a paragraph and hopeless on a book. Splitting and normalising have no such leverage. Dictionaries are not sorted either, so reporting still needs a sort, cheap, because it runs once rather than n times.

Read the lesson: Integrated Programming Synthesis →

Practise Integrated Programming Synthesis

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

More questions on Integrated Programming Synthesis