VOOZH about

URL: https://www.geeksforgeeks.org/dsa/implement-reverse-dns-look-cache/

⇱ How to Implement Reverse DNS Look Up Cache? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Implement Reverse DNS Look Up Cache?

Last Updated : 23 Jul, 2025

Reverse DNS look up is using an internet IP address to find a domain name. For example, if you type 74.125.200.106 in browser, it automatically redirects to google.in. 
How to implement Reverse DNS Look Up cache? Following are the operations needed from cache:

  • Add an IP address to URL Mapping in cache.
  • Find URL for a given IP address.

One solution is to use Hashing
In this post, a Trie based solution is discussed. One advantage of Trie based solutions is, the 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 urls for a common prefix of IP addresses). 
The general disadvantage of Trie is large amount of memory requirement, this is not a major problem here as the alphabet size is only 11 here. Ten characters are needed for digits from '0' to '9' and one for dot ('.'). 
The idea is to store IP addresses in Trie nodes and in the last node we store the corresponding domain name. Following is C style implementation in C++.
 


Output: 
Reverse DNS look up resolved in cache:
107.108.11.123 --> www.samsung.com

 

Time complexity- The time complexity of the insert function is O(k), where k is the length of the IP address being inserted. The searchDNSCache function also has a time complexity of O(k), as it traverses the trie for the given IP address.

Space complexity - The space complexity of the program is O(nm), where n is the number of IP addresses and m is the length of the longest URL. This is because we are storing the URL for each IP address in the trie, which can take up to m space. Additionally, we are creating a new trie node for each level of the IP address, which can take up to 11 pointers and a boolean flag, or 12 units of space per node. Therefore, the total space complexity is O(nm12), or O(nm).


Note that the above implementation of Trie assumes that the given IP address does not contain characters other than {'0', '1',..... '9', '.'}. What if a user gives an invalid IP address that contains some other characters? This problem can be resolved by validating the input IP address before inserting it into Trie. We can use the approach discussed here for IP address validation.
Java implementation is given below: 
 


Output: 
74.125.200.106 : www.google.in
107.109.123.245 : No url associated/Invalid IP address

 

Time Complexity:

The insert() method has a time complexity of O(nm), where n is the number of IP addresses and m is the length of the IP address string.
The search() method has a time complexity of O(m), where m is the length of the IP address string.
Space Complexity:

The space complexity of the TrieNode class is O(k * n), where k is the average length of the URL and n is the number of URLs.
The space complexity of the ReverseDNSLookup class is O(1) since it only contains methods and no additional variables.
The overall space complexity of the program is O(k * n), where k is the average length of the URL and n is the number of URLs.

Python3 Implementation: 
 


Output: 
www.samsung.net
Domain name not found

 


 

Comment
Article Tags: