27.10.2014 Views

Cracking the Coding Interview, 4 Edition - 150 Programming Interview Questions and Solutions

Cracking the Coding Interview, 4 Edition - 150 Programming Interview Questions and Solutions

Cracking the Coding Interview, 4 Edition - 150 Programming Interview Questions and Solutions

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Solutions</strong> to Chapter 7 | Object Oriented Design<br />

7.2 Imagine you have a call center with three levels of employees: fresher, technical lead<br />

(TL), product manager (PM). There can be multiple employees, but only one TL or PM.<br />

An incoming telephone call must be allocated to a fresher who is free. If a fresher<br />

can’t h<strong>and</strong>le <strong>the</strong> call, he or she must escalate <strong>the</strong> call to technical lead. If <strong>the</strong> TL is<br />

not free or not able to h<strong>and</strong>le it, <strong>the</strong>n <strong>the</strong> call should be escalated to PM. Design <strong>the</strong><br />

classes <strong>and</strong> data structures for this problem. Implement a method getCallH<strong>and</strong>ler().<br />

SOLUTION<br />

pg 62<br />

All three ranks of employees have different work to be done, so those specific functions are<br />

profile specific. We should keep <strong>the</strong>se specific things within <strong>the</strong>ir respective class.<br />

There are a few things which are common to <strong>the</strong>m, like address, name, job title, age, etc.<br />

These things can be kept in one class <strong>and</strong> can be extended / inherited by o<strong>the</strong>rs.<br />

Finally, <strong>the</strong>re should be one CallH<strong>and</strong>ler class which would route <strong>the</strong> calls to <strong>the</strong> concerned<br />

person.<br />

NOTE: On any object oriented design question, <strong>the</strong>re are many ways to design<br />

<strong>the</strong> objects. Discuss <strong>the</strong> trade-offs of different solutions with your interviewer.<br />

You should usually design for long term code flexibility <strong>and</strong> maintenance.<br />

1 public class CallH<strong>and</strong>ler {<br />

2 static final int LEVELS = 3; // we have 3 levels of employees<br />

3 static final int NUM_FRESHERS = 5; // we have 5 freshers<br />

4 ArrayList[] employeeLevels = new ArrayList[LEVELS];<br />

5 // queues for each call’s rank<br />

6 Queue[] callQueues = new LinkedList[LEVELS];<br />

7<br />

8 public CallH<strong>and</strong>ler() { ... }<br />

9<br />

10 Employee getCallH<strong>and</strong>ler(Call call) {<br />

11 for (int level = call.rank; level < LEVELS - 1; level++) {<br />

12 ArrayList employeeLevel = employeeLevels[level];<br />

13 for (Employee emp : employeeLevel) {<br />

14 if (emp.free) {<br />

15 return emp;<br />

16 }<br />

17 }<br />

18 }<br />

19 return null;<br />

20 }<br />

21<br />

22 // routes <strong>the</strong> call to an available employee, or adds to a queue<br />

CareerCup.com<br />

1 5 2

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!