Java Interview Practice Problem (Beginner): Domain Name Exists?

  • Post last modified:September 7, 2023
  • Reading time:4 mins read


  • The front-end team wants an API endpoint that allows them to send a List of domain names and API returns to which of them are available domains to register.
  • As a backend engineer, our task is to write a logic to take a list of domains as input and return the ones that are already registered and not available.
  • We have also been given a List of all the domains that are registered so far.


Input Domain List

  • List of domains that we need to assess if they have already registered or not.
List<String> input = List.of(

List of Registered Domains

  • List of domains that are already registered.


  • We have two domains that are not registered.

Before jumping to the solution consider giving an attempt.


  • We are discussing 4 options to solve this problem. If you have any other solution please add that to the comments, we would love to see them!


  • In this solution, we are iterating over the input domain using streams API. Then for each domain name, we search for it in a registered domain list and whichever domain exists in it we return it to the collector.
List<String> output =
        .map(d -> -> d1.equals(d)).findAny())


  • We can shorten the below code using the contains() method provided by the list.
.map(d -> -> d1.equals(d)).findAny())
  • With this modification, our solution becomes shorter.
List<String> output =


  • We can reduce our search by creating a hashmap of domain extensions ( such as .org ) with a registered domain name.
Map<String, List<String>> groupedByDomains =
       .collect(Collectors.groupingBy(d -> 
          d.substring(d.lastIndexOf("."), d.length()))

  • groupedByDomains would look like the below
{.io=[,,,, ... ], .com=[,, ... ] }
  • Once we have the reduced target search we can improve the search performance.
  • We need to do some processing before we can start the search in the target area. In the below logic, we are extracting the domain extension .com/.org and using that as a key for our hashmap to find the target search.
List<String> output =
    .filter(d -> {
        String tld = d.substring(d.lastIndexOf("."), d.length());
        return groupedByDomains.containsKey(tld) && groupedByDomains.get(tld).contains(d);


  • We can also convert registered domains into HashSet. By doing so it will allow us to search any domain in constant time.
 Set<String> domainsSet = new HashSet<String>(domains);
  • Once we have set all we have to stream over the input domain and search it in HashSet of the registered domain.
List<String> output =


  • Find all the code for this & others exercises on GitHub


  • In this problem, we provided 4 solutions using different approaches. We used streams API to iterate over input data and perform processing. Our solution contains some optimization for search as well.

Over to you! Write down your solution in the comment section for others to get back to it

Before You Leave

Other Problems List

Leave a Reply