Junior — Middle
Explain what a hash function is and what it is used for.
sobes.tech AI
Answer from AI
A hash function is an algorithm that transforms input data (such as a string or file) into a fixed-size string or number, called a hash or hash code. The main properties of a hash function:
- Fast computation of the hash for any data.
- The same input always produces the same hash.
- Different data likely produce different hashes (collision minimization).
In programming, hash functions are used for:
- Fast data lookup and comparison (e.g., in hash tables or dictionaries).
- Data integrity verification.
- Cryptographic purposes (hashing passwords, digital signatures).
Example in Python:
# Getting the hash of a string
s = "example"
h = hash(s)
print(h)
Here, the hash function returns an integer hash of the object.