Courses / DSA Patterns for Working Engineers / Chapter 25
Module 8 · Hash maps, tracking, union-find and custom structures · Chapter 25 of 39
Knowing What to Track: Counts, Signatures and Frequencies
Builds on Hash Maps: Lookups, Complements and Pattern Matching. Next up: Union-Find: Merging Groups and Finding Redundant Links.
Part 1 · Introduction to tracking
Deciding what to keep
A search engine, a spam filter and a ticket counter all scan a stream of items and keep a small summary of what they have seen. Choosing that summary is most of the work.
Tracking means keeping a compact record while you scan: how many times each letter appeared, which letters have appeared an odd number of times, the total on each row of a board. The record answers the question the problem asks, so you do not look back through the input. Before writing a loop, decide three things. What is the question? What is the smallest record that can answer it? How cheaply can that record be updated for each new item?
The answers fall into a few families, and the rest of the chapter uses all of them.
| Record | Use it when | Cost per item | Space |
|---|---|---|---|
| Frequency map | any kind of key, counts needed | O(1) expected | distinct keys |
| Fixed-slot array |