Consider a scenario where you want to design a system for storing records of employees of your organization keyed using their employee IDs. We need this system primarily to do the following 3 tasks:
What are the data structures that we can think of for performing the above functionalities for our system efficiently? Well, let’s analyze about implementing this system using the known data structures:
O(Logn)
time. But this will have an impact on insert and delete functionalities because we need to maintain sorted order of arrays upon every insert and deleting of records.
A BBST is a binary tree in which the left and right subtrees of every node always differs in height by no more than 1.
Here, due to the nature of BBST data structure, we can guarantee that the search, insert and delete functionalities can take place in O(Logn)
time.
But, is there any more efficient way? Let’s look for other options.
O(1)
retrieval time complexity property of Arrays to its advantage. Lets see how below.
null
if employee ID is not present, else the value stores pointer to data mapping to the employee ID.
O(1)
time which is the best possible we can achieve.
Consider a scenario where you use cloud services to store your files and data. Now you need to be assured that the third party cloud service providers dont tamper with your files. We do so by computing hash of that file/data using a cryptographic hashing algorithms. We store these computed hashes in our local machines for reference.
Next time whenever we download the files from cloud storages, we can directly compute the hash of the files. We match it with the locally stored hash that was previously computed.
If the file has been tampered by the service providers, the hash value of the file would definitely change. If it hasnt been tampered then the hash values of the downloaded file and the locally saved value would be exactly same.
Most common cryptographic algorithm is SHA256. These are also called as digest algorithms. The files in the above example is considered to be a message.
Are Hashing algorithms secure?
Who invented Hashing?
How does Hashing work?
How is hashing implemented in Java?
import java.util.*;
class InterviewBit {
public static void main(String args[])
{
// Defining HashTable to store
// String values mapped to integer keys
Hashtable<Integer, String>
hashTable = new Hashtable<Integer, String>();
// Fill the values in HashTable
hashTable.put(1, "InterviewBit");
hashTable.put(2, "Example");
hashTable.put(5, "for");
hashTable.put(3, "HashTable");
// Display the hashtable
System.out.println(hashTable);
}
}
{1=InterviewBit, 2=Example, 5=for, 3=HashTable}
import java.util.*;
class InterviewBit {
public static void main(String args[])
{
// Defining HashMap to store
// String values mapped to integer keys
HashMap<Integer, String>
hashMap = new HashMap<Integer, String>();
// Fill the values in HashMap
hashMap.put(1, "InterviewBit");
hashMap.put(2, "Example");
hashMap.put(5, "for");
hashMap.put(3, "HashMap");
// Display the hashMap
System.out.println(hashMap);
}
}
{1=InterviewBit, 5=for, 3=HashMap, 2=Example}
Why the results of hashing cannot be reverted back to original input?
When is it not advisable to use Hashing/ Hash Tables?
Where is Hashing used?
Problem | Score | Companies | Time | Status |
---|---|---|---|---|
Colorful Number | 150 |
|
44:55 | |
Largest Continuous Sequence Zero Sum | 200 |
|
68:45 | |
Longest Subarray Length | 200 |
|
57:26 | |
First Repeating element | 200 |
|
21:50 | |
2 Sum | 300 |
|
49:18 | |
4 Sum | 325 |
|
72:27 | |
Valid Sudoku | 325 |
|
50:42 | |
Diffk II | 375 |
|
29:06 |
Problem | Score | Companies | Time | Status |
---|---|---|---|---|
Pairs With Given Xor | 200 |
|
27:20 | |
Anagrams | 350 |
|
46:36 | |
Equal | 350 |
|
71:23 | |
Copy List | 450 |
|
56:18 |
Problem | Score | Companies | Time | Status |
---|---|---|---|---|
Check Palindrome! | 200 |
|
18:56 | |
Fraction | 450 |
|
81:53 | |
Points on the Straight Line | 450 |
|
78:55 |
Problem | Score | Companies | Time | Status |
---|---|---|---|---|
An Increment Problem | 200 |
|
50:48 | |
Subarray with given XOR | 200 |
|
56:12 | |
Two out of Three | 200 |
|
33:54 | |
Substring Concatenation | 1000 |
|
71:27 |
Problem | Score | Companies | Time | Status |
---|---|---|---|---|
Subarray with B odd numbers | 200 |
|
52:47 | |
Window String | 350 |
|
82:16 | |
Longest Substring Without Repeat | 350 |
|
51:12 |