Amazon Interview Question for Software Engineer / Developers






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

Maintain two data structures. A linked list and a hash table. The hash table should contain the (key, pointer), where pointer is a pointer to the element in the linked list. LRU evicts the least recently used element from the cache. On removing an element, remove from both the linked list and the hash table. On requesting an element, move it to head of the linked list.

This solution is O(1) time complexity. Space complexity is O(n)

- Shar3oob May 25, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi Shar3oob,

In the previous solution where are you maintaining the LRU element, every time you access an element you'll have to update the least recently used element which will be a little more time consuming than O(1).

Also I dont understand what is the use of maintaining a list, I suppose cache will be of a constant size so we can just maintain an array and store the index of the array in the hash table instead of storing a pointer(which will take more space)

Thanks,
Loony

- loony June 28, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

in Array i think the problem will be in updation . Since in LRU u move the most recenlty used item to the TOP then in array u have to shift all the elements 1 by 1 . Which I think is more complex then in LinkLists as it might take worst case O(n) .
and a hash is mantained so as to quickly index what is the current location of the item . rather than traversing the LinkList..
Hope this helps

- Manni mbd February 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

good solution. Thanks.

- jiangok2006 May 13, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Time complexity can be O(1) if it's circular linked list.

- ravishchawla September 21, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Skip list is another possible solution

- Some one March 17, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Opps............ sorry ..........Splay Tree

- Some one March 17, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

How? I think they are good for a Least Frequently Used cache but for LRU is not the case...

- Zetinator November 02, 2020 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class LRUCache<K,V>{
	private MyLinkedHashMap<K,V> cache;
	private final int cache_size;
	public LRUCache(int size){
		cache_size = size;
		this.cache = new MyLinkedHashMap<K,V>(){
			protected boolean removeEldestEntry(MyLinkedHashMap.Entry<K,V> eldest){
				//System.out.println("remove Eldest entry: " + (cache_size >= size()));
				return cache_size < size();
			}
		};
	}

	public V get(K key){
		return cache.get(key);
	}
	public void put(K key, V value){
		cache.put(key,value);
	}
	public boolean  containsKey(K key){
		return cache.containsKey(key);
	}
	public void print(){
		cache.print();
	}
	public void printTable(){
		this.cache.printTable();
	}
}

- .·´¯`·.´¯`·.¸¸.·´¯`·.¸><(((º> March 12, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

stack can also be a good option

- Anonymous February 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.


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