.google и
对新手来说,碰到这题可以说是残忍的,因为答案看了都很长,强行记忆是不现实的。记住需要一个HashMap,一个doubly linked list,剩下的,就要靠基本功和理解了。而对于有了几年工作经验,使用过redis等工具的同学,这题就相当于一个最好的学习工具。因为我本人早期一直做service,刷到这题,当时我惊为天人,原来后台就是这么写的。
故事一:
面试官是同胞经理,面试人不是同胞。位子是senior。.google и
面试官:你们平时用不用cache?
面试人:yeah,we use redis. not only for caching the data, I also use it for rate limiters, blah, blah...
面试官:如果哪天, for whatever reason, 不让大家用redis了,你咋办?
面试人:(这是啥意思?)we can use other products, like memcache, it's light weight, less built-in functions, but also a popular key-value store.....
面试官:(打断)no, no third party products
面试人:(他想说啥?)then we use in memory cache classes, there are also other libraries that we can use...
-baidu 1point3acres
面试官:(打断)no, no libraries
面试人:(不会吧,不会是要我写个LRU吧,刚才不是system design interview吗)Java has built-in key value store, HashMap, that we can use and expand, we can just wrap it up and enhance it.
. check 1point3acres for more.
. 1point 3acres
面试官:不行,我们app自己的memory都不够用了。
面试人:(哦,那还是system design)I get it. In that case, we should design our own caching service, and consider future reuse. Let's draw it on the whiteboard.. 1point 3acres
面试官:(开始点头,终于上路子了)行,让我们选个简单的写写,就LRU吧
面试人:(我靠,又改成coding了,图穷匕见啊,你耍我吧,时间还来得及吗,大哥你早讲啊)no problem, since we are running short on time, can we simplify a little and just assume the key is string, and value is generic type, and skip TTL and capacity control?
面试官:ok ok.1point3acres
面试人:(这里就开始手搓代码了,先写API的框架,然后解释说现成的KV store是HashMap,但是为了sort by usage时间,及时挪动,需要加一个doubly linked list,这个一定要别写边讲,或者先停下来,讲清楚为什么用了两个data structure再开始写。如果等面试官问出这个问题,就有点晚了。)
面试官:你还有几分钟,你觉得哪里不够好的可以改进?
面试人:we still have a few mins, we can add capacity control, expiration control, both are critical part. capacity control is easy to implement. how about we design a system to clean up expired data? .1point3acres
-baidu 1point3acres
面试官:(有两下子啊,这里写capacity容易,就是LC上有的,这家伙宁愿选难点的)
面试人:(其实我题目背的不熟,但clean up batch job最近工作里刚写过)Overall, we have two strategies, proactive cleanup and reactive cleanup. Let's only design a batch job which is a proactive clean up here. blah blah.....
以上由真实故事改编,最后朋友拿到了offer。
希望同学们能注意到,LC里面的原题为了简单化,key value都是integer,但是现实生活里,不可能这样简单。所以这位面试人故意用了string做key,object做value,coding工作量一样,但是能成功地展示一下自己不是背诵的答案。如果时间充分,应该利用这个机会讨论一下怎么在create的时候,加一个parameter,让用户来定义key type。
. check 1point3acres for more.
类似的讨论点,还有get的时候,如果cache miss了,就是key not exist的情况,LC是让我们return -1,这个在现实生活中也是不合理的,(为什么不合理?同学们可以思考一下,这里面试官往往会challenge你)好的面试官看你这么写,肯定要问一下为什么写成这样,所以要主动解释一下,如果是面亚麻或买它,因为不需要compile,这里应该throw KeyNotFoundEexception。
还有许多讨论点,比如capacity应该是create的时候的一个optional parameter。用户不给,就用一个default的。caching service怎么做monitor等等。batch cleanup是不是可以在monitor发现快要out of memory了,再触发。怎么处理multi thread。. Waral dи,
好,最后讲一个朋友挂的例子:
面试官:Let's design a cache.google и
面试人:(哈?今天不是coding 面试吗?怎么改system design了)。。。。。。 . 1point 3acres .1point3acres
事后朋友说他当时大脑一片空白。天竺面试官这是刁难我吗?叫我design一个cache,这个是啥啊。我说那不就是LRU LFU吗,你不是很熟的吗?我们不是还讨论过吗。他才恍然大悟,根本没有把这道LC题和design a cache联系起来。
也曾经面过小厂,面试官往往就是把LC上选好的题目的description原封不动直接copy一下,这种就是他们自己都没有完全理解,没有能力改动题目,改了自己可能就看不懂code了。但是必须承认,大厂面试官确实让人impress,问的问题一听就是自己都里里外外明白了,全在点子上,而且往往会对题目改动一些,这样靠记忆是过不了的。所以如果我们能预判他们的预判,提前就边写边解释清楚,这里为什么这么写,那里是为了简化才这样,就能省下时间来讨论更加重要的东西了。比如LRU写完了,还有时间,就可以自己提出来,这个cache还能怎么改进, data encryption? segregation by company id? 不一定要写出来能测试,但是能提出解决方法。