Skip to content
Advertisement

How to implement a java plugin library where you can search methods by signature

I was asked this question during an interview and could not come up with a good solution. Basically, I have to implement a java library where clients can register there methods and later search for methods by their signature.

Below is the Function class —

class Function {
  public final List<String> argumentTypes; // e.g. ["Integer", "String", "PersonClass"]
  public final String name;

  Function(String name, List<String> argumentTypes) {
    this.name = name;
    this.argumentTypes = argumentTypes;
  }

  public String toString() {
    return this.name;
  }
}

The above class cannot be changed. And then I have this skeleton code —

 class FunctionLibrary {
     void register(Set<Function> functions) {
      
      }
      List<Function> findMatches(List<String> argumentTypes) {
    
        return null;
      }
}








    
    
         

What should be the implementation of the findMatches and register methods ?

Advertisement

Answer

register gets a set of functions and adds them to a collection? i think this can be done in O(1) for each function.

create a HashMap<String, List<Function>> and in register() method for each function in the set, add it to the appropriate list in the map according to its arguments by making a new string concatenating the arguments and adding an underscore between them for example the map would be: “int_string_long” -> [F1, F2…]

then in findMatches() simply concat the strings from the arguments with an underscore between them and “get” that key from the map is it exists. this is also O(1) for each search.

do i get the job now? 🙂

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement