TopcoderArchive
Archive
Algorithm problem

ProcessingRequests

Problem statement, definition, constraints, and public examples.

Problem Statement

Write a program to receive requests from two clients, process them, and obtain the summary of requests.

Given class RequestsUtils:

class RequestsUtils {
  /**
   *  Enumeration of clients.
   */
  public enum ClientNumber {
    Client1, Client2;
  };

  /**
   *  Creates a RequestsUtils object with the given data seed.
   * 
   *  @param dataSeed An integer representing the data seed.
   */
  public RequestsUtils(int dataSeed);

  /**
   * Gets the client object for a given client number.
   *
   * @param client A ClientNumber value representing a client.
   * @return The Client object for the given client.
   */
  public Client getClient(ClientNumber client);

  /**
   *  Processes the given request from the given client. This method works for about
   *  50 milliseconds and blocks the current thread until the request is complete.
   *  The method can be called from at most one thread at the same time. 
   * 
   *  @param client The ClientNumber for the request.
   *  @param data The Data to be processed.
   */
  public void processRequest(ClientNumber client, Data data);

  /**
   *  Gets the summary of all requests processed by this object.
   * 
   *  @return The compressed summary of processed requests.
   */
  public int getRequestsSummary();
}

Given class Data:

Represents data that needs to be processed.

class Data {
  /**
   * Gets whether or not the Data is empty.
   * 
   * @return True if and only if the Data is non-empty.
   */
  public boolean isEmpty();
}

Given class Client:

A client will have at most 15 requests to process data.

class Client {
  /**
   *  Blocks until the next request is available. If there are no more requests from this client,
   *  empty data is returned and does not need to be processed. Waiting for a request can
   *  take some time - at most 100 milliseconds. Only one thread may call this method per instance.
   *
   *  @return The Data to be processed.
   */
  public Data waitForRequest();
}

DEFINITION

Define public class ProcessingRequests with public method int getResult(int dataSeed).

getResult must be implemented as follows:

GET CLIENTS

Create RequestsUtils utils by passing dataSeed to RequestsUtils(int).

Use utils.getClient(ClientNumber) to get both Client instances.

PROCESS THE REQUESTS

Process all requests from both clients by using the processRequest() method. The requests can be processed in any order, but requests from the same client must be processed in the order that they are received.

GET SUMMARY OF REQUESTS

After all the requests are processed, return utils.getRequestsSummary().

Definition

Class:
ProcessingRequests
Method:
getResult
Parameters:
int
Returns:
int
Method signature:
int getResult(int dataSeed)
(be sure your method is public)

Notes

  • The execution time of your program must not exceed 2 seconds. You are recommended to use multithreading to decrease the execution time in this problem.

Constraints

  • dataSeed will be between 0 and 1,000,000,000, inclusive.

Examples

  1. 1
    Returns: 1288753421
    There will be 1 request from the first client and no requests from the second client.
  2. 2
    Returns: -1702426868
    There will be 2 requests from the first client and 3 requests from the second client.
  3. 3
    Returns: 1729917023
    There will be 6 requests from the first client and 10 requests from the second client.
  4. 4
    Returns: 1960042076
    There will be 15 requests from the first client and 15 requests from the second client.
Back to all problems