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

刚面完的Linkedin电面

 
全局:

2019(1-3月) 码农类General 硕士 全职@linkedin - 网上海投 - 技术电面  | | Other | 在职跳槽

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
电面介绍自己之后是两道题,一个是leetcode上 Nested list weight sum 那道,我做出来了。

但是第二道是一道cache设计题
您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
现它不是, 这是题目是这样的, 网上有答案
  1. public class RetainBestCache<K, T extends Rankable> {
  2. int entriesToRetain;
  3. HashMap<K, T> map = new HashMap<K,T>();
  4. DataSource<K,T> ds;

  5. /* Constructor with a data source (assumed to be slow) and a cache size */
  6. public RetainBestCache(DataSource<K,T> ds, int entriesToRetain) {
  7. //impliment here
  8. }
  9. /* Gets some data. If possible, retrieves it from cache to be fast. If the data is not cached,
  10. * retrieves it from the data source. If the cache is full, attempt to cache the returned data,
  11. * evicting the T with lowest rank among the ones that it has available
  12. * If there is a tie, the cache may choose any T with lowest rank to evict.
  13. */
  14. public T get(K key) {
  15. //impliment here
  16. }
  17. /*
  18. * For reference, here are the Rankable and DataSource interfaces.
  19. * You do not need to implement them, and should not make assumptions
  20. * about their implementations.
  21. */
  22. public interface Rankable {
  23. /**
  24. * Returns the Rank of this object, using some algorithm and potentially
  25. * the internal state of the Rankable.
  26. */
  27. long getRank();
  28. }
  29. public interface DataSource<K, T extends Rankable> {
  30. T get(K key);
  31. }
复制代码




评分

参与人数 9大米 +46 收起 理由
吃撑的烤串儿 + 1 很有用的信息!
Jessiey + 1 给你点个赞!
专业抛光核弹头 + 1 给你点个赞!
mchzh + 3 给你点个赞!
tinalxj12 + 1 THX

查看全部评分


上一篇:Zillow万年不变OA
下一篇:[面试经验] 2.21 谷歌OA New Grad
推荐
密码 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]
回复

使用道具 举报

推荐
Miaaaaa 2022-4-20 06:19:37 | 只看该作者
全局:
biomedicineman 发表于 2021-8-24 21:31
TreeMap.remove() 应该是logN吧?
PriorityQueue remove() max/min也是logN,但remove(object)是 O(N)

但是这题priorityqueue只需要remove top,不需要remove specific object,所以用pq也是o(lgN)吧,而且实现上更简单?
回复

使用道具 举报

全局:
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)
回复

使用道具 举报

全局:
请问楼主怎么做的
Cache hit o(1) , cache miss tree map ologn 的方案面试官接受吗
回复

使用道具 举报

🔗
 楼主| jeh 2019-2-22 10:06:19 | 只看该作者
全局:
Wyf2222 发表于 2019-2-22 09:51
请问楼主怎么做的
Cache hit o(1) , cache miss tree map ologn 的方案面试官接受吗

我没有做出来来,还没说到那个程度, 我只找到这个网上的答案: https://www.cnblogs.com/apanda009/p/7945036.html
回复

使用道具 举报

🔗
 楼主| jeh 2019-2-22 11:17:19 | 只看该作者
全局:
Wyf2222 发表于 2019-2-22 09:51
请问楼主怎么做的
Cache hit o(1) , cache miss tree map ologn 的方案面试官接受吗

没有出来   在网上找到这个答案: https://www.cnblogs.com/apanda009/p/7945036.html
回复

使用道具 举报

全局:
请问楼主是LRU 或者 LFU吗?
回复

使用道具 举报

🔗
 楼主| jeh 2019-2-25 10:19:46 | 只看该作者
全局:
gui59106375 发表于 2019-2-22 11:44
请问楼主是LRU 或者 LFU吗?

不是的,我上面的链接给了网上的解法
回复

使用道具 举报

🔗
Ellachen 2019-3-1 12:18:12 | 只看该作者
全局:
用PriorityQueue也可以
回复

使用道具 举报

全局:
谢谢楼主cache的分享
回复

使用道具 举报

🔗
huntersjm 2019-4-1 11:50:56 | 只看该作者
全局:
cache 这题很有意思,用最小堆实现吗?超过size把堆顶remove掉,把新的add进去
回复

使用道具 举报

🔗
liqingfd 2019-5-3 11:12:25 | 只看该作者
全局:
刚刚写了一个版本,跟楼上grandyang链接里面的差不多,欢迎指正!
  1. import java.util.*;

  2. public class RetainBestCache<K, T extends RetainBestCache.Rankable> {
  3.     int entriesToRetain;
  4.     Map<K, T> keyValueMap; // map<key, val>
  5.     Map<Long, List<K>> rankKeyMap; // map<rank, list<key>>
  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.         this.keyValueMap = new HashMap<>();
  10.         this.rankKeyMap = new TreeMap<>();
  11.         this.ds = ds;
  12.         this.entriesToRetain = 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 (keyValueMap.containsKey(key)) {
  21.             return keyValueMap.get(key);
  22.         }
  23.         T val = ds.get(key);
  24.         keyValueMap.put(key, val);
  25.         rankKeyMap.put(val.getRank(), new ArrayList<>());
  26.         rankKeyMap.get(val.getRank()).add(key);
  27.         while (keyValueMap.size() > entriesToRetain) {
  28.             Long lowestRank = rankKeyMap.keySet().iterator().next();
  29.             K k = rankKeyMap.get(lowestRank).get(0);
  30.             rankKeyMap.get(lowestRank).remove(0);
  31.             if (rankKeyMap.get(lowestRank).size() == 0) {
  32.                 rankKeyMap.remove(lowestRank);
  33.             }
  34.             keyValueMap.remove(k);
  35.         }
  36.         return val;
  37.     }

  38.     /*
  39.      * For reference, here are the Rankable and DataSource interfaces.
  40.      * You do not need to implement them, and should not make assumptions
  41.      * about their implementations.
  42.      */
  43.     public interface Rankable {
  44.         /**
  45.          * Returns the Rank of this object, using some algorithm and potentially
  46.          * the internal state of the Rankable.
  47.          */
  48.         long getRank();
  49.     }

  50.     public interface DataSource<K, T extends Rankable> {
  51.         T get(K key);
  52.     }
  53. }
复制代码

补充内容 (2019-5-5 06:04):
line 28写错了,应该先判断一下map里面有没有这个key再put的。
回复

使用道具 举报

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

本版积分规则

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