System design Questions

** What are good ways to answer interview design questions? **
** Cited from Quora **

https://www.quora.com/What-are-good-ways-to-answer-interview-design-questions


There are 2 sorts of design interviews:

  1. System design. Eg: Design a twitter like system, which should let users post some sentences and these then be published to other users.
  2. Class design. Eg: Design how you'd represent a Football tournament.

Let's see how you should walk through each of these types of questions:

-- It's clear the system supports "users". Clarify what else the system should let a user do - can he find friends / follow people / etc. What are some limits on how quickly a post by one user appears in someone else's feed?
-- Come up with high level components for this system.
--There needs to be a data store (DB or key-value store) that stores user data, people he follows etc. Each of these relationships might be in the same table or be broken into multiple tables. The relationship might have attributes and those need to be put in somewhere too. Basically I'm looking for some db design skills here and need the interviewee to come up with a good table design.
--There needs to be a front-end for the user to view/login etc. and a backend that talks to this data store (or it could be a 2 level system with the front end directly talking to the datastore). I expect the candidate to talk about the pros/cons of the two approaches --What is a good mechanism to send posts from a user to others? A queue? Or can it be modeled using the same datastore? Is it advantageous to use a queue so that it can be extended to publish to third party people at some time in the future?
-- Is a cache needed? What would it store, what is its eviction policy etc.
-- What about security of this system. I'd expect the candidate to atleast come up with some validation at the entry points of each of these components. -- And so on. Notice how the discussion revolves around a very high level design of different components and usually involves discussing data storage and scalability.

For 2, here's what I'd expect:
-- Clarify how the tournament is going to proceed. Find out the major "entities" that you need - each of these will mostly end up being a class of its own. It will typically have league matches, quarterfinals, semis leading upto the finals. There were will be different types of participants - teams, players, coaches, referees, cheerleaders, presenters and so on. Matches will have venues, competing teams, a referee, result, statistics, list of players with red cards etc. and so on.
-- You'll end up with a bunch of classes with some relationship between them (base and child classes or composite ones - isA or hasA relationship)
-- Now that you have a set of classes, start with the major ones and figure out what methods /operations make sense of each of them and write down a method signature. The interviewer will mostly restrict the discussion to a few major classes and just the major methods on each of these classes. - Eg: Tournament.createSchedule(), Tournament.getStatsForMatch(), Match.calculateStats() etc.-- Next, think about the best data structures to be used inside each of these classes so that the methods can work efficiently. Notice that here, we discuss about OO concepts, polymorphism, implementation details inside a class etc. There's more focus on extensibility of a class and correct representation of concepts, rather than a focus on scale.Hope this helps!


As mentioned earlier in one of the posts that a design question can be of either a high-level system design for e.g. design a chat application, or the low-level classes and methods.

For a high level system design some things that should be handled well are Speed, Reusability, Scalability, Availability and Security.

Reusability comes from the software architecture, design patterns, classes and data structures. How modular your class methods are ? Usage of proper abstraction (abstract classes and interfaces) ? Correct visibility of classes and methods (private, public, protected wherever necessary) ? Handled class and package dependencies properly ? Any framework used MVC etc. ? and so on...

Scalability comes from handling of large amount of data and handling large number of requests effectively. Multiple web servers serving requests ? How is database designed and managed ? Proper indexing of database tables ? Caching of requests handled properly or technologies used for caching ? Master-slave configuration ? Multiple nodes ? Usage of NoSQL to avoid joining tables ? Hadoop or MapReduce design patterns for models ? Knowledge of what is persistent data and what should be kept in memory ? How to read persistent data effectively ? and so on...

Speed comes with improving application performance, views loading faster. Lowering of server latency using Memcache, Redis etc. ? UI components handling for web page requests (separate css, js files, CDN serving images, lazy loading of images) ? Avoiding costly database queries and using Session variables and cookies wherever necessary ? Caching of web pages on browsers ? and so on...

Availability comes from how fast you can recover from failures. Multiple servers ? Backup databases ? Round robin server request handling ? Proper logging mechanisms ? Efficient log file reading ? Log rotation ? Proper "downtime" messages for users and so on ...

Security comes with strong authentication and encryption maintained. HTTPS ? Prevention of XSS and MySQL injection attacks ? Two step verification of user input data ? Proper cleansing of user input data (is it what is only required) ? Captcha ? Securing api's using HMAC and nonces ? Strong passwords only ? Proper access rules for files defined in .htaccess ? Proper permissions of the code base ? and so on..

Now for the design of classes and methods, one should properly identify the entities in the problem. Define the entities in their appropriate classes. Create abstract classes and interfaces wherever the entity is an abstraction and can be reusable by overriding. Classes used as data structures should be efficient i.e. possibly lowest runtime complexity for methods. Usage of polymorphic methods to handle flexible requests. Encapsulation of algorithms in separate methods and classes. Define a method such that it does only what it is supposed to do. Handling thread safety. Where static classes and methods were used and why ? Final and Immutability for required variables. What design patterns used if any ? (e.g. Factory pattern) and so on..


In a nutshell, I'd suggest people first make the problem more well-defined. Twitter is a huge product and we should clarify the scope of discussion. For instance, we can set the goal to design only core features of Twitter - follow relationship and browse feed.

Secondly, high-level idea is preferable. Don't try to dive into all those details. Instead, try to provide high-level ideas about how to solve the problem. You may explain how to model the user relationship and how to store/serve feeds.

Lastly, we can discuss in depth about specific issue. While discussing, interviewers might be interested in particular detail and the discussion will go deeper to that topic. I covered few examples in the post as well.

It's worth to note that for system design interview, specific techniques like whether to use EC2 or Heroku, whether to use AngularJS or ReactJS, which backend framework to choose etc. are not important at all. What really matters is the more general and basic ideas about how to design the whole system.


Key concept

Thread-safe (with respect to a piece of code)

A piece of code is thread-safe if it only manipulates shared data structures in a manner that guarantees safe execution by multiple threads at the same time. There are various strategies for making thread-safe data structures.[1]
[2]

A program may execute code in several threads simultaneously in a shared address space where each of those threads has access to virtually all of the memory of every other thread. Thread safety is a property that allows code to run in multi-threaded environments by re-establishing some of the correspondences between the actual flow of control and the text of the program, by means of synchronization.

Implementation approaches(wiki)

  • The first class of approaches focuses on avoiding shared state, and includes
  • The second class of approaches are synchronization-related, and are used in situations where shared state cannot be avoided

synchronization

In computer science, synchronization refers to one of two distinct but related concepts: synchronization of processes, and synchronization of data.Process synchronization refers to the idea that multiple processes are to join up or handshake at a certain point, in order to reach an agreement or commit to a certain sequence of action. Data synchronization refers to the idea of keeping multiple copies of a dataset in coherence with one another, or to maintain data integrity. Process synchronization primitives are commonly used to implement data synchronization.

Thread synchronization is defined as a mechanism which ensures that two or more concurrent processes orthreads do not simultaneously execute some particular program segment known as critical section. When one thread starts executing the critical section (serialized segment of the program) the other thread should wait until the first thread finishes. If proper synchronization techniques[1]
are not applied, it may cause a race condition where, the values of variables may be unpredictable and vary depending on the timings of context switches of the processes or threads.


v

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,839评论 6 482
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,543评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 153,116评论 0 344
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,371评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,384评论 5 374
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,111评论 1 285
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,416评论 3 400
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,053评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,558评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,007评论 2 325
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,117评论 1 334
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,756评论 4 324
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,324评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,315评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,539评论 1 262
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,578评论 2 355
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,877评论 2 345

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,399评论 0 23
  • 付出太多回报没收到一个却收到一系列的背叛有时候精神可以支撑,有时候真的支撑不了。连陪我说话谈心事的人,却一无所有
    只是烦恼太多阅读 161评论 0 0
  • 文/七蒙主 随着大学录取率的升高,考上大学的梦想,确实比以前更容易实现了。虽然还有很多人对目前的教育和高考颇有微词...
    七蒙主阅读 504评论 5 17
  • 塔普了我明天下午我的好的嗯(⊙_⊙)嗯嗯嗯嗯那你就是在集美我明天是的是在上班我我明天是的嗯(⊙_⊙)(⊙_⊙)(⊙...
    Jupiter_7280阅读 137评论 0 1
  • 怎么说 就我个人而言 内心实在算不是一个多么积极阳光的人 丧是生活的主旋律 准确地讲 悲观是我面对生活的方式之一 ...
    Shalynnn阅读 139评论 0 0