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 --> 126.96.36.199
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
- Range and Update Sum Queries with Factorial
- Range Sum Queries and Update with Square Root
- Count pairs (A, B) such that A has X and B has Y number of set bits and A+B = C
- Counting inversions in an array using segment tree
- Some Basic Theorems on Trees
- Range queries to count 1s in a subarray after flip operations
- Optimal sequence for AVL tree insertion (without any rotations)
- Fibonacci Heap - Insertion and Union
- Find maximum in a stack in O(1) time and O(1) extra space