Domanda di colloquio di Microsoft

Implement a LRU cache.

Risposte di colloquio

Anonimo

19 nov 2016

I talked about what an LRU cache is with my interviewer. Then I talked about different data structures we could use for our implementation. I settled on a queue. On the whiteboard I began by defining all of the classes I would need. Then I added functions for cache operations to the respective classes. This is the type of question that checks how well you can architect and organize code. We went over many edge cases and then covered a series of input examples to demonstrate the functionality of the LRU cache.

1

Anonimo

6 ago 2018

static Map map = new LinkedHashMap(); static void consume(int res){ if(map.size() list = new LinkedList(); for(Integer i : map.keySet()){ list.add(i); } Map tmp = new LinkedHashMap(); for(int i =1; i <=2; i++){ tmp.put(list.get(i),map.get(list.get(i))); } map = tmp ; map.put(res,1); } } }

Anonimo

6 ago 2018

static Map map = new LinkedHashMap(); static void consume(int res){ if(map.size() list = new LinkedList(); for(Integer i : map.keySet()){ list.add(i); } Map tmp = new LinkedHashMap(); for(int i =1; i <=2; i++){ tmp.put(list.get(i),map.get(list.get(i))); } map = tmp ; map.put(res,1); } } }