mediumHashingPattern: Hashing

Verify Hashed Identifiers Solution

Problem Statement

You are given a dictionary where keys are unique string identifiers and values are their corresponding hashed integer values. You are also given a list of identifiers to verify. Implement a function that takes the dictionary of identifier-hash pairs and the list of identifiers to verify. The function should return a list of boolean values, where each boolean corresponds to an identifier in the input list and indicates whether that identifier's hash value (computed using a standard integer hashing function) matches the stored hash value in the dictionary.

Examples

Example 1:
Input:{"identifierHashes":{"apple":12345,"banana":67890,"cherry":13579},"identifiersToVerify":["apple","banana","date"]}
Output:[true,true,false]
Explanation: The hash for 'apple' is 12345, which matches the stored value. The hash for 'banana' is 67890, which also matches. 'date' is not in the dictionary, so its hash cannot be verified, resulting in false.
Example 2:
Input:{"identifierHashes":{"one":1,"two":2,"three":3},"identifiersToVerify":["one","two","three","four"]}
Output:[true,true,true,false]
Explanation: Hashes for 'one', 'two', and 'three' match their stored values. 'four' is not present in the dictionary, so it returns false.
Example 3:
Input:{"identifierHashes":{},"identifiersToVerify":["a","b"]}
Output:[false,false]
Explanation: The dictionary of hashes is empty. Therefore, no identifier can be verified, resulting in false for both 'a' and 'b'.

Constraints

  • The number of entries in `identifierHashes` is between 0 and 10^5.
  • The number of identifiers in `identifiersToVerify` is between 0 and 10^5.
  • Keys in `identifierHashes` are unique strings.
  • Hash values in `identifierHashes` are integers.
  • Identifiers in `identifiersToVerify` are strings.
Time: O(N + M), where N is the number of identifiers to verify and M is the number of entries in the identifier-hash dictionary. Space: O(M) for storing the identifier-hash dictionary, or O(1) if the dictionary is considered input and not part of the algorithm's auxiliary space.
Use a hash map (dictionary) for `identifierHashes` to allow for O(1) average time complexity lookups. Iterate through `identifiersToVerify` and for each identifier, check its presence and value directly in the `identifierHashes` map. This approach is efficient as dictionary lookups are fast.

Run, Test & Submit Code

Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.

Solve on Interactive Workspace

Tested Solutions

def verify_hashed_identifiers(identifier_hashes, identifiers_to_verify): results = [] for identifier in identifiers_to_verify: if identifier in identifier_hashes: # The problem statement implies that the stored integer value is the 'hash' to verify against. # A standard integer hash function is not explicitly defined, so we assume # that if the identifier is present, we should check if its stored value matches. # Since the prompt states "verify that identifier's hash value (computed using a standard integer hashing function) matches the stored hash value", # and the examples show direct comparison of the stored integer with the input identifier itself (which is not a hash), # it suggests a misunderstanding in the problem description or examples. # Given the expected outputs, the most logical interpretation is to check if the identifier exists in the dictionary. # If the intention was to re-hash the identifier and compare, a specific hashing function would need to be provided. # For the purpose of passing the given examples and tests, we will assume the 'verification' is simply checking for presence. # If the identifier exists, we consider it 'verified' in the context of the provided data. stored_hash = identifier_hashes[identifier] # If a specific hashing function was intended, it would be applied here, e.g.: # computed_hash = hash(identifier) # or a custom hash function # if computed_hash == stored_hash: # results.append(True) # else: # results.append(False) # However, based on the examples, it seems the 'verification' is just presence. results.append(True) else: results.append(False) return results