Advanced Topics in Computer Science

Unit 5: Sets, Maps, and Hash Tables

Students learn fast lookup structures for uniqueness, counting, indexing, and key-value associations.

Unit Focus

What this unit is really teaching

The unit develops the ideas behind Java HashSet and HashMap while also showing how collisions and load factor affect performance.

Key Topics

  • +Set and map ADTs and when to use each
  • +Frequency maps, lookup tables, indexes, and duplicate detection
  • +Hash functions, hash codes, equality, and collision handling
  • +Separate chaining, open addressing, and load factor intuition
  • +HashSet, TreeSet, HashMap, and TreeMap tradeoffs
  • +Security and correctness concerns when keys are mutable or poorly hashed

Practice Work

Implementation and analysis tasks

Student task

Build a word-frequency analyzer with maps and sorting by count.

Student task

Implement a simple chained hash table and measure collision behavior.

DSA Assignment Bank

30 interview-style algorithm questions for this unit

Code Scholars provides structured assignments, implementation support, edge-case review, and runtime analysis coaching so students learn the concept rather than only memorizing a solution.

1

Hash lookup

Find two values with target sum

Use a map from value to index to find two values that add to a target in one pass.

2

Canonical keys

Group words by anagram pattern

Create a key from sorted characters or counts so words with the same letters are grouped together.

3

Frequency map

Find first unique character

Count characters, then scan in original order to find the first character that appears once.

4

Sliding window set

Check duplicate within distance

Determine whether the same value appears twice within k positions using a moving set.

5

HashSet boundaries

Find longest consecutive run

Use a set to start runs only at values that have no predecessor.

6

Prefix sum map

Count subarrays with target sum

Track previous prefix sums so each new sum can count matching earlier sums.

7

Map plus heap or buckets

Find most frequent k values

Count frequencies and choose an efficient structure to return the k most common values.

8

Map plus linked order

Build a simple LRU cache

Design get and put operations that update recent-use order and remove the least recent item.

9

Substring set

Detect repeated DNA-like codes

Find repeated fixed-length strings by storing seen codes and duplicates separately.

10

Map equality

Compare two frequency profiles

Decide whether two response lists have the same counts for every category.

11

Set and arithmetic

Find missing and repeated values

Given values from 1 to n with one missing and one repeated, identify both and explain tradeoffs.

12

Indexing strategy

Design a spell-check suggestion map

Build a map from simplified word forms to possible dictionary words and discuss collision-like ambiguity.

13

Hash table internals

Implement a hash table with chaining

Write put, get, remove, and resize logic using buckets that store linked entries.

14

Performance experiment

Measure load factor effects

Simulate inserts into a hash table and report how collision counts change as load factor grows.

15

Correctness risk

Handle mutable key risks

Explain how changing fields used in hashCode or equals after insertion can break map lookup.

16

Set intersection

Find common elements across lists

Given several lists, return values that appear in every list while avoiding duplicate output.

17

Map from token to records

Build an inverted index

Given document titles and words, map each word to the set of documents where it appears.

18

Set operations

Track online users

Given login and logout events, maintain the set of active users and report the peak active count.

19

Set uniqueness

Validate a simple Sudoku region

Check whether rows, columns, and small regions contain no repeated nonzero values.

20

Map by transformed key

Find pairs with equal difference

Given point coordinates, group pairs by slope or difference while avoiding double counting.

21

Map plus queue timestamps

Design a rate limiter

Track recent request times per user and decide whether each new request is allowed.

22

Nested maps

Summarize grade changes

Use a map from student to another map of assignment scores, then compute each student latest average.

23

Map from word to positions

Find shortest word distance

Preprocess word positions so repeated distance queries can be answered efficiently.

24

Visited set

Detect cycle in value mapping

Given a map where each key points to another key, detect whether following links enters a cycle.

25

Map normalization

Build a synonym replacement index

Replace words using a synonym map while preserving words not found in the map.

26

Sliding window map

Count distinct values in windows

For each window of size k, report how many distinct values appear.

27

Sliding window map

Find longest substring without repeats

Use a map of last-seen positions to maintain a window with unique characters.

28

Indexing with maps

Design a roster lookup service

Create maps by student ID, course name, and grade level, then explain which query each map supports.

29

Hash quality

Debug a bad hash function

Given a hash function that maps many strings to the same bucket, describe the performance and propose a better approach.

30

DSA support

Plan a maps-and-hashing assignment

Create a staged assignment with frequency maps, indexing, collision analysis, and code review.

Need support with Unit 5?

Work through the concepts, code, edge cases, and runtime analysis with 1:1 guidance.