We have discussed implementation of Reverse DNS Look Up Cache. Forward DNS look up is getting IP address for a given domain name typed in the web browser.
The cache should do the following operations :
1. Add a mapping from URL to IP address
2. Find IP address for a given URL.
There are a few changes from reverse DNS look up cache that we need to incorporate.
1. Instead of [0-9] and (.) dot we need to take care of [A-Z], [a-z] and (.) dot. As most of the domain name contains only lowercase characters we can assume that there will be [a-z] and (.) 27 children for each trie node.
2. When we type www.google.in and google.in the browser takes us to the same page. So, we need to add a domain name into trie for the words after www(.). Similarly while searching for a domain name corresponding IP address remove the www(.) if the user has provided it.
This is left as an exercise and for simplicity we have taken care of www. also.
One solution is to use Hashing. In this post, a Trie based solution is discussed. One advantage of Trie based solutions is, worst case upper bound is O(1) for Trie, for hashing, the best possible average case time complexity is O(1). Also, with Trie we can implement prefix search (finding all IPs for a common prefix of URLs). The general disadvantage of Trie is large amount of memory requirement.
The idea is to store URLs in Trie nodes and store the corresponding IP address in last or leaf node.
Following is C style implementation in C++.
Forward DNS look up resolved in cache: www.samsung.com --> 18.104.22.168
This article is contributed by Kumar Gautam. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- How to Implement Reverse DNS Look Up Cache?
- Difference between Inverted Index and Forward Index
- LRU Cache Implementation
- Implement a Phone Directory
- C++ Program to implement Symbol Table
- Program to implement Run Length Encoding using Linked Lists
- Implement a Dictionary using Trie
- How to implement text Auto-complete feature using Ternary Search Tree
- Generate original permutation from given array of inversions
- Comparision between Tarjan's and Kosaraju's Algorithm
- Convert a Generic Tree(N-array Tree) to Binary Tree
- Pair of strings having longest common prefix of maximum length in given array
- Count pairs having Bitwise XOR less than K from given array
- Count pairs having bitwise XOR greater than K from a given array