回复: 7
跳转到指定楼层
上一主题 下一主题
收起左侧

凌鹰电面挂经

全局:

2020(1-3月) 码农类General 硕士 全职@linkedin - Other - 技术电面  | | Fail | 在职跳槽

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

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

x
本帖最后由 cqzxlong 于 2020-1-25 02:48 编辑

面的system and infra,面完就知道挂了,昨天收到拒信,说Concurrency and Multi-Threading没答好

开头互相介绍,然后开始做题,大概有这些Process vs Thread,Context switch in thread and process,Wha
您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
使用VIP即刻解锁阅读权限或查看其他获取积分的方式
游客,您好!
本帖隐藏的内容需要积分高于 188 才可浏览
您当前积分为 0。
VIP即刻解锁阅读权限查看其他获取积分的方式
Unlock interview details and practice with AI
Curated Interview Questions from Top Companies
种情况,希望有知道的大佬提供一些想法

顺便吐槽一下,这个coordinator实在是太不专业了,周五早上面试,周四下午5点才发来confirmation,本来都以为取消了

Anyway,求大米

评分

参与人数 4大米 +13 收起 理由
randomjoe + 1 赞一个
黄瓜太郎 + 2 很有用的信息!
convexopt + 1 欢迎分享你知道的情况,会给更多积分奖励!
匿名用户-GAOGY + 9

查看全部评分


上一篇:Whova phone screening
下一篇:微软昂赛 面经
推荐
convexopt 2020-1-29 13:18:45 | 只看该作者
全局:
谢谢分享。关于followup 2我提一点想法抛砖引玉:
1 首先我觉得访问DataSource(在实际情况里就是访问数据库)的主要问题不是慢而是并发的压力。
    比如有一个key的读请求很多,那么这个key一旦被更新,缓存被失效,如果此时所有的缓存get请求
    都变成一个数据库的读请求,会对数据库造成过大的压力(所谓缓存雪崩)。

2 不知道楼主是怎么回答锁的问题。这类缓存一般会对锁shard,比如用hash值mod 64,减少竞争。

3 解决这个问题我有两个思路
   i.  当get请求发现key不存在或失效时,本线程先拿(sharded)锁,然后执行从DataSource载入数据
       的操作。这样即使有大量线程get同一个key,也只产生了一个数据库请求。数据库请求完成后释放这个锁,
       然后其他线程被unblock,就可以从缓存里读数据了。如果线设计成异步的接口,比如c++可以返回一个
       std::shared_future,由读数据库的线程来set。

   ii. 另一个常见的技巧是租约,这样不但可以协调同一个进程的get请求,还能用于不同进程乃至机器。做法是
       读数据库给缓存更新的时候需要请求一个token,同一时间只能有一个token。拿不到token的话,还可以
       考虑返回一个stale值,由应用决定接不接受。

评分

参与人数 2大米 +4 收起 理由
LordSavesU + 3 给你点个赞!
729654213 + 1 赞一个

查看全部评分

回复

使用道具 举报

推荐
DeniLi 2020-2-12 14:03:57 | 只看该作者
全局:
  1. package easy;

  2. import java.util.*;
  3. import java.util.concurrent.Semaphore;
  4. import java.util.concurrent.locks.Lock;
  5. import java.util.concurrent.locks.ReentrantLock;

  6. public class RetainBestCache<K, T extends Rankable> {
  7.     int entriesToRetain;
  8.     private Map<K, T> cache;
  9.     private TreeMap<Long, Set<K>> rankingKeySetMap;
  10.     Semaphore semaphore;
  11.     DataSource<K,T> ds;

  12.     /* Constructor with a data source (assumed to be slow) and a cache size */
  13.     public RetainBestCache(DataSource<K,T> ds, int entriesToRetain) {
  14.         cache = new HashMap<>();
  15.         rankingKeySetMap = new TreeMap<>();
  16.         this.ds = ds;
  17.         this.entriesToRetain = entriesToRetain;
  18.         semaphore = new Semaphore(entriesToRetain);
  19.     }
  20.     /* Gets some data. If possible, retrieves it from cache to be fast. If the data is not cached,
  21.      * retrieves it from the data source. If the cache is full, attempt to cache the returned data,
  22.      * evicting the T with lowest rank among the ones that it has available
  23.      * If there is a tie, the cache may choose any T with lowest rank to evict.
  24.      */
  25.     public T get(K key) {
  26.         //implement here
  27.         if(cache.containsKey(key)){
  28.             return cache.get(key);
  29.         }
  30.         return fetchFromDS(key);
  31.     }

  32.     private T fetchFromDS(K key){
  33.         if(cache.size() >= entriesToRetain){
  34.             evitLowestRank();
  35.         }
  36.         T object = ds.get(key);
  37.         try{
  38.             semaphore.acquire();
  39.             cache.put(key, object);
  40.             long score = object.getRank();
  41.             if(!rankingKeySetMap.containsKey(score)){
  42.                 rankingKeySetMap.put(score, new HashSet<>());
  43.             }
  44.             rankingKeySetMap.get(score).add(key);
  45.         }
  46.         catch (InterruptedException e){

  47.         }
  48.         finally {
  49.             semaphore.release();
  50.         }
  51.         return object;
  52.     }

  53.     private void evitLowestRank(){
  54.         Map.Entry<Long, Set<K>> entry = rankingKeySetMap.firstEntry();
  55.         K key = entry.getValue().iterator().next();
  56.         entry.getValue().remove(key);
  57.         cache.remove(key);
  58.         if(entry.getValue().size() == 0){
  59.             rankingKeySetMap.remove(entry.getKey());
  60.         }
  61.     }
  62. }
  63.     /*
  64.      * For reference, here are the Rankable and DataSource interfaces.
  65.      * You do not need to implement them, and should not make assumptions
  66.      * about their implementations.
  67.      */
  68.     public interface Rankable {
  69.         /**
  70.          * Returns the Rank of this object, using some algorithm and potentially
  71.          * the internal state of the Rankable.
  72.          */
  73.         long getRank();
  74.     }
  75.     public interface DataSource<K, T extends Rankable> {
  76.         T get(K key);
  77.     }
复制代码
回复

使用道具 举报

全局:
从DataSource拿值会不会是应该加一个读写锁, 因为如果大量的请求是读的话 可以使用读写锁 这样可以做到大量的读请求可以同时获得锁而不是被block 只有当写操作发生的时候才会block住 如果还是太慢可以lock by bucket 这样写的时候也不是block所有 不知道面试官是不是这个意思
回复

使用道具 举报

🔗
linda90321 2020-1-29 03:53:29 | 只看该作者
全局:
同面试前一天下午三四点才收到确认
回复

使用道具 举报

🔗
40fs 2020-1-29 05:15:50 来自APP | 只看该作者
全局:
看来做backeend/infra 并发很重要。
回复

使用道具 举报

🔗
DeniLi 2020-2-12 14:04:38 | 只看该作者
全局:
我的想法是用Semaphore 优化
回复

使用道具 举报

地里匿名用户
🔗
匿名用户-HVJ82  2021-9-21 14:45:20
用semaphore 不行,最好还是lock,因为semaphore的话比如你有先把cache塞满了然后又开了很多个thread进去的话就有问题了。我觉得还是一个一个thread 跑最好,然后我感觉第二个follow up的意思就是要你在synchronize lock 之前把value 从ds里面拿出来而已, 这样的话你就可以concurrently 从ds里面拿东西while nonblocking,不知道说的对不对
回复

使用道具 举报

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

本版积分规则

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