GfG QA is closing soon... Please refer PRACTICE or GfG now on wards !!

Google Interview: Design an Cache system for Worker objects

 

You are given an scenario where there is an Worker object. Each Worker object has one mandatory field unique id workerId and their are few other non unique and not mandatory Ids : DeptId and UnitID. 
Design an cache for your Worker objects considering the fact that most of the queries on your cache will be on unitId. If the there is an query for an Worker which is not available in your cache then that data will be fetched from the server. 
Design an cache such that there will be minimum number of server calls. Imagine there are 100;s of such non unique ids non mandatory Ids in the Worker object and you have to design the cache so that your client can query on any given id with minimum calls to server. 

Your Worker object looks like this: 
Worker1=> workerid=1, deptId=>765, unitId=>123 Name=“” Surname=“” Data=“” 
Worker2=> workerid=2 , deptId=>765 Name=“” Surname=“” Data=“” 
Worker3=> workerid=3, unitid=>123 Name=“” Surname=“” Data=“” 

so when your client queries using unitId getWorkersbyUnitId(123) it should return Worker1 and Worker3. 
If query is on deptId 765 then it should return Worker1 and Worker2 

You can assume there is never an query on individual workerId. 

other information given while discussion is you can query the server using any id and all the worker objects of that id will be fetched by that single call: Example there are 10 workers associated with untId: 99 then server call to getserverUnitIdWorkers(99)-> will output list of 10 workers. Each unitId or any other non mandatory id has approximate 8 to 10 workers associated with it. And there are millions of such unitIds.

 

asked Apr 16, 2016 by GeeksforGeeksUser
...