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 --> 188.8.131.52
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.
- LRU Cache Implementation
- How to Implement Reverse DNS Look Up Cache?
- Implement a Phone Directory
- C++ Program to implement Symbol Table
- Difference between Inverted Index and Forward Index
- Program to implement Run Length Encoding using Linked Lists
- Find the number of different numbers in the array after applying the given operation q times
- Persistent Trie | Set 1 (Introduction)
- B-Tree Insert without aggressive splitting
- Index of kth set bit in a binary array with update queries
- Arrange array elements such that last digit of an element is equal to first digit of the next element
- Count of strings whose prefix match with the given string to a given length k
- Queries to update a given index and find gcd in range
- Introduction to R-tree