📣 独立日限时特惠: VIP通行证立减$68
楼主: jeh
跳转到指定楼层
上一主题 下一主题
收起左侧

刚面完的Linkedin电面

 
🔗
密码 2019-5-7 09:03:45 | 只看该作者
全局:
[quote]liqingfd 发表于 2019-5-3 11:12
刚刚写了一个版本,跟楼上grandyang链接里面的差不多,欢迎指正!
  1. import java.ut ...[/quote]
  2. [hide=200]没必要用TreeMap吧,感觉PriorityQueue应该就够了吧?[code]public class RetainBestCache<K, T extends Rankable> {
  3.         int entriesToRetain;
  4.         HashMap<K, T> map = new HashMap<K,T>();
  5.         PriorityQueue<Wrapper<K, T>> pq;
  6.         DataSource<K,T> ds;

  7.         /* Constructor with a data source (assumed to be slow) and a cache size */
  8.         public RetainBestCache(DataSource<K,T> ds, int entriesToRetain) {
  9.                 //impliment here
  10.                 this.pq = new PriorityQueue<>(new Comparator<Wrapper>() {
  11.                         public int compare(Wrapper w1, Wrapper w2) {
  12.                                 return w1.data.getRank() - w2.data.getRank();
  13.                         }
  14.                 });
  15.                 this.ds = ds;
  16.                 this.entriesToRetain = entriesToRetain;
  17.         }
  18.         /* Gets some data. If possible, retrieves it from cache to be fast. If the data is not cached,
  19.         * retrieves it from the data source. If the cache is full, attempt to cache the returned data,
  20.         * evicting the T with lowest rank among the ones that it has available
  21.         * If there is a tie, the cache may choose any T with lowest rank to evict.
  22.         */
  23.         public T get(K key) {
  24.                 //impliment here
  25.                 if (map.containsKey(key)) {
  26.                         return map.get(key);
  27.                 }
  28.                 T data = DataSource.get(key);
  29.                 if (map.size() < entriesToRetain) {
  30.                         map.put(key, data);
  31.                         pq.offer(new Wrapper(key, data));
  32.                 } else {
  33.                         evict();
  34.                         map.put(key, data);
  35.                         pq.offer(new Wrapper(key, data));
  36.                 }
  37.                 return data;
  38.         }

  39.         private evict() {
  40.                 Wrapper leastRank = pq.poll();
  41.                 map.remove(leastRank.key);
  42.         }
  43. }

  44. class Wrapper<K, T> {
  45.         T data;
  46.         K key;

  47.         public Wrapper(T data, K key) {
  48.                 this.data = data;
  49.                 this.key = key;
  50.         }
  51. }

  52. /*
  53. * For reference, here are the Rankable and DataSource interfaces.
  54. * You do not need to implement them, and should not make assumptions
  55. * about their implementations.
  56. */
  57. public interface Rankable {
  58.         /**
  59.         * Returns the Rank of this object, using some algorithm and potentially
  60.         * the internal state of the Rankable.
  61.         */
  62.         long getRank();
  63. }

  64. public interface DataSource<K, T extends Rankable> {
  65.         T get(K key);
  66. }
复制代码
[/hide]
回复

使用道具 举报

🔗
Falldawn 2021-6-11 11:13:39 | 只看该作者
全局:
本帖最后由 Falldawn 于 2021-6-11 11:17 编辑

找到了这个帖子https://www.1point3acres.com/bbs/thread-279704-1-1.html,这题应该用TreeMap因为Heap的remove操作是O(n),用一个HashMap作为cache记录<Key, Value>,用一个TreeMap来记录rank对应的Set of Value

如果multi thread会有什么问题,该如何处理?


  1. public class RetainBestCache<K, T extends Rankable> {
  2.      private Map<K, T> cacheMap;
  3.      private TreeMap<Long, Set<K>> rankMap;
  4.      private DataSource<K, T> dataSource;
  5.      private int maxSizeOfCache;

  6.     /* Constructor with a data source (assumed to be slow) and a cache size */
  7.     public RetainBestCache(DataSource<K,T> ds, int entriesToRetain) {
  8.         // Implementation here
  9.         cacheMap = new HashMap<>();
  10.         rankMap = new TreeMap<>();
  11.         dataSource = ds;
  12.         maxSizeOfCache = entriesToRetain;
  13.     }

  14.     /* Gets some data. If possible, retrieves it from cache to be fast. If the data is not cached,
  15.      * retrieves it from the data source. If the cache is full, attempt to cache the returned data,
  16.      * evicting the T with lowest rank among the ones that it has available
  17.      * If there is a tie, the cache may choose any T with lowest rank to evict.
  18.      */
  19.     public T get(K key) {        
  20.         if (cacheMap.containsKey(key)) {        
  21.           return cacheMap.get(key);
  22.         }
  23.         return fetchDataFromDs(key);
  24.     }

  25.     private T fetchDataFromDs(K key) {
  26.        if(cacheMap.size() > maxSizeOfCache) {
  27.           evictElement();
  28.         }
  29.         T curValue = dataSource.get(key);
  30.         cache.put(key, curValue);
  31.         long curRank = curValue.getRank();      
  32.         rankMap.putIfAbsent(curRank, new HashSet<>());      
  33.         rankMap.get(curRank).add(key);
  34.         return curValue;
  35.     }

  36.     private void evictElement() {
  37.       Entry<Long, Set<K>> entry = rankMap.firstEntry();//It returns an entry with the least key and null if the map is empty.
  38.       K key = entry.getValue().iterator().next();
  39.       entry.getValue().remove(key);
  40.       cacheMap.remove(key)
  41.       if(entry.getValue().size() == 0) {
  42.         rankMap.remove(entry.getKey());
  43.       }
  44.     }
  45. }

  46. // What if rank is defined as the number of reads of elements in cache?
  47. // LRU
  48. // Let's assume that rank can change dynamically. It is not immutable, but it is not LRU. We do not know how it is changed


  49. /*
  50. * For reference, here are the Rankable and DataSource interfaces.
  51. * You do not need to implement them, and should not make assumptions
  52. * about their implementations.
  53. */

  54. public interface Rankable {
  55.     /**
  56.      * Returns the Rank of this object, using some algorithm and potentially
  57.      * the internal state of the Rankable.
  58.      */
  59.     long getRank();
  60. }

  61. public interface DataSource<K, T extends Rankable> {
  62.     T get (K key);
  63. }
复制代码
回复

使用道具 举报

🔗
xhdt 2021-7-8 12:46:02 | 只看该作者
全局:
Falldawn 发表于 2021-6-11 11:13
找到了这个帖子https://www.1point3acres.com/bbs/thread-279704-1-1.html,这题应该用TreeMap因为Heap的re ...

Heap remove 的时间复杂度难道不是O(log n)
回复

使用道具 举报

🔗
Falldawn 2021-7-8 12:48:39 | 只看该作者
全局:
xhdt 发表于 2021-7-8 12:46
Heap remove 的时间复杂度难道不是O(log n)

显然不是,搜一下吧
回复

使用道具 举报

🔗
xhdt 2021-7-8 20:42:38 | 只看该作者
全局:
本帖最后由 xhdt 于 2021-7-8 21:12 编辑
Falldawn 发表于 2021-6-11 11:13
找到了这个帖子https://www.1point3acres.com/bbs/thread-279704-1-1.html,这题应该用TreeMap因为Heap的re ...

再看了下,两个MAP是需要的。
回复

使用道具 举报

全局:
Falldawn 发表于 2021-6-10 20:13
找到了这个帖子https://www.1point3acres.com/bbs/thread-279704-1-1.html,这题应该用TreeMap因为Heap的re ...

TreeMap.remove() 应该是logN吧?
PriorityQueue remove() max/min也是logN,但remove(object)是 O(N)
回复

使用道具 举报

🔗
Falldawn 2021-8-25 22:31:33 | 只看该作者
全局:
biomedicineman 发表于 2021-8-24 21:31
TreeMap.remove() 应该是logN吧?
PriorityQueue remove() max/min也是logN,但remove(object)是 O(N)

是的,因为TreeMap是平衡二叉树,而Heap只有上下级的大小关系

评分

参与人数 1大米 +5 收起 理由
biomedicineman + 5 恩恩,平时都没怎么关注TreeMap的实现

查看全部评分

回复

使用道具 举报

全局:
Falldawn 发表于 2021-8-25 07:31
是的,因为TreeMap是平衡二叉树,而Heap只有上下级的大小关系
恩恩,平时都没怎么关注TreeMap的实现
回复

使用道具 举报

🔗
brucezu 2021-9-3 03:19:13 | 只看该作者
全局:

-HashMap keep k - v relation
-TreeSet or PriorityQueue  to keep the rank list

补充内容 (2021-09-08 09:58 +8:00):
TreeSet does not work
回复

使用道具 举报

🔗
richard_515 2021-12-1 03:55:03 | 只看该作者
全局:
提问:如果不用java怎么写?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表