Pergunta de entrevista da empresa Workday

Create a LRU cache that can hold five objects maximum at a time, and write the get, add, and remove functions for it. If you add an item to the cache with five items in it, the least accessed item should be removed, and your item should be added to the top of the cache.

Resposta da entrevista

Sigiloso

16 de dez. de 2016

class LRU extends LinkedHashMap { private final int maxCapacity; public LRU(int maxCapacity) { this.maxCapacity = maxCapacity; super(maxCapcity, 1.0, true); } protected boolean removeEldestEntry(Map.Entry eldest) { return size() > maxCapacity; } }