Monday, March 16, 2015

Design a Call Center


Imagine you have a call center with three levels of employees: fresher, technical lead (TL), product manager (PM).There can be multiple employees, but only one TL or PM.An incoming telephone call must be allocated to a fresher who is free.If a fresher can’t handle the call, he or she must escalate the call to technical lead.If the TL is not free or not able to handle it, then the call should be escalated to PM.Design the classes and data structures for this problem.Implement a method getCallHandler() 

This is a more complicated problem, but there are lots of interesting parts.

First, we need the employee class, and three subclasses to handle fresher, TL and PM. Since except the rank, all three roles share the same information (duties, personal information, etc. ). We will focus on the function of the call center (receive calls) to design this class. So this class will have the following fields and methods:

A CallHandler object: the CallHandler class is the call coordinator we will discuss later.
int rank: the rank of the employee (0 for fresher, 1 for TL and 2 for PM).
boolean free: if the employee is free right now.
receiveCall(): method to receive a call
callHandled():complete a call
cannotHandle(): if the employee with the current rank cannot handle the call


class Employee{
 CallHandler callHandler;
 int rank;//0 - fresher, 1 - technical lead, 2 - product manager
 boolean free;
 Employee(int rank) {
  this.rank = rank;
 }
 void receiveCall(Call call){}
 void callHandled(Call call, String message){
  call.reply(message);
  call.disconnect();
  callHandler.getNextCall(this);
 }//complete call
 //If the employee with the current rank cannot handle the call
 void cannotHandle(Call call){
  //escalate the call to a higher rank
  call.rank = rank + 1;
  callHandler.dispatchCall(call);
  free = true;
  callHandler.getNextCall(this);
 }
}

class Fresher extends Employee{
 public Fresher() {super(0);}
}
class TechLead extends Employee{
 public TechLead() {super(1);}
}
class ProductManager extends Employee{
 public ProductManager() {super(2);}
}


Second, the CallHandler class, which is used to coordinate the calls. According to the problem statement, the call is first assigned to the available with the lowest rank, if the employee cannot handle the call, the call needs to be escalated to a higher rank person, if no higher rank employee is available, the call will be added to a queue. And the current employee will try to handle the next call.

LEVELS = 3: we have 3 levels of employees
NUM_FRESHERS: the number of freshers
List<Employee>[]: an array of employee list. We need all employee's information to know who is available for the call, the array has a length of 3, i.e., 3 levels.
Queue<Call>[]: the queue waiting list. If no one is available to take the call, let the call wait until the next available employee at current level.


public class CallHandler{
 static final int LEVELS = 3;
 static final int NUM_FRESHERS; //assume we have 5 freshers
 List[] employeeLevels;
 Queue[] callQueues;//enqueue waiting calls
 
 public CallHandler(int NUM_FRESHERS){
  this.NUM_FRESHERS = NUM_FRESHERS;
  employeeLevels = new ArrayList[LEVELS];
  callQueues = new LinkedList[LEVELS];
 }
 //return the first available person equal or higher than 
 //current call rank
 Employee getCallHandler(Call call){
  for(int level = call.rank; level < LEVELS - 1; level++){
   List employeeLevel = employeeLevels[level];
   for(Employee emp : employeeLevel){
    if(emp.free) return emp;
   }
  }
  return null;
 }
 void dispatchCall(Call call){
  //try to route the call to an employee with minimal rank
  Employee emp = getCallHandler(call);
  if(emp != null)
   emp.ReceiveCall(call);
  //if no one is available for the call, put the call on the queue
  //waiting for the next available employee
  else
   callQueues[call.rank].add(call);
 }
 //look for call at e's rank
 void getNextCall(Employee e){
  if(callQueues[e.level].size() != 0){
   Call next = callQueues[e.level].poll();
   e.receiveCall(next);
  }
 } 
}


Third, the Call class. This class is nothing special, we need it because, we need calls.


class Call {
 int rank = 0; // minimal rank of employee who can handle this call
 public void reply(String message) {}
 public void disconnect(){}//end call
}

No comments:

Post a Comment