The words you are searching are inside this book. To get more targeted content, please make full-text search by clicking here.
Discover the best professional documents and content resources in AnyFlip Document Base.
Search
Published by Aditya Diwan, 2024-03-13 06:55:17

Data Structure Question and Answer

Data Structure Question and Answer

Ques20: Write a menu driven program to sort the elements of an array in descending order using selection sort and Insertion sort. Ans:


Created By: - Assigned By: - Sumit Debbarma Mr. Aditya Diwan Sir Deep Raj Gupta Suman Das Rohit Roy


Unit 4 Chapter 11 Hashing


Unit 4 Chapter 11 Hashing Question & Answers: - Ques1: What is Hashing? Ans: Hashing is the process of mapping keys to their appropriate locations in the hash table. It is the most effective technique of searching the values in an array or in a hash table. Hashing allows to update and retrieve any data entry in a constant time 0(1). Ques2: what is the need of Hashing? Ans: • Ensures the integrity of data by generating fixed-size hash values that uniquely represent the original data. • hashing is an important data structure designed to solve the problem of efficiently finding and storing data in an array. • Hashing is an efficient method to store and retrieve elements. • With hashing we can narrow down the search and find the number within seconds. Ques3: Define Hash function. Ans: the function of converting the key into the table/array index is called Hash function. It is usually denoted by ‘H’. Ques4: Define Hash table. Ans: Hash Table is a data structure which stores data in an associative manner. In a hash table, data is stored in an array format, where each data value has its own unique index value. Access of data becomes very fast if we know the index of the desired data.


Ques5: Explain Hashing Mechanism with an example. Ans: Hashing is the process of transforming any given key or a string of characters into another value. This is usually represented by a shorter, fixed-value or key represents and makes it easier to find or employ the original string. Hashing in a data structure is a two-step process. 1. The hash function converts the item into small integer or hash value. This integer is used as an index to store the original data. 2. it stores the data in a hash table. We can use a hash key to locate the data quickly. Ques6: Explain the various methods of choosing a hashing function? Ans: 1. Division Method The division method is quite good for just about any value of M and since it requires only a single division operation, the method works very fast. However, extra care should be taken to select a suitable value for M. As said early it is the simplest method of hashing an integer x. This method divides x by M and then uses the remainder obtained. In this case, the hash function can be given as h(x) = x mod M The number of collisions depend on the value of ‘M’. if it is a power of two then is sure that we will get more collisions. So, we should take the ‘M’ as a prime number for minimizing the collisions.


2. Midsquare Method In this method, we square the key first. Then we take some digits from the middle of this number as the generating address. The hash function is H(K) = m Where ‘m’ is obtained by removing some digits from both ends of “K2” . But it should be noted that the digits taken from “K2” should be at the same positions. 3. Folding Method This is the easiest way of computing that key where the key is broken into pieces and then adding all of them to get the hash address. Each piece except possibly the last, should have the same number of digits as the required address. For this we ignore the last carry. The hash function is: H(K) = K1 + K2 + ….. + Kn Where K1, K2 …… Kn are the broken pieces. Sometimes, we can reverse some pieces (say even-numbered pieces K2, K4 …..) and then add to get the hash address.


4. Mixed method Sometimes, we can mix up other methods with the division method in order to ensure that addresses are in the range of hash table. So, by means of mixed method we get the addresses in the desired range once a division method is performed. But one thing should be kept in mind that doing one more and complete operation with the digits of the key will affect the first criteria choosing the good hash function i.e. easy computation of the key. Ques7: What is Hash Collision? Give one example. Ans: Hash collision is said to occur when two keys generate the same value. Whenever there is one or more than one key that point/map to the same slot in the hash table, the phenomenon is called a collision.


Ques8: What are the techniques of resolving Hash Collisions? Ans: 1. Open Addressing • Linear Probing. • Quadratic Probing. • Double Hashing. • Rehashing. 2. Chaining • Linear probing. • Quadratic probing. • Double probing. Ques9: Explain Collision Resolution with Open Addressing. Ans: ➢ once a collision takes place, open addressing or closed hashing computes new positions using a probe sequence and the next record is stored in that position. ➢ The hash table contains two types of values: sentinel values (e.g., -1) and data values. ➢ The presence of sentinel values indicates the location contains no data value at present but can be used to hold a value. ➢ When key is mapped to a particular memory location, then the value it holds is checked. ➢ If it contains a sentinel value, then the location is free and data value can be stored in it. ➢ If the location has some values in it, then other slots are checked systematically in the forward direction to find a free slot.


➢ If a single free location is not found, then we have an OVERFLOW condition. Ques10: Explain Collision Resolution by Chaining. Ans: ➢ Collision resolution by chaining combines linked representation with hash table. ➢ When two or more records hash to the same location, these records are constituted into a singly-linked list called a chain. ➢ In chaining, we store all the values with the same index with the help of linked list. ➢ In this method, a chain of elements is maintained which have the same hash address. ➢ The hash table here behaves like an array of pointers. ➢ Each location in the hash table stores a pointer to the linked list, which contains all the key elements that were hashed to that location. ➢ The major drawback of chaining is that a linked list requires extra pointers. Collision Resolution by Chaining Ques11: Explain Linear Probing with an example. Ans: ➢ This hashing technique find the hash key. [or] Generated address value through hash function and maps the key on particular position in the hash table. ➢ In case if the key has same hash address, then it will find the next empty position in the hash table. ➢ We take hash table as a circular array. ➢ So if the ‘table size’ is ‘N’, then after (N-1) position, it will search for 0 th position in the array.


➢ For searching the element, first we check the hash generated address position in the table. ➢ If element is not available at that position, then we sequentially search the element after the has address position. ➢ The main disadvantage of the linear probing technique is the records tend to cluster i.e., when half of the table is full, then it is difficult to find the empty position in the hash table in case of hash clash. ➢ Searching will also become slow because it will go for linear searching. Ques12: Explain Quadratic Probing with an example. Ans: In order to minimize the clustering problem, quadratic probing can be used. Suppose a hash generating address is ‘a’ then in the case of collision, linear probing searches the location a, a+1, a+2…. So the location where the insertion and searching takes place is (a+i) (for i = 0,1, 2,…). But in quadratic probing, the location of insertion and searching takes place in (a+i2 ) (for i = 0,1,2,…) i.e., at the locations a, a+1, a+4, a+9……. So it will decrease the problem of clustering but the problem of this technique is it cannot search all the locations. If the ‘tablesize’ is prime number then it will search at least half of the locations of the hash table.


Ques13: Explain Double Hashing with an example. Ans: Double hashing requires the hashing second time in case of collision. Suppose the generated address of a hash function ‘H’ is ‘a’, then in case of collision we will again do the hashing of this hash function. Let that hash function is H1 and its address is a1. Now we will insert and search at the locations a, a+a1 , a+2a1 , a+3a1…. But it requires two times the calculation of hash function which creates it complex and searching will also be slower than linear and quadratic probing. Here, the next probing depends on the second hash function H1 . So we should be very careful in choosing H1. Ques14: Explain Rehashing with an example. Ans: ➢ Sometimes, there are chances of insertion failure when hash table is full. So The solution for this particular case is to create a new table with the double size of the previous hash table.


➢ Here we will use a new hash function and insert all the elements of the previous hash table. ➢ We will scan the elements of the previous hash table one by one and calculate the has key with the new has function and insert them into new hash table. This technique is called rehashing. ➢ It ensures the insertion of element in hash table. Ques15: What are the Characteristics of Good Hash Function? Ans: • Easy to Compute: it should be computed quickly and returns values within the range of the Hash-Table. • Uniform Distribution: it should provide a uniform distribution of hash values. A non-uniform distribution increases the number of collisions and the cost of resolving them. • Less collisions: chose a hash function with a good collision resolution algorithm which can be used to compute alternative index if the collision occurs. • High Load Factor: It should have a high load factor for a given set of keys. Load factor = Number of elements in hash Table / Hash Table size. Ques16: What do you mean by Probing? Ans: The process of examining memory locations in that hash table is called Probing. Created By: - Assigned By: - Sumit Debbarma Mr. Aditya Diwan Sir Deep Raj Gupta Suman Das Rohit Roy


Click to View FlipBook Version