Files
CSCI-1200/labs/11_hash_tables/README.md
2023-11-07 02:20:38 -05:00

5.1 KiB

Lab 11 — 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 isHappy function in this program. This function determines if a number n is happy 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 happy 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 happy.

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

Example 1:

Input: n = 19
Output: true

Explanation:
12 + 92 = 82
82 + 22 = 68
62 + 82 = 100
12 + 02 + 02 = 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 isHappy 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,4,5,6: Separate Chaining Hash Table (Yes, it's Checkpoint 3,4,5,6, as there are 3 extra credits for making this program work.)

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 checkpoint (3,4,5,6): Show a TA your program and the test results.

Note: checkpoint (3,4,5,6) is very challenging, if you can't get it work during your lab period, make sure you understand it completely after the lab period, because one of the four problems on Test 3, will be about separate-chaining-based hash tables. It's 100% certain.