Interview question
Anonymous User
207

Imagine you have a number of service endpoints, and wish to invoke them concurrently to
obtain results. Each endpoint accepts a number of optional parameters and returns a list of
values. A Java interface for the service endpoint is on the next page.
Goal: Write a service manager that invokes a collection of service endpoints using a set of
parameters (a Map<String, Object>). The service manager should return an aggregate of the
results from the invoked endpoints.
The service manager may accept any map of parameters, but an endpoint should only be
invoked if it supports all of the given map’s parameters. An endpoint may be invoked with any
subset of its supported parameters. For example, an endpoint that supports “location” and
“category” would be invoked if the manager is given a map with just one parameter name
“location,” but it would be skipped if the manager were invoked with a map with three
parameters, “location,” “category,” and “price.” An empty parameter map would invoke all
service endpoints.
Design and implement a thread-safe API for the service manager which can concurrently invoke
the applicable services while respecting the concurrency limits of each endpoint.
Notes:
• Please write your solution in Java. Use any Java version and libraries you think are
helpful. (We typically use Java 8 and Guava and edit with IntelliJ.)
• Design the API however you think is best. Enhancements (such as returning details of
failures, or omitted endpoints) are welcome, but prioritize correctness and clarity. Feel
free to include ideas for improving your solution that you have not implemented.
• We’ll evaluate responses for correctness, clean design, and coding style.
• Incorporate the ServiceEndpoint interface (below) into your own code.
• Include any tests, stubs, or harnesses you write.
Java code for the ServiceEndpoint interface:
import java.util.List;
import java.util.Map;
import java.util.Set;
/**

  • A simple service interface that combines an {@link #invoke(java.util.Map)}
  • method to accept a map of named parameters and return zero or more results with
  • additional policy methods that specify {@link #getSupportedParameters() which
  • parameters are accepted} and {@link #getMaxConcurrentInvocations() the maximum
  • number of concurrent invocations allowed}.
    /
    public interface ServiceEndpoint {
    /
    *
  • @return the maximum number of concurrent invocations allowed, or {@code -1}
  • if unlimited.
    /
    int getMaxConcurrentInvocations();
    /
    *
  • @return the set of parameter names supported by this service
    /
    Set getSupportedParameters();
    /
    *
  • Invoke the service with a map of named parameters (any subset of the supported
  • list of parameters) and return the results.
  • @param parameters a map from parameter names to values
  • @return the results of
    */
    List invoke(Map<String, Object> parameters) throws InvocationException;
    }
Comments (1)