Amazon Interview Question for Software Engineer / Developers


Team: KDK - Kindle Development Kit
Country: India
Interview Type: In-Person




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

O(1) insert, delete search
Implementation is easy. Just each entry of the hashTable shall have a links to the next and prev entry. So whenever accessed the entry you just move it to the head of the LinkedList like entries. If you want to add a max count of the elements in cache just whenever new node added remove the last one from the LinkedList like entries. Here is a very simple java implementation using the built in LinkedHashTable :

public class SimpleCache<E> {
 	LinkedHashMap<String, E> cache;
 
 	@SuppressWarnings("serial")
 	public SimpleCache(final int maxSize) {
 		cache = new LinkedHashMap<String, E>(maxSize, 0.75f, true) {
 			@Override
 			protected boolean removeEldestEntry(Map.Entry<String, E> eldest) {
 				return size() > maxSize;
 			}
 		};
 	}
 
 	public synchronized void add(String key, E value) {
 		cache.put(key, value);
 	}
 
 	public synchronized E get(String key) {
 		E result = cache.get(key);
 
 		if (result != null)
 			return (E) result;
 		else
 			return null;
 	}
 }

- GKalchev June 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <vector>
#include <hash_map>
 
using namespace std;
 
template <class K, class T>
struct LRUCacheEntry
{
   K key;
   T data;
   LRUCacheEntry * prev;
   LRUCacheEntry * next;
};
 
template <class K, class T>
class LRUCache
{
public:
  LRUCache(size_t sz) : head(NULL), tail(NULL){
    entries = new LRUCacheEntry<K, T>[sz];
    for (int i = 0; i < sz; i++)
      freeEntries.push_back(entries + i);
  }
   
  ~LRUCache(){  delete[] entries; } 
 
  bool containsKey(K key) { return _m.find(key) != _m.end(); }
 
  void put(K key, T data){
    LRUCacheEntry<K, T> * entry;
     
    if (_m.find(key) != _m.end()) return;
 
    if (freeEntries.size() > 0){
      entry = freeEntries.back();
      freeEntries.pop_back();
    }
    else{
      // no free entries
      entry = tail;
      if (tail->prev != NULL) tail->prev->next = NULL;
      _m.erase(entry->key);
    }
      
    _m[key] = entry;
    entry->key = key;
    entry->data = data;
    entry->next = head; 
    entry->prev = NULL;
    if (head != NULL){
      head->prev = entry;
    }
      
    head = entry;
  }
 
  // we assumes that containsKey is always called before this method
  T get(K key) {  
    LRUCacheEntry<K, T> * entry = _m[key];
   
   if (entry != head){
      if (entry->next != NULL)
      entry->next->prev = entry->prev;
 
      entry->prev->next = entry->next;
      if (entry == tail) tail = entry->prev;
 
      entry->next = head;
      entry->prev = NULL;
      head = entry;
    }
 
    return entry->data;
  }
 
  void print(){
    LRUCacheEntry<K, T> * entry = head;
    while (entry != NULL){
      cout << entry->data << " ";
      entry = entry->next;
    }
    cout << endl;
  }
private:
  hash_map<K, LRUCacheEntry<K, T> *> _m;
  LRUCacheEntry<K, T> * head, *tail, *entries;
  vector<LRUCacheEntry<K, T> *> freeEntries;
};
 
 
 
int main()
{
  LRUCache<int, int>  cache(3);
 
  cache.put(3, 3);
  cache.put(4, 4);
  cache.put(5, 5);
    
  cache.print();
 
  cache.get(3);
  cache.print();
  return 0;

}

- saurabh June 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Why not use only min-heap where pages with least recently used can be extracting the head of the min-heap. Heap will be formed based on the time of use. Say we use a variable clock which represents the time of use. A new page will be inserted in logn time.
I don't see any use of hash-map there.

- Aashish June 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Why minheap ? Java has a different way of implementing LinkedHashMap using a hashmap and a doubly linked list. The key to age an entry is to when its used, we add it to the head and keep removing entries from the tail (which will hold the oldest entries)

- _geek July 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about a circular linked list where the head will be the one with least recently used.

- DashDash July 10, 2012 | 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