Files
CSCI-1200/labs/hash_tables/README.md
2025-01-07 17:20:18 -05:00

5.2 KiB

Lab 12 — Hash Tables

In this lab, you will practice using std::unordered_set, std::unordered_map, and construct your own separate-chaining-based hash table.

Checkpoint 1: Using std::unordered_set and std::unordered_map

estimate: 15-30 minutes

Complete the isGood function in this program. This function determines if a number n is good or not. You can assume 1<=n<50000. You must write two versions of the function, one version uses std::unordered_set, the other version uses std::unordered_map.

A good number is a number defined by the following process:

  • Starting with any positive integer, replace the number by the sum of the squares of its digits.
  • Repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1.
  • Those numbers for which this process ends in 1 are good.

Return true if n is a good number, and false if not. Here are some examples:

Example 1:

Input: n = 19
Output: true

Explanation:
1^2 + 9^2 = 82
8^2 + 2^2 = 68
6^2 + 8^2 = 100
1^2 + 0^2 + 0^2 = 1
Example 2:

Input: n = 2
Output: false

To complete this checkpoint: Show a TA the two versions of your program and the test results.

Checkpoint 2: Separate Chaining Hash Table

estimate: 30-40 minutes

Complete the isGood function using separate chaining. Do not use any of these: std::unordered_map, std::unordered_set, std::map, std::set.

To complete this checkpoint: Show a TA these additions and the test output.

Checkpoint 3

estimate: 30-40 minutes

Form a team of two members. Complete the longestConsecutive function in this program. You must use a separate-chaining-based hash table. Do not use any of these: std::unordered_map, std::unordered_set, std::map, std::set.

The longestConsecutive function takes an unsorted std::vector of integers, and returns the length of the longest consecutive elements sequence.

Example 1:

Input: nums = [100,4,200,1,3,2]
Output: 4
Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4.
Example 2:

Input: nums = [0,3,7,2,5,8,4,6,0,1]
Output: 9
Explanation: The longest consecutive elements sequence is [0, 1, 2, 3, 4, 5, 6, 7, 8]. Therefore its length is 9.

To complete this checkpoint: Show a TA your program and the test results.