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.
- Word formation using concatenation of two dictionary words
- Print all valid words that are possible using Characters of Array
- Binary Indexed Tree or Fenwick Tree
- How to Implement Reverse DNS Look Up Cache?
- Red-Black Tree | Set 1 (Introduction)
- Suffix Array | Set 1 (Introduction)
- Longest prefix matching - A Trie based solution in Java
- Segment Tree | Set 1 (Sum of given range)
- Pattern Searching using Suffix Tree
- Given a sequence of words, print all anagrams together | Set 2
- Print unique rows in a given boolean matrix
- LRU Cache Implementation
- AVL Tree | Set 1 (Insertion)
- Trie | (Delete)
- Trie | (Insert and Search)