Skip to content
CCC Python Course

Hash-based lookup

Stage
4
Module
M4.1
Lessons
1

In this module

  • Use a dictionary to store and retrieve data in O(1) time.
  • Solve lookup problems where you count or collect items by key.
  • Recognize when a dictionary is faster than a list or multiple conditions.
  • Handle missing keys safely with get() or membership checks.

Before this module

Lessons

  1. 1Hash-based lookup

Practice

Try these on the judges. Each link opens the problem on WMOJ or DMOJ.

  1. 2020 J2
    Epidemiology (opens on DMOJ in a new tab) DMOJ

    Count how many athletes finished in each age category.

    Why DMOJ: A direct counting problem where a dictionary groups results by key.

  2. 2022 J4
    Good Groups (opens on WMOJ in a new tab) WMOJ(same problem as 2022 S2)

    Look up which group a name belongs to, checking membership in O(1) per query.

  3. 2014 S2
    Assigning Partners (opens on DMOJ in a new tab) DMOJ

    Build a name-to-partner mapping and check it for symmetry.

    Why DMOJ: A direct dict-mapping problem, the same shape as this lesson's phone-book example.