System Design
文章平均质量分 58
纸上得来终觉浅 绝知此事要躬行
github.com/luqian2017
展开
-
系统设计个人小结
系统设计个人小结原创 2023-11-28 06:42:06 · 532 阅读 · 0 评论 -
秒杀系统中乐观锁(Optimistic Lock)和悲观锁(Pessimistic Lock)的对比
参考九章的讲座。悲观锁:用户先判断是否拿到锁,如果没有拿到,则排队等待。若可以拿到,则减库存,然后释放锁,这时排队等待的线程有一个可以得到锁。SQL实现: 注意"for update"表示执行完step 1语句后,把product_id = xxxxx 锁住,其它线程则不能对product_id = xxxxx操作。//step 1:select * from stock where product_id = xxxxx for udpate//step 2:udpate stock原创 2020-09-21 01:24:05 · 3034 阅读 · 2 评论 -
LintCode 554: Sort Integers (Map Reduce)
Sort Integers (Map Reduce)Sort integers by Map Reduce framework.In the mapper, key is the document id which can be ignored, value is the integers.In the reducer, your can specify what the key / v...原创 2020-01-02 15:46:50 · 363 阅读 · 0 评论 -
LintCode-555: Counting Bloom Filter (System Design题)
这题跟Standard Bloom Filter很像,只是不能用bitset了,因为每个pos都要记counting:add()对各个pos的counting加1, remove()对各个pos的 counting–。contain()检查每个pos的counting是不是>0。#include <bitset>class HashClass{public: H...原创 2018-08-26 09:01:39 · 434 阅读 · 0 评论 -
LintCode-566: GFS Client (System Design题)
这题就是模拟一个大文件的分块和组装。 有一个地方要注意: 当fileSize < chunkSize时,numOfChunk仍然要设为1。 可以用以下技巧: int numOfChunks = (content.size() + chunkSize - 1) / chunkSize; 也可以用 int numOfChunks = (conten...原创 2018-08-19 09:50:35 · 340 阅读 · 0 评论 -
LintCode 565: Heart Beat (System Design题)
这题其实是考察map的用法。class HeartBeat {public: HeartBeat() { // do intialization if necessary } /* * @param slaves_ip_list: a list of slaves'ip addresses * @param k: An inte...原创 2018-08-19 14:43:53 · 268 阅读 · 0 评论 -
LintCode-527: Trie Serialization (System Design重要题!)
serialize() 用DFS深搜。 deserialize() 有点像根据Trie树的前序遍历和><符号来重构Trie。/** * Definition of TrieNode: * class TrieNode { * public: * TrieNode() {} * map<char, TrieNode*> children; *...原创 2018-08-17 14:46:17 · 423 阅读 · 0 评论 -
LintCode-505: Web Logger (System Design题)
数据结构采用deque,参考了网上的答案。 注意 1) get_hit_count_in_last_5_minute()里面必须用<=。 2) 同一个timestamp可能hit多次,所以都必须加起来。 3)deque.front()是最老的entry,所以将其与当前timestamp比较,若不在当前5分钟内,则其count不算,再pop_front(),继续与当前timestamp...原创 2018-08-27 15:18:49 · 324 阅读 · 0 评论 -
LintCode-549: Top K Frequent Words (Map Reduce) (System Design题)
我用的C++ stl::priority_queue来作为一个max heap来保存前K个最频繁的词。 注意 1) 这里heap的维护是在Reducer端。 2) priority_queue需要自定义operator < (因为object是自定义的)。如果里面是this->count < pair2.count就是最大堆(跟int类型默认一样)。如果是this->...原创 2018-08-23 14:40:55 · 575 阅读 · 0 评论 -
LintCode-504: Inverted Index (Map Reduce) (System Design题)
这题要注意: 1) Map()的output输出input->value.id,而不是1。因为这里是要统计每个单词对应文档的id,不是统计单词个数。 2) Reduce()不需要专门维护一个map/** * Definition of Input: * template<class T> * class Input { * public: * bool ...原创 2018-08-23 16:01:20 · 234 阅读 · 0 评论 -
LintCode 503: Anagram (Map Reduce) (System Design 题)
注意std::sort()可以对string排序。我们可以把一个word排序后的新word作为key。/** * Definition of Input: * template<class T> * class Input { * public: * bool done(); * // Returns true if the iteration...原创 2018-08-24 13:37:33 · 325 阅读 · 0 评论 -
LintCode-499: Word Count (Map Reduce) (System Design题)
这题其实就是简单模拟字符串分块统计个数和归并的过程。 我用的stringstream来split字符串。/** * Definition of Input: * template<class T> * class Input { * public: * bool done(); * // Returns true if the iterati...原创 2018-08-21 15:52:51 · 453 阅读 · 0 评论 -
LintCode 215: Rate Limiter (System Design 题)
主要数据结构采用mapclass Solution {public: /* * @param timestamp: the current timestamp * @param event: the string to distinct different event * @param rate: the format is [integer]/[s/m...原创 2018-08-29 23:20:21 · 644 阅读 · 0 评论 -
LintCode 24: LFU Cache(系统设计经典题)
LFU Cache中文EnglishLFU (Least Frequently Used) is a famous cache eviction algorithm.For a cache with capacity k, if the cache is full and need to evict a key in it, the key with the lease frequentl...原创 2019-07-16 14:52:06 · 350 阅读 · 0 评论 -
LintCode 556: Standard Bloom Filter (System Design题)
参考的网上的答案。 注意: 1) Bloom Filter的add()的主要思想是用多个hash方程来将输入map到bit串中的若干位,并对这些位都设1。contains()的主要思想是用多个hash方程将输入map到bit串中的若干位,若这些位都为1,则返回true;有一个或多位为0,则返回false。 2) hash方程的cap和seed可以随意。seed越大则map得越均匀。 3) ...原创 2018-08-26 07:41:49 · 493 阅读 · 0 评论 -
LintCode-231: Typeahead (System Design题)
此题考察对map的应用。 map &lt; string, vector &gt; wordMap; 记录每个subWord对应的dict中的名字。 vector words; 把dict中的名字按顺序记下来,方便search()返回。注意去重。有两种去重的情况。 一个是dict里面单个名字出现两个同样的字母或同样的单词时,比如说tree,或”Yang Yang”。这种情况通过 i...原创 2018-08-19 02:38:10 · 337 阅读 · 0 评论 -
LintCode-522: Tiny Url II (System Design 经典题)
这道题是System Design超经典题,但是并不简单。 首先是不能指望把一个long_url hash成一个6位字符串,因为6位字符串太短,所以会有大量重复。 解法是把short_url map成一个id (基于62),这样short_url和id是严格的一一对应了,可以互相编解码出来。那long_url又怎么和short_url或id来对应呢?答案是用系统生成的唯一的一个counter来...原创 2018-07-29 13:34:10 · 1021 阅读 · 0 评论 -
LintCode-519: Consistent Hashing
这是一道system design的题,但我觉得更像算法题。 参考 的网上的答案。 注意事项: 1)bit operation &lt;&lt;或&gt;&gt;优先级非常低! 这题可能有更好的基于数学的解法,这样O(1)就可以生成。下次再思考。class Solution {public: /* * @param n: a positive integer ...原创 2018-07-26 14:30:25 · 323 阅读 · 0 评论 -
LintCode-501: Design-Twitter (经典系统设计题)
第一次做系统题,不知道思路。直接参考答案。做完收获也不少。 最重要的一点是在设计getTen()时,一定要从order逆序找出order最大的那10条,这样才能说是最近的10条tweet。/** * Definition of Tweet: * class Tweet { * public: * int id; * int user_id; * Strin...原创 2018-07-21 14:53:46 · 1116 阅读 · 0 评论 -
LintCode-538: Memcache (System Design题)
这道题目主要考验hashmap的应用。记得node.timeout = curtTime + ttl - 1; //这里要-1.class Memcache {public: class Node { public: Node(int v = 0, int t = 0) : value(v), timeout(t) {} ...原创 2018-07-27 14:56:55 · 316 阅读 · 0 评论 -
LintCode-525: Mini Uber (System Design 经典题)
这题主要考map的应用。/** * Definition of Trip: * class Trip { * public: * int id; // trip's id, primary key * int driver_id, rider_id; // foreign key * double lat, lng; // pick up location...原创 2018-08-09 13:48:08 · 890 阅读 · 0 评论 -
LintCode-530: Geohash II (System Design 经典题)
用一个string base32Map来表示具体编码就可以了。 比如说11100 -&gt; W, 11101 -&gt;X。 string.find(‘W’)会返回28, string.find(‘X’)会返回29。 另外要注意isLongTurn的翻转是全局有效,而不是仅对某个5-digit的字母有效。 class GeoHash {public: /* * @p...原创 2018-08-05 08:40:08 · 407 阅读 · 0 评论 -
LintCode-520. Consistent Hashing II (System Design经典题)
这题要注意的是用一个mapclass Solution {public: int n, k; map&amp;amp;lt;int, int&amp;amp;gt; shards; //stores id&amp;amp;lt;-&amp;amp;gt;machine mapping, multiple ids to one machine set&amp;amp;lt;int&原创 2018-08-01 13:10:01 · 337 阅读 · 0 评论 -
LintCode-529: Geohash (System Design题)
这题就是Geohash II的逆运算。 需要注意的是 1) totalCode必须用string, 不能long long,否则肯定溢出。class GeoHash {public: /* * @param latitude: one of a location coordinate pair * @param longitude: one of a loc...原创 2018-08-06 15:43:28 · 469 阅读 · 0 评论 -
LintCode-559: Trie Service (System Design题)
这个Trie的实现好像跟那种经典的每个节点分出26个分叉的Trie不太一样。 insert()的代码很重要。下次再学习。/** * Definition of TrieNode: * class TrieNode { * public: * TrieNode() {} * map<char, TrieNode*> children; * v...原创 2018-08-14 16:39:07 · 241 阅读 · 0 评论 -
LintCode-232: Tiny Url (System Design 经典题)
这题也是System Design经典题,与Tiny Url II的区别是它要求没有那么严格,不需要生成全局唯一的id。 这两种方案区别如下:Tiny Url: long_url &lt;-(256Base mapping)-&gt; id &lt;-(62Base mapping)-&gt; short_keyTiny Url II: long_url &lt;-...原创 2018-08-02 15:20:14 · 1320 阅读 · 0 评论 -
LintCode-509: Mini Yelp (经典System Design题)
这题要注意的是要用两个map,一个存储hashcode vs restaurant, 一个存储id vs hashcode。 注意这里hashcode 不能直接用geohash,因为同一个坐标可能有好几家餐馆。可以用geohash+id 或 geohash+name。/** * Definition of Location: * class Location { * public: ...原创 2018-08-12 05:44:29 · 960 阅读 · 0 评论 -
LintCode-500: Inverted Index (System Design题)
这题其实是考的字符串的tokenizer,我的方法比较繁琐。看到九章的解法更简洁。/** * Definition of Document: * class Document { * public: * int id; * string content; * } */class Solution {public: /** * @param d...原创 2018-08-12 08:14:34 · 417 阅读 · 0 评论 -
LintCode-442: Implement Trie Prefix Tree (System Design题)
经典的Trie设计题。注意因为都是小写字母,所以节点上不需要map,直接开一个26叉的vector指针数组即可。class Node {public: Node() : isWord(false) { children = vector&lt;Node *&gt;(26, nullptr); } bool isWord; vector&lt;...原创 2018-08-15 13:26:11 · 248 阅读 · 0 评论 -
LintCode-502: Mini Cassandra (System Design题)
这题注意query返回的vector必须是按column_key排好序的,所以最好用map/** * Definition of Column: * class Column { * public: * int key; * String value; * Column(int key, string value) { * this-&amp;gt...原创 2018-07-28 14:33:50 · 305 阅读 · 0 评论