三道关于面向对象设计的面试题

题一

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().

分析一下,我们大概可以确定有如下类:

call 这个类应该有

rank: 这个call的级别

content: 这个call的内容

reply(): 回复这个call

disconnect():挂断这个call


employee 这个类有雇员的一般信息(这里就不考虑这个了). 同时应该有几个重要属性和方法:

rank:雇员可以处理的事物级别。

free:雇员此刻是否空闲。

receiveCall(): 雇员被派发了一个call。

canHandleCall(): 雇员处理一个call。

canNotHandleCall(): 雇员无法处理一个call时。这里会选择将call的级别加一,并再次dispatch这个call。


fresher,technicalLead,productMmanager。这3个类应该继承employee类。其中两个类可以考虑使用singleton模式保证只能实例化一个实例。

callHandler 这个类应该实现题目中要求的getCallHandler()方法,这个方法返回一个符合要求的雇员。

实例化不同的雇员。

取得一个call

派发一个call,这个方法从getCallHandler()得到一个合适的雇员,并调用他的receiveCall()。

public class CallHandler{
    static final int LEVELS = 3;
    static final int NUM_FRESHERS = 5;
    ArrayList[] employeeLevels = new ArrayList[LEVELS];
    // queues for each call’s rank
    Queue[] callQueues = new LinkedList[LEVELS];

    public CallHandler() { ... }

    Employee getCallHandler(Call call) {
        for (int level = call.rank; level < LEVELS - 1; level++) {
            ArrayList employeeLevel = employeeLevels[level];
            for (Employee emp : employeeLevel) {
                if (emp.free) {
                return emp;
                }
            }
        }
        return null;
    }

    // routes the call to an available employee, or adds to a queue
    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);
        } else {
        // place the call into queue according to its rank
            callQueues[call.rank].add(call);
        }
    }
    void getNextCall(Employee e) {...} // look for call for e’s rank
    }

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

    class Employee {
        CallHandler callHandler;
        int rank; // 0- fresher, - technical lead, - product manager
        boolean free;
        Employee(int rank) { this.rank = rank; }
        void ReceiveCall(Call call) { ... }
        void CallHandled(Call call) { ... } // call is complete
        void CannotHandle(Call call) { // escalate call
            call.rank = rank + 1;
            callHandler.dispatchCall(call);
            free = true;
            callHandler.getNextCall(this); // look for waiting call
        }
    }

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


题二

Design the data structures for an online book reader system.


题三
Explain how you would design a chat server. In particular, provide details about the various backend components, classes, and methods. What would be the hardest
problems to solve?

这设计题并不难,需要面试的时候和面试人沟通,确定更多的细节。这里我们可以假设


题四

Explain the data structures and algorithms that you would use to design an in-memory !le system. Illustrate with an example in code where possible.

大家先看看,之后再补充答案。



你可能感兴趣的:(设计模式)