GeeksforGeeks » Interview Questions

Google Interview Question about Data Structure

(6 posts)
  1. Dheeraj
    Member
    Posted 1 year ago #

    Design a layer in front of a system which cache the last n requests and the responses to them from the system. what data structure would you use to implement the cache in the later to support following operations.
    [a] When a request comes look it up in the cache and if it hits then return the response from here and do not pass the request to the system
    [b] If the request is not found in the cache then pass it on to the system
    [c] Since cache can only store the last n requests, Insert the n+1th request in the cache and delete one of the older requests from the cache
    The objective is to achieve all the three operations in O(1).

  2. Kolo
    guest
    Posted 1 year ago #

    I think it's similar to LRU cache. The solution would be based on ArrayList + HashMap (in Java) as suggested in http://dncroot.com/2007/09/18/lru-caches/

  3. Sushil
    guest
    Posted 1 year ago #

    You can use stack -- The bottom of the stack is least recently used. The stack is implemented using a doubly linked list. It has on top and one bottom pointer. In this way whenever a page is encountered and it is not in the stack -- the bottom pointer points to the page which needs to be replaced and if the page is already present then put it on the top of the stack. The top pointer points to the page which was last used....

  4. sony1
    Member
    Posted 1 year ago #

    Can somebody explain me with an example why do we need an Hash Map .I understand we need an ArrayList but not able to get why we need hash map.Please give an example.

  5. Ram
    guest
    Posted 1 year ago #

    @sony1 : hashtable allows u to retrieve the item in o(1) from the list. else it will take o(N) to retrieve the item from cache list

  6. Sharada
    guest
    Posted 4 months ago #

    Splay trees can be used for implementing caches. The most recently accessed data is maintained at the root.
    http://en.wikipedia.org/wiki/Splay_tree


Reply

You must log in to post.

RSS feed for this topic