Google Interview Question

Create a cache with fast look up that only stores the N most recently accessed items.

Interview Answers

Anonymous

Jul 14, 2009

Use a hash map and a queue.

2

Anonymous

Jul 19, 2009

To ml: N was not the index, instead some random key. The combined data structure had the implement the interface: Object get(String key); and void put (String key, Object obj); Besides the array isn't the best choice when you have to take an object out of the array and add it to the front of the queue every time the object is accessed. A double linked list works better. The hash map entries points to an item in the linked list, which then points to the object to be returned. This way you can find the item quickly to move it to the front of the queue.

Anonymous

Aug 21, 2010

This answer below has the best solution: http://stackoverflow.com/questions/1935777/c-design-how-to-cache-most-recent-used

1

Anonymous

Jul 18, 2009

I think you can do better than hash map and a queue...or just simply it. Since you're only interested in the N most recently accessed items, an array is suffice. Because N is essentially the index right?