DSA Assignment 2
DSA Assignment 2
COURSE NAME
Data Structures (CS-203)
SEMESTER
3rd Fall 2024
SUBMMITED BY
ROSHAN AKMAL
(23014119-103)
DEPARTMENT
COMPUTER SCIENCE
SECTION
B (EVE)
SUBMMITED TO
SIR TASADDAQ LATIF
Q1. What is a hash function? Define it and discuss its significance in the field of
computer science.
Answer:
A hash function is a function that takes an input (or "message") and returns a fixed-size
string of bytes. The output is known as the hash value or hash code. Hash functions are
widely used in computer science for data retrieval, data integrity, and cryptography.
1. Data Retrieval: Hash functions are crucial in data structures like hash tables,
where they are used to quickly locate a data record given its key. The hash function
transforms the key into an index in an array, enabling constant time complexity
(O(1)) for search, insert, and delete operations.
2. Data Integrity: Hash functions help ensure data integrity by generating a unique
hash for a given piece of data. If the data changes, even slightly, the hash will
change significantly, making it easy to detect any alterations or errors in the data.
3. Cryptography: In cryptographic applications, hash functions are used in algorithms
like digital signatures, password storage, and blockchain technology. They provide
secure ways to verify data without revealing the original data.
4. Collisions: While hash functions are designed to produce unique outputs for
different inputs, collisions can occur when two different inputs produce the same
hash value. This is generally rare and is a key consideration in designing strong hash
functions.