Amazon Interview Question for SDE-2s


Country: India




Comment hidden because of low score. Click to expand.
2
of 2 vote

In Java, we can do it using LinkedHashMap

public LRUCache(final int size) {
		this.size = size;

		cache = new LinkedHashMap<K,V>(size, 0.75f, true) {
			//Return true when LinkedHashMap should delete its eldest
			//entry
			protected boolean removeEldestEntry(Map.Entry eldest) {
				return size() > size;

			}
		};
	}

- Anonymous August 04, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

C++ Code (Queue with DLL and Hash Map)

struct LRUCacheNode {
		int key;
		int value;
		LRUCacheNode* prev;
		LRUCacheNode* next;
		LRUCacheNode(): key(0),value(0),next(NULL),prev(NULL) { } ;
} ;

class LRUCache{
private:
    hash_map< int, LRUCacheNode* >  Map;
    LRUCacheNode *  Head;
    LRUCacheNode *  Tail;
	int Capacity ;
	int Count ;
    
public:
    LRUCache(int capacity) {
		Capacity = capacity ;
		Count = 0 ;
        Head = new LRUCacheNode;
        Tail = new LRUCacheNode;
        Head->prev = NULL;
        Head->next = Tail;
        Tail->next = NULL;
        Tail->prev = Head;
    }
    
    ~LRUCache ( ) {
        delete Head;
        delete Tail;
    }
    
    int get(int key) {
        LRUCacheNode* node = Map[key];
        if(node)
        {
            DetachNode(node);
            AttachNodeToFront(node);
            return node->value;
        }
        else
        return -1 ;

    }
    
    void set(int key, int value) {
        LRUCacheNode* node = Map[key];
        if(node)
        {
            DetachNode(node);
            node->value = value;
            AttachNodeToFront(node);
        }
        else{
			LRUCacheNode* node = new LRUCacheNode ;
            if ( Capacity == Count ) { // If Cache is Full
                RemoveLRUNode ( key ) ;					
            }
            
			node->key = key;
			node->value = value;
			Map[key] = node;
			AttachNodeToFront(node);
			Count++ ;            
        }
    }
private:
	void RemoveLRUNode ( int key ) {
		LRUCacheNode* node = Tail->prev;
        DetachNode(node);
        Map.erase(node->key);
		Count-- ;
	}

    void DetachNode(LRUCacheNode* node) {
            node->prev->next = node->next;
            node->next->prev = node->prev;
    }
    
    void AttachNodeToFront(LRUCacheNode* node) {
            node->next = Head->next;
            node->prev = Head;
            Head->next = node;
            node->next->prev = node;
    }
};

- R@M3$H.N January 09, 2014 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More