Type hints

Hints describe the shapes of values. The ones you will use most:

  • list[int], dict[str, int], set[str], tuple[int, str] for a fixed pair, tuple[int, ...] for any length.
  • int | None for "an int, or nothing". Older code writes Optional[int].
  • Callable[[int, int], bool] for a function taking two ints and returning a bool. Import it from typing or collections.abc.
  • Any when you really can't say.

Python ignores hints at run time. The program shows that: passing a string where the hint says list[int] runs fine. Tools like mypy and your editor catch it before it runs.

Why bother in an interview? A signature like def top_k(counts: dict[str, int], k: int) -> list[str] states the input and output before you write the body. It shows clear thinking, and it settles questions with the interviewer early.

Run it, then add hints to pair_up.