commit 2182a70770d6df87b6bd36de3fe043142955b0ce Author: Tay Yang Shun Date: Wed Sep 20 15:27:28 2017 +0800 Squash commit diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 00000000..be3654ef --- /dev/null +++ b/.editorconfig @@ -0,0 +1,11 @@ +root = true + +[*] +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true + +[*.{js,py}] +charset = utf-8 +indent_style = space +indent_size = 4 diff --git a/README.md b/README.md new file mode 100644 index 00000000..b922299f --- /dev/null +++ b/README.md @@ -0,0 +1,32 @@ +# Tech Interview Handbook + +Handbook to help you ace your next technical interview, with a focus on algorithms and the front end domain. System design questions are in-progress. + +This handbook is pretty new and help from you in contributing content would be very much appreciated! + +## Motivations + +While there are many awesome books like [Cracking the Coding Interview](http://www.crackingthecodinginterview.com/) and interview-related repositories on Github that contain a lot of algorithm questions, for more domain-specific and non-software engineering questions, there seems to be a lack in resources. This handbook aims to cover questions beyond the algorithm coding questions. + +## Contents + +- **[Algorithms Questions](algorithms)** + - Questions categorized by topic. +- **[Design Questions](design)** +- **[Domain-specific Questions](domain)** +- **[Front End Study Notes](front-end)** + - Summarized notes on the various aspects of front end. + - [Front End Job Interview Questions and Answers](front-end/interview-questions.md) ⭐ +- **[Non-Technical Tips](non-technical)** + - Random non-technical tips that cover behavioral and psychological aspects, interview formats and "Do you have any questions for me?". + - [Behavioral Questions](non-technical/behavioral.md) + - [Interview Formats](non-technical/format.md) + - [Psychological Tricks](non-technical/psychological.md) + - [Questions to Ask](non-technical/questions-to-ask.md) + - [Negotiation Tips](non-technical/negotiation.md) +- **[Utilities](utilities)** + - Snippets of algorithms/code that will help in coding questions. + +## Contributing + +There are no hard contributing guidelines at the moment as things are still in flux, might find a better approach to structure content, but we will figure out as we go along! Just contribute whatever that you think will help others. If you would like to contribute content for different domains, feel free to create an issue or submit a pull request and we can discuss further. diff --git a/algorithms/README.md b/algorithms/README.md new file mode 100644 index 00000000..2dd360ff --- /dev/null +++ b/algorithms/README.md @@ -0,0 +1,522 @@ +Algorithm Questions +== + +Here are some tips and questions you can think in your head or clarify with the interviewer. They may lead you to +discover corner cases you might have missed out or even lead you towards the optimal approach! + +## Contents + +- [Array](array.md) +- [Dynamic Programming and Memoization](dynamic-programming.md) +- [Geometry](geometry.md) +- [Graph](graph.md) +- [Hash Table](hash-table.md) +- [Heap](heap.md) +- [Interval](interval.md) +- [Linked List](linked-list.md) +- [Math](math.md) +- [Matrix](matrix.md) +- [Object-Oriented Programming](oop.md) +- [Permutation](permutation.md) +- [Queue](queue.md) +- [Sorting and Searching](sorting-searching.md) +- [Stack](stack.md) +- [String](string.md) +- [Tree](tree.md) + +## Picking a Programming Language + +Before anything else, you need to pick a programming language to do your interviews in. Most companies will let you code in any language you want, the only exception I know being Google, where they only allow candidates to pick from Java, C++ or Python for their algorithmic coding interviews. Most of the time, I would recommend that you use a language that you are extremely familiar with rather than picking up a new language just for doing interviews because the company uses that language heavily. + +There are some languages which are more suitable than others for coding interviews and some languages you absolutely want to avoid. From my experience interviewing as an interviewer, most candidates pick Python or Java. Other commonly seen languages include JavaScript, Ruby and C++. I would absolutely avoid lower level languages like C or Go, simply because they lack in many standard library functions and data structures. + +Personally, Python is my de facto choice for coding algorithms during interviews because it is succinct and has a pretty huge library of functions and data structures available. One of my top reasons for recommending Python is that it uses consistent APIs that operate on different data structures, such as `len()`, `for ... in ...` and slicing notation on sequences (strings/lists/tuples). Getting the last element in a sequence is `arr[-1]` and reversing it is simply `arr[::-1]`. You can achieve a lot with minimal syntax in Python. + +Java is a decent choice too but having to constantly declare types in your code means extra keystrokes which results in slower coding/typing speed. This issue will be more apparent when you have to write on a whiteboard during on-site interviews. The reasons for choosing/not choosing C++ are similar to Java. Ultimately, Python, Java and C++ are decent choices of languages. If you have been using Java at work for a while now and do not have time to be comfortably familiar with another language, I would recommend just sticking to Java instead of picking up Python from scratch just for interviews to avoid having to context switch between languages during work vs interviews. Most of the time, the bottleneck is in the thinking and not the writing. + +One exception to the convention of allowing you to "pick any programming language you want" is when you are interviewing for a domain-specific position, such as Front End/iOS/Android Engineer roles, in which you would need to be familiar with coding algorithms in JavaScript, Objective-C/Swift and Java respectively. If you need to use a data structure that the language does not support, such as a Queue or Heap in JavaScript, perhaps try asking the interviewer whether you can assume that you have a data structure that implements certain methods with specified time complexities. If the implementation of that data structure is not crucial to solving the problem, the interviewer will usually allow it. In reality, being aware of existing data structures and selecting the appropriate ones to tackle the problem at hand is more important than knowing the intricate implementation details. + +## Revise your CS101 + +If you have been out of college for a while, it will be highly advisable to revise the CS fundamentals — Algorithms and Data Structures. Personally I prefer to revise as I practiced so I scanned through my college notes and revised the various algorithms as I worked on the algorithm problems from LeetCode and Cracking the Coding Interview. + +This [interviews repository](https://github.com/kdn251) by Kevin Naughton Jr. served as a quick refresher for me. + +## Mastery through Practice + +Next, gain familiarity and mastery of the algorithms and data structures in your chosen programming language: + +1. Practice coding algorithms using your chosen language. While [Cracking the Coding Interview](http://www.crackingthecodinginterview.com/) is a good resource for practice, I prefer being able to type code, run it and get instant feedback. There are various Online Judges such as [LeetCode](https://leetcode.com/), [HackerRank](https://www.hackerrank.com/) and [CodeForces](http://codeforces.com/) for you to practice questions online and get used to the language. From experience, LeetCode questions are the most similar to the kind of questions being asked in interviews whereas HackerRank and CodeForces questions are more similar to competitive programming questions. If you practice enough LeetCode questions, there is a good chance that you would have seen/done your actual interview question (or some variant) on LeetCode before. +2. Learn and understand the time and space complexities of the common operations in your chosen language. For Python, this [page](https://wiki.python.org/moin/TimeComplexity) will come in handy. Also find out the underlying sorting algorithm that is being used in the language's `sort()` function and its time and space complexity (in Python its Timsort which is a hybrid sort). After completing a question on LeetCode, I usually add the time and space complexities of the written code as comments above the function body to remind myself to analyze the algorithm after I am done with the implementation. +3. Read up on the recommended coding style for your language and stick to it. If you have chosen Python, refer to the PEP 8 Style Guide. If you have chosen Java, refer to Google’s Java Style Guide. +4. Find out and be familiar with the common pitfalls and caveats of the language. If you point out them out during the interview and intelligently avoid falling into them, you will usually impress the interviewer and that results in bonus points in your feedback, regardless of whether the interviewer is familiar with the language or not. +5. Gain a broad exposure to questions from various topics. In the second half of the article I mention algorithm topics and practice questions for each topic. Do around 100–200 LeetCode questions and you should be good. + +Practice, practice and more practice! + +## Phases of a Coding Interview + +Congratulations, you are ready to put your skills into practice! In a real coding interview, you will be given a technical question by the interviewer, write code in a real-time collaborative editor (phone screen) or on a whiteboard (on-site) to solve the problem within 30–45 minutes. This is where the real fun begins! + +Your interviewer will be looking out for signals that you fit the requirements of the role and it is up to you to display those signals to them. Initially it may feel weird to be talking while you are coding as most programmers do not have the habit of explaining out loud as they are typing code. However, it is hard for the interviewer to know what you are thinking just by looking at the code that you type. If you communicate your approach to the interviewer before you start coding, you can validate your approach with him and the both of you can agree upon an acceptable approach. + +**Before the Interview (Remote)** + +For phone screens/remote interviews, prepare paper and pen/pencil to jot down and visualize stuff. If you are given a question on trees and graphs, it usually helps if you draw out some examples of the data structure given in the question. + +Use earphones and make sure you are in a quiet environment. You definitely do not want to be holding a phone in one hand and only be able to type with the other. Try avoiding using speakers because if the echo is bad, communication is harder and repeating of words will just result in loss of valuable time. + +**Upon Getting the Question** + +Many candidates jump into coding the moment they hear the question. That is usually a big mistake. Take a moment to repeat the question back at the interviewer and make sure that you understand exactly what he is asking. If you misunderstood and when you repeat back the question, he'll clarify. + +Always seek clarification about the question upon hearing it even if it you think it is clear to you. You might discover something that you have missed out and it also sends a signal to the interviewer that you are a careful person who pays attention to details. Consider asking the following questions: + +- How big is the size of the input? +- How big is the range of values? +- What kind of values are there? Are there negative numbers? Floating points? Will there be empty inputs? +- Are there duplicates within the input? +- What are some extreme cases of the input? +- How is the input stored? If you are given a dictionary of words, is it a list of strings or a Trie? + +After you have sufficiently clarified the scope and intention of the problem, explain your high level approach to the interviewer even if it is a naive solution. If you are stuck, consider various approaches and explain out loud why it will/will not work. Sometimes your interviewer might drop hints and lead you towards the right path. + +Start with a brute force approach, communicate it to the interviewer, explain the time and space complexity and why it is bad. It is unlikely that the brute force approach will be one that you will be coding. At this point, the interviewer will usually pop the dreaded "Can we do better?" question, meaning that he is looking for a more optimal approach. In my opinion, this is usually the hardest part of the interview. In general, look for repeated work and try to optimize them by potentially caching the calculated result somewhere and reference it later, rather than having to compute it all over again. There are some tips on tackling topic-specific questions that I dive into details below. + +Only start coding after you and your interviewer have agreed on an approach and has given you the green light. + +**Starting to Code** + +Write your code with good coding style. Reading code written by others is usually not an enjoyable task. Reading horribly-formatted code by others makes it worse. Your goal is to make your interviewer understand the code you have written so that he can quickly evaluate if your code does what you say it does and whether it solves the given problem. Use clear variable names, avoid single letter names unless they are for iteration. However, if you are coding on a whiteboard, you might not want to use extremely verbose variable names for the sake of reducing the amount you have to write. + +Always be explaining what you are currently writing/typing to the interviewer. This is not about literally reading out what you are typing to the interviewer. Talk about the section of the code you are currently implementing at a higher level, explain why it is written as such and what it is trying to achieve. + +When you copy and paste code, consider whether it is necessary. Sometimes it is, sometimes it is not. If you find yourself copying and pasting one large chunk of code spanning multiple lines, it is probably an indicator that you can refactor by containing those lines into a function. If it is just a single line you copied, usually it is fine. Do remember to change the respective variables in your copied line of code where relevant. Copy-paste errors are a common source of bugs even in day-to-day coding! + +**After Coding** + +After you have finished coding, do not immediately announce to the interviewer that you are done. In most cases, your code is usually not perfect and contains some bugs or syntax errors. What you need to do now is to review your code. + +Firstly, look through your code from start to finish as if it is the first time you are seeing it, as if it was written by someone else and you are trying to spot bugs in it. That’s exactly what your interviewer will be doing. Look through and fix any minor issues you may find. + +Next, come up with small test cases and step through the code (not your algorithm!) with those sample input. Interviewers like it when you read their mind and what they usually do after you have finished coding would be to get you to write tests. It is a huge plus if you write tests for your code even before prompts from them. You should be emulating a debugger when stepping through and jot down or say out the values of certain variables as you step through the lines of code. + +If there are huge duplicated chunks of code in your solution, it would be a good chance to refactor it and demonstrate to the interviewer that you are one who values code quality. Also look out for places where you can do [short-circuit evaluation](https://en.wikipedia.org/wiki/Short-circuit_evaluation). + +Lastly, give the time/space complexity of your code and explain why it is such. You can even annotate certain chunks of your code with the various time/space complexities to demonstrate your understanding of your code and the APIs of your chosen programming language. Explain any trade-offs in your current approach vs alternative approaches, possibly in terms of time/space. + +If your interviewer is happy with the solution, the interview usually ends here. It is also not uncommon that the interviewer asks you extension questions, such as how you would handle the problem if the whole input is too large to fit into memory, or if the input arrives as a stream. This is a common follow-up question at Google where they care a lot about scale. The answer is usually a divide-and-conquer approach — perform distributed processing of the data and only read certain chunks of the input from disk into memory, write the output back to disk and combine them later on. + +## Practicing via Mock Interviews + +Interviewing is a skill that you can get better at. The steps mentioned above can be rehearsed over and over again until you have fully internalized them and following those steps become second nature to you. A good way to practice is to find a friend to partner with and the both of you can take turns to interview each other. + +A great resource for practicing mock coding interviews would be [interviewing.io](https://interviewing.io/). interviewing.io provides free, anonymous practice technical interviews with Google and Facebook engineers, which can lead to real jobs and internships. By virtue of being anonymous during the interview, the inclusive interview process is de-biased and low risk. At the end of the interview, both interviewer and interviewees can provide feedback to each other for the purpose of improvement. Doing well in your mock interviews will unlock the jobs page and allow candidates to book interviews (also anonymously) with top companies like Uber, Lyft, Quora, Asana and more. For those who are totally new to technical interviews, you can even view a [demo interview](https://start.interviewing.io/interview/9hV9r4HEONf9/replay) on the site (requires sign in). + +I have used interviewing.io both as an interviewer and an interviewee and found the experience to be really great! [Aline Lerner](https://twitter.com/alinelernerLLC), the CEO and co-founder of interviewing.io and her team are passionate about revolutionizing the technical interview process and helping candidates to improve their skills at interviewing. She has also published a number of technical interview-related articles on the [interviewing.io blog](http://blog.interviewing.io/). interviewing.io is still in beta now but I recommend signing up as early as possible to increase the likelihood of getting an invite. + +Another platform that allows you to practice coding interviews is [Pramp](https://pramp.com/). Where interviewing.io matches potential job seekers with seasoned technical interviewers, Pramp differs takes a different approach. Pramp pairs you up with another peer who is also a job seeker and both of you take turns to assume the role of interviewer and interviewee. Pramp also prepares questions for you, along with suggested solutions and prompts to guide the interviewee. Personally, I do not really like Pramp's approach because if I were to interview someone, I would rather choose a question I am familiar with. Also, many users of the platform do not have the experience of being interviewers and that can result in a horrible interview experience. There was once where my matched peer, as the interviewer, did not have the right understanding of the question and attempted to lead me down the wrong path of solving the question. + +The next section dives deep into practical tips for specific topics of algorithms and data structures which appear frequently in coding questions. Many algorithm questions involve techniques that can be applied to questions of similar nature. The more techniques you have in your arsenal, the higher the chances of passing the interview. For each topic, there is also a list of recommended common questions which in my opinion is highly valuable for mastering the core concepts for the topic. + +## General Tips + +- Input validation: + - Always validate input first. Check for invalid/empty/negative/different type input. Never assume you are given the valid parameters. Alternatively, clarify with the interviewer whether you can assume valid input (usually yes), which can save you time from writing code that does input validation. +- Are there any time/space complexity requirements/constraints? +- Check corner cases: + - Check for off-by-one errors. + - In languages where there are no automatic type coercion, check that concatenation of values are of the same type: `int`/`str`/`list`. + - After finishing your code, use a few example inputs to test your solution. +- Is the algorithm meant to be run multiple times, for example in a web server? If yes, the input is likely to be preprocess-able to improve the efficiency in each call. +- Use a mix of functional and imperative programming paradigms: + - Write pure functions as much as possible. + - Pure functions are easier to reason about and can help to reduce bugs in your implementation. + - Avoid mutating the parameters passed into your function especially if they are passed by reference unless you are sure of what you are doing. + - However, functional programming is usually expensive in terms of space complexity because of non-mutation and the repeated allocation of new objects. On the other hand, imperative code is faster because you operate on existing objects. Hence you will need to achieve a balance between accuracy vs efficiency, by using the right amount of functional and imperative code where appropriate. + - Avoid relying on and mutating global variables. Global variables introduce state. + - If you have to rely on global variables, make sure that you do not mutate it by accident. +- Generally, to improve the speed of a program, we can either choose a more appropriate data structure/algorithm or use more memory. It's a classic space/time tradeoff. +- Data structures are your weapons. Choosing the right weapon for the right battle is the key to victory. Be very familiar about the strengths of each data structure and the time complexities for its various operations. +- Data structures can be augmented to achieve efficient time complexities across different operations. For example, a hash map can be used together with a doubly-linked list to achieve O(1) time complexity for both the `get` and `put` operation in an [LRU cache](https://leetcode.com/problems/lru-cache/). +- Hashmaps are probably the most commonly used data structure for algorithm questions. If you are stuck on a question, your last resort can be to enumerate through the possible data structures (thankfully there aren't that many of them) and consider whether each of them can be applied to the problem. This has worked for me sometimes. +- If you are cutting corners in your code, state that out loud to your interviewer and say what you would do in a non-interview setting (no time constraints). E.g., I would write a regex to parse this string rather than using `split()` which does not cover all cases. + +## Sequence + +- Arrays and strings are considered sequences (a string is a sequence of characters). There are tips relevant for dealing with both arrays and strings which will be covered here. +- Are there duplicate values in the sequence, would it affect the answer? +- Check for sequence out of bounds. +- Be mindful about slicing or concatenating sequences in your code. Typically, slicing and concatenating sequences require O(n) time. Use start and end indices to demarcate a subarray/substring where possible. +- Sometimes you can traverse the sequence from the right rather than from the left. +- Master the [sliding window technique](https://discuss.leetcode.com/topic/30941/here-is-a-10-line-template-that-can-solve-most-substring-problems) that applies to many substring/subarray problems. +- When you are given two sequences to process, it is common to have one index per sequence to traverse/compare the both of them. For example, we use the same approach to merge two sorted arrays. +- Corner cases: + - Empty sequence. + - Sequence with 1 or 2 elements. + - Sequence with repeated elements. + +## Array + +- Is the array sorted or partially sorted? If it is, some form of binary search should be possible. This also usually means that the interviewer is looking for a solution that is faster than O(n). +- Can you sort the array? Sometimes sorting the array first may significantly simplify the problem. Make sure that the order of array elements do not need to be preserved before attempting a sort. +- For questions where summation or multiplication of a subarray is involved, pre-computation using hashing or a prefix/suffix sum/product might be useful. +- If you are given a sequence and the interviewer asks for O(1) space, it might be possible to use the array itself as a hash table. For example, if the array only has values from 1 to N, where N is the length of the array, negate the value at that index (minus one) to indicate presence of that number. + +#### Practice Questions + +- [Two Sum](https://leetcode.com/problems/two-sum/) +- [Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/) +- [Contains Duplicate](https://leetcode.com/problems/contains-duplicate/) +- [Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self/) +- [Maximum Subarray](https://leetcode.com/problems/maximum-subarray/) +- [Maximum Product Subarray](https://leetcode.com/problems/maximum-product-subarray/) +- [Find Minimum in Rotated Sorted Array](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/) +- [Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/) +- [3Sum](https://leetcode.com/problems/3sum/) +- [Container With Most Water](https://leetcode.com/problems/container-with-most-water/) + +## Binary + +- Questions involving binary representations and bitwise operations are asked sometimes and you must be absolutely familiar with how to convert a number from decimal form into binary form (and vice versa) in your programming language of choice. +- Test kth bit is set: `num & (1 << k) != 0`. +- Set kth bit: `num |= (1 << k)`. +- Turn off kth bit: `num &= ~(1 << k)`. +- Toggle the kth bit: `num ^= (1 << k)`. +- To check if a number is a power of 2, `num & num - 1 == 0`. + +#### Practice Questions + +- [Sum of Two Integers](https://leetcode.com/problems/sum-of-two-integers/) +- [Number of 1 Bits](https://leetcode.com/problems/number-of-1-bits/) +- [Counting Bits](https://leetcode.com/problems/counting-bits/) +- [Missing Number](https://leetcode.com/problems/missing-number/) +- [Reverse Bits](https://leetcode.com/problems/reverse-bits/) + +## Dynamic Programming + +- Usually used to solve optimization problems. +- Alaina Kafkes has written an [awesome post](https://medium.freecodecamp.org/demystifying-dynamic-programming-3efafb8d4296) on tackling DP problems. +- The only way to get better at DP is to practice. It takes some amount of practice to be able to recognize that a problem can be solved by DP. +- Sometimes you do not need to store the whole DP table in memory, the last two values or the last two rows of the matrix will suffice. + +#### Practice Questions + +- 0/1 Knapsack +- [Climbing Stairs](https://leetcode.com/problems/climbing-stairs/) +- [Coin Change](https://leetcode.com/problems/coin-change/) +- [Longest Increasing Subsequence](https://leetcode.com/problems/longest-increasing-subsequence/) +- [Longest Common Subsequence]() +- [Word Break Problem](https://leetcode.com/problems/word-break/) +- [Combination Sum](https://leetcode.com/problems/combination-sum-iv/) +- [House Robber](https://leetcode.com/problems/house-robber/) and [House Robber II](https://leetcode.com/problems/house-robber-ii/) +- [Decode Ways](https://leetcode.com/problems/decode-ways/) +- [Unique Paths](https://leetcode.com/problems/unique-paths/) +- [Jump Game](https://leetcode.com/problems/jump-game/) + +## Geometry + +- When comparing euclidean distance between two pairs of points, using dx2 + dy2 is sufficient. It is unnecessary to square root the value. +- To find out if two circles overlap, check that the distance between the two centers of the circles is less than the sum of their radii. + +## Graph + +- Be familiar with the various graph representations, graph search algorithms and their time and space complexities. +- You can be given a list of edges and tasked to build your own graph from the edges to perform a traversal on. The common graph representations are: + - Adjacency matrix. + - Adjacency list. + - Hashmap of hashmaps. +- Some questions look like they are trees but they are actually graphs. In that case you will have to handle cycles and keep a set of visited nodes when traversing. +- Graph search algorithms: + - **Common** - Breadth-first Search, Depth-first Search + - **Uncommon** - Topological Sort, Dijkstra's algorithm + - **Rare** - Bellman-Ford algorithm, Floyd-Warshall algorithm, Prim's algorithm, Kruskal's algorithm +- In coding interviews, graphs are commonly represented as 2-D matrices where cells are the nodes and each cell can traverse to its adjacent cells (up/down/left/right). Hence it is important that you be familiar with traversing a 2-D matrix. When recursively traversing the matrix, always ensure that your next position is within the boundary of the matrix. More tips for doing depth-first searches on a matrix can be found [here](https://discuss.leetcode.com/topic/66065/python-dfs-bests-85-tips-for-all-dfs-in-matrix-question/). A simple template for doing depth-first searches on a matrix goes like this: + +```py +def traverse(matrix): + rows, cols = len(matrix), len(matrix[0]) + visited = set() + directions = ((0, 1), (0, -1), (1, 0), (-1, 0)) + def dfs(i, j): + if (i, j) in visited: + return + visited.add((i, j)) + # Traverse neighbors + for direction in directions: + next_i, next_j = i + direction[0], j + direction[1] + if 0 <= next_i < rows and 0 <= next_j < cols: # Check boundary + # Add any other checking here ^ + dfs(next_i, next_j) + + for i in range(rows): + for j in range(cols): + dfs(i, j) +``` + +- Corner cases: + - Empty graph. + - Graph with one or two nodes. + - Disjoint graphs. + - Graph with cycles. + +#### Practice Questions + +- [Clone Graph](https://leetcode.com/problems/clone-graph/) +- [Course Schedule](https://leetcode.com/problems/course-schedule/) +- [Alien Dictionary](https://leetcode.com/problems/alien-dictionary/) +- [Pacific Atlantic Water Flow](https://leetcode.com/problems/pacific-atlantic-water-flow/) +- [Number of Islands](https://leetcode.com/problems/number-of-islands/) +- [Graph Valid Tree](https://leetcode.com/problems/graph-valid-tree/) +- [Number of Connected Components in an Undirected Graph](https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/) +- [Longest Consecutive Sequence](https://leetcode.com/problems/longest-consecutive-sequence/) + +## Interval + +- Interval questions are questions where you are given an array of two-element arrays (an interval) and the two values represent a start and an end value. Interval questions are considered part of the array family but they involve some common techniques hence they are extracted out to this special section of their own. +- An example interval array: `[[1, 2], [4, 7]]`. +- Interval questions can be tricky to those who have not tried them before because of the sheer number of cases to consider when they overlap. +- Do clarify with the interviewer whether `[1, 2]` and `[2, 3]` are considered overlapping intervals as it affects how you will write your equality checks. +- A common routine for interval questions is to sort the array of intervals by each interval's starting value. +- Be familiar with writing code to check if two intervals overlap and merging two overlapping intervals: + +```py +def is_overlap(a, b): + return a[0] < b[1] and b[0] < a[1] + +def merge_overlapping_intervals(a, b): + return [min(a[0], b[0]), max(a[1], b[1])] +``` + +- Corner cases: + - Single interval. + - Non-overlapping intervals. + - An interval totally consumed within another interval. + - Duplicate intervals. + +#### Practice Questions + +- [Insert Interval](https://leetcode.com/problems/insert-interval/) +- [Merge Intervals](https://leetcode.com/problems/merge-intervals/) +- [Meeting Rooms](https://leetcode.com/problems/meeting-rooms/) and [Meeting Rooms II](https://leetcode.com/problems/meeting-rooms-ii/) +- [Non-overlapping Intervals](https://leetcode.com/problems/non-overlapping-intervals/) + +## Linked List + +- Like arrays, linked lists are used to represent sequential data. The benefit of linked lists is that insertion and deletion from anywhere in the list is O(1) whereas in arrays the following elements will have to be shifted. +- Adding a dummy node at the head and/or tail might help to handle many edge cases where operations have to be performed at the head or the tail. The presence of dummy nodes essentially ensures that operations will never have be done on the head or the tail, thereby removing a lot of headache in writing conditional checks to dealing with null pointers. Be sure to remember to remove them at the end of the operation. +- Sometimes linked lists problem can be solved without additional storage. Try to borrow ideas from reverse a linked list problem. +- For deletion in linked lists, you can either modify the node values or change the node pointers. You might need to keep a reference to the previous element. +- For partitioning linked lists, create two separate linked lists and join them back together. +- Linked lists problems share similarity with array problems, think about how you would do it for an array and try to apply it to a linked list. +- Two pointer approaches are also common for linked lists. For example: + - Getting the kth from last node - Have two pointers, where one is k nodes ahead of the other. When the node ahead reaches the end, the other node is k nodes behind. + - Detecting cycles - Have two pointers, where one pointer increments twice as much as the other, if the two pointers meet, means that there is a cycle. + - Getting the middle node - Have two pointers, where one pointer increments twice as much as the other. When the faster node reaches the end of the list, the slower node will be at the middle. +- Be familiar with the following routines because many linked list questions make use of one or more of these routines in the solution: + - Counting the number of nodes in the linked list. + - Reversing a linked list in-place. + - Finding the middle node of the linked list using fast/slow pointers. + - Merging two lists together. +- Corner cases: + - Single node. + - Two nodes. + - Cycle in linked list - Clarify whether there can be a cycle in the list? Usually the answer is no. + +#### Practice Questions + +- [Reverse a Linked List](https://leetcode.com/problems/reverse-linked-list/) +- [Detect Cycle in a Linked List](https://leetcode.com/problems/linked-list-cycle/) +- [Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/) +- [Merge K Sorted Lists](https://leetcode.com/problems/merge-k-sorted-lists/) +- [Remove Nth Node From End Of List](https://leetcode.com/problems/remove-nth-node-from-end-of-list/) +- [Reorder List](https://leetcode.com/problems/reorder-list/) + +## Math + +- If code involves division or modulo, remember to check for division or modulo by 0 case. +- When a question involves "a multiple of a number", perhaps modulo might be useful. +- Check for and handle overflow/underflow if you are using a typed language like Java and C++. At the very least, mention that overflow/underflow is possible and ask whether you need to handle it. +- Consider negative numbers and floating point numbers. This may sound obvious, but under interview pressure, many obvious cases go unnoticed. +- If the question asks to implement an operator such as power, squareroot or division and want it to be faster than O(n), binary search is usually the approach to go. +- Some common formulas: + - Sum of 1 to N = (n+1) * n/2 + - Sum of GP = 20 + 21 + 22 + 23 + ... 2n = 2n+1 - 1 + - Permutations of N = N! / (N-K)! + - Combinations of N = N! / (K! * (N-K)!) + +#### Practice Questions + +- [Pow(x, n)](https://leetcode.com/problems/powx-n/) +- [Sqrt(x)](https://leetcode.com/problems/sqrtx/) +- [Integer to English Words](https://leetcode.com/problems/integer-to-english-words/) + +## Matrix + +- A matrix is a 2-dimensional array. Questions involving matrices are usually related to dynamic programming or graph traversal. +- Corner cases: + - Empty matrix. Check that none of the arrays are 0 length. + - 1 x 1 matrix. + - Matrix with only one row or column. +- For questions involving traversal or dynamic programming, you almost always want to make a copy of the matrix with the same dimensions that is initialized to empty values to store the visited state or dynamic programming table. Be familiar with such a routine: + +```py +rows, cols = len(matrix), len(matrix[0]) +copy = [[0 for _ in range(cols)] for _ in range(rows)] +``` + +- Many grid-based games can be modeled as a matrix, such as Tic-Tac-Toe, Sudoku, Crossword, Connect 4, Battleship, etc. It is not uncommon to be asked to verify the winning condition of the game. For games like Tic-Tac-Toe, Connect 4 and Crosswords, where verification has to be done vertically and horizontally, one trick is to write code to verify the matrix for the horizontal cells, transpose the matrix and reuse the logic for horizontal verification to verify originally vertical cells (which are now horizontal). +- Transposing a matrix in Python is simply: + +```py +transposed_matrix = zip(*matrix) +``` + +#### Practice Questions + +- [Set Matrix Zeroes](https://leetcode.com/problems/set-matrix-zeroes/) +- [Spiral Matrix](https://leetcode.com/problems/spiral-matrix/) +- [Rotate Image](https://leetcode.com/problems/rotate-image/) +- [Word Search](https://leetcode.com/problems/word-search/) + +## Recursion + +- Remember to always define a base case so that your recursion will end. +- Useful for permutation, generating all combinations and tree-based questions. +- Be familiar with how to generate all permutations of a sequence as well as how to handle duplicates. +- Recursion implicitly uses a stack. Hence all recursive approaches can be rewritten iteratively using a stack. +- Beware of cases where the recursion level goes too deep and causes a stack overflow (the default limit in Python is 1000). +- Recursion will never be O(1) space complexity because a stack is involved unless there is [tail-call optimization](https://stackoverflow.com/questions/310974/what-is-tail-call-optimization). Do find out if your chosen language supports tail-call optimization. + +#### Practice Questions + +- [Subsets](https://leetcode.com/problems/subsets/) and [Subsets II](https://leetcode.com/problems/subsets-ii/) +- [Strobogrammatic Number II](https://leetcode.com/problems/strobogrammatic-number-ii/) + +## String + +- Please read the above tips on [Sequence](#sequence) because they apply to strings too. +- Corner cases: + - Strings with only one distinct character. +- Ask about input character set and case sensitivity. Usually the characters are limited to lower case Latin characters, i.e. a-z. +- When you need to compare strings where the order isn't important (like anagram), you may consider using a hash map as a counter. If your language has a built-in `Counter` class like Python, ask to use that instead. +- If you need to keep a counter of characters, a common mistake to make is to say that the space complexity required for the counter is O(n). The space required for a counter is O(1) not O(n), because the upper bound is the range of characters which is usually a fixed constant of 26 when the input set is just lower case Latin characters. +- Common data structures for looking up strings efficiently: + - [Trie / Prefix Tree](https://www.wikiwand.com/en/Trie) + - [Suffix Tree](https://www.wikiwand.com/en/Suffix_tree) +- Common string algorithms: + - [Rabin Karp](https://www.wikiwand.com/en/Rabin%E2%80%93Karp_algorithm) for efficient searching of substring using a rolling hash. + - [KMP](https://www.wikiwand.com/en/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm) for efficient searching of substring. + +#### Non-repeating Characters + +- Use a 26-bit bitmask to indicate which lower case latin characters are inside the string. + +```py +mask = 0 +for c in set(word): + mask |= (1 << (ord(c) - ord('a'))) +``` + +- To determine if two strings have common characters, perform `&` on the two bitmasks and if the result is non-zero, the two strings do have common characters: `mask_a & mask_b > 0`. + +#### Anagram + +- An anagram is direct word switch or word play, the result of rearranging the letters of a word or phrase to produce a new word or phrase, using all the original letters exactly once. In interviews, usually we are only bothered with words without spaces in them. +- Determine if two strings are anagrams: + - Sorting both strings should produce the same resulting string. + - If we map each character to a prime number and we multiply each mapped number together, anagrams should have the same multiple (prime factor decomposition). + - Frequency counting of characters will help to determine if two strings are anagrams. + +#### Palindrome + +- A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward as forward, such as madam or racecar. +- Ways to determine if a string is a palindrome: + - Reverse the string and it should be equal to itself. + - Have two pointers at the start and end of the string, move inwards till they meet. At any point in time the characters at both pointers should match. +- The order of characters within the string matters, so hash maps are usually not helpful. +- When a question is about counting the number of palindromes, a common trick is to have two pointer that move outwards, away from the middle. Note that palindromes can be even/odd length, and that for each middle pivot position, you would need to check twice, once including the character, and once without. + - For substrings, you can terminate early once there is no match. + - For subsequences, use dynamic programming as there are overlapping subproblems. Check out [this question](https://leetcode.com/problems/longest-palindromic-subsequence/). + +#### Practice Questions + +- [Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/) +- [Longest Repeating Character Replacement](https://leetcode.com/problems/longest-repeating-character-replacement/) +- [Minimum Window Substring](https://leetcode.com/problems/minimum-window-substring/description/) +- [Encode and Decode Strings](https://leetcode.com/problems/encode-and-decode-strings/) +- [Valid Anagram](https://leetcode.com/problems/valid-anagram) +- [Group Anagrams](https://leetcode.com/problems/group-anagrams/) +- [Valid Parentheses](https://leetcode.com/problems/valid-parentheses) +- [Valid Palindrome](https://leetcode.com/problems/valid-palindrome/) +- [Longest Palindromic Substring](https://leetcode.com/problems/longest-palindromic-substring/) +- [Palindromic Substrings](https://leetcode.com/problems/palindromic-substrings/) + +## Tree + +- A tree is an undirected, connected, acyclic graph. +- Recursion is a common approach for trees. When you notice the subtree problem can be used to solve the whole problem, you should try recursion. +- When using recursion, always remember to check for the base case, usually where the node is `null`. +- When you are asked to traverse a tree by level, use depth-first search. +- Sometimes it is possible that your recursive function needs to return two values. +- If the question involves summation of nodes along the way, be sure to check whether nodes can be negative. +- You should be very familiar with writing pre-order, in-order and post-order traversal recursively. As an extension, challenge yourself by writing them iteratively. Sometimes interviewers do ask candidates for the iterative approach, especially if the candidate finishes writing the recursive approach too fast. +- Corner cases: + - Empty tree. + - Single node. + - Two nodes. + - Very skewed tree (like a linked list). + +**Binary Tree** + +- In-order traversal of a binary tree is insufficient to uniquely serialize a tree. Pre-order or post-order traversal is also required. + +**Binary Search Tree** + +- In-order traversal of a BST will give you all elements in order. +- Be very familiar with the properties of a BST and validating that a binary tree is a BST. This comes up more often than expected. +- When a question involves a BST, the interviewer is usually looking for a solution which runs faster than O(n). + +#### Practice Questions + +- [Maximum Depth of Binary Tree](https://leetcode.com/problems/maximum-depth-of-binary-tree/) +- [Same Tree](https://leetcode.com/problems/same-tree/) +- [Invert/Flip Binary Tree](https://leetcode.com/problems/invert-binary-tree/) +- [Binary Tree Maximum Path Sum](https://leetcode.com/problems/binary-tree-maximum-path-sum/) +- [Binary Tree Level Order Traversal](https://leetcode.com/problems/binary-tree-level-order-traversal/) +- [Serialize and Deserialize Binary Tree](https://leetcode.com/problems/serialize-and-deserialize-binary-tree/) +- [Subtree of Another Tree](https://leetcode.com/problems/subtree-of-another-tree/) +- [Construct Binary Tree from Preorder and Inorder Traversal](https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/) +- [Validate Binary Search Tree](https://leetcode.com/problems/validate-binary-search-tree/) +- [Kth Smallest Element in a BST](https://leetcode.com/problems/kth-smallest-element-in-a-bst/) +- [Lowest Common Ancestor of BST](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/) + +## Trie + +- Tries are special trees (prefix trees) that make searching and storing strings more efficient. Tries have many practical applications such as for searching and autocomplete. It will be helpful to know these common applications so that you can easily identify when a problem can be solved efficiently using a trie. +- Sometimes preprocessing a dictionary of words (given in a list) into a trie will improve the efficiency when searching for a word of length k among n words. Searching becomes O(k) instead of O(n). +- LeetCode has written a [very comprehensive article](https://leetcode.com/articles/implement-trie-prefix-tree/) on tries which you are highly encouraged to read. +- Be familiar with implementing a `Trie` class and its `add`, `remove` and `search` methods from scratch. + +#### Practice Questions + +- [Implement Trie (Prefix Tree)](https://leetcode.com/problems/implement-trie-prefix-tree) +- [Add and Search Word](https://leetcode.com/problems/add-and-search-word-data-structure-design) +- [Word Search II](https://leetcode.com/problems/word-search-ii/) + +## Heap + +- If you see a top/lowest K being mentioned in the question, it is usually a signal that a heap can be used to solve the problem. +- If you require the top K elements use a Min Heap of size K. Iterate through each element, pushing it into the heap. Whenever the heap size exceeds K, remove the minimum element, that will guarantee that you have the K largest elements. + +#### Practice Questions + +- [Merge K Sorted Lists](https://leetcode.com/problems/merge-k-sorted-lists/) +- [Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/) +- [Find Median from Data Stream](https://leetcode.com/problems/find-median-from-data-stream/) + +###### References + +- http://blog.triplebyte.com/how-to-pass-a-programming-interview +- https://quip.com/q41AA3OmoZbC +- http://www.geeksforgeeks.org/must-do-coding-questions-for-companies-like-amazon-microsoft-adobe/ diff --git a/algorithms/array.md b/algorithms/array.md new file mode 100644 index 00000000..43a9a0e7 --- /dev/null +++ b/algorithms/array.md @@ -0,0 +1,63 @@ +Arrays +== + +- In an arrays of arrays, e.g. given `[[], [1, 2, 3], [4, 5], [], [], [6, 7], [8], [9, 10], [], []]`, print: `1, 2, 3, 4, 5, 6, 7, 8, 9, 10`. + - Implement an iterator that supports `hasNext()`, `next()` and `remove()` methods. +- Given a list of item prices, find all possible combinations of items that sum a particular value `K`. +- Paginate an array with constraints, such as skipping certain items. +- Implement a circular buffer using an array. +- Given array of arrays, sort them in ascending order. +- Given an array of integers, print out a histogram of using said array; include a base layer (all stars) + - E.g. `[5, 4, 0, 3, 4, 1]` + +``` +* +** * +** ** +** ** +** *** +****** +``` + +- Given an array and an index, find the product of the elements of the array except the element at that index. +- Given a set of rectangles represented by a height and an interval along the y-axis, determine the size of its union. +- Given 2 separate arrays, write a method to find the values that exist in both arrays and return them. +- Given an array of integers find whether there is a sub-sequence that sums to 0 and return it. + - E.g. `[1, 2, -3, 1]` => `[1, 2, -3]` or `[2, -3, 1]`. +- Given an input array and another array that describes a new index for each element, mutate the input array so that each element ends up in their new index. Discuss the runtime of the algorithm and how you can be sure there would not be any infinite loops. +- Given an array of non-negative numbers, find continuous subarray with sum to S. + - [Source](http://blog.gainlo.co/index.php/2016/06/01/subarray-with-given-sum/). +- Given an array of numbers list out all triplets that sum to 0. Do so with a running time of less than O(n^3). + - [Source](http://blog.gainlo.co/index.php/2016/07/19/3sum/). +- Given an array of numbers list out all quadruplets that sum to 0. Do so with a running time of less than O(n^4). +- Given an array of integers, move all the zeroes to the end while preserving the order of the other elements. You have to do it in-place and are not allowed to use any extra storage. +- Given a list of people, and a function `knows(a, b)` that returns `true`/`false` if person `a` knows the person `b`. Write a function that finds a VIP, which everybody knows and he doesn't know anybody else. +- Given an array of integers, find the subarray with the largest sum. Can you do it in linear time. + - Maximum subarray sum problem. +- You have an array with the heights of an island (at point 1, point 2 etc) and you want to know how much water would remain on this island (without flowing away). + - Trapping rain water question. +- Given a sequence of tasks like `A, B, C` (means 3 different tasks), and a cold time, which means you need to wait for that much time to start the next same task, output the best task-finishing sequence. + - E.g. input: `AAABBB, 2`, output: `AB_AB_AB` ( `_` represents do nothing and wait) +- You are given a list of dominoes. Determine if any two of those dominoes add up to `[6, 6]`. + - E.g. `[1, 4]` + `[5, 2]`). +- Given an array containing only digits `0-9`, add one to the number and return the array. + - E.g. Given `[1, 4, 2, 1]` which represents `1421`, return `[1, 4, 2, 2]` which represents `1422`. +- Find the second maximum value in an array. +- Given an array, find the longest arithmetic progression. +- Rotate an array by an offset of k. +- Remove duplicates in an unsorted array where the duplicates are at a distance of k or less from each other. +- Given an unsorted list of integers, return true if the list contains any duplicates within k indices of each element. Do it faster than O(n^2). +- Given an unsorted list of integers, return true if the list contains any fuzzy duplicates within k indices of each element. A fuzzy duplicate is another integer within d of the original integer. Do it faster than O(n^2). + - E.g. If d = 4, then 6 is a fuzzy duplicate of 3 but 8 is not. +- Say you have an unordered list of numbers ranging from 1 to n, and one of the numbers is removed, how do you find that number? What if two numbers are removed? +- Given an array of string, find the duplicated elements. + - [Source](http://blog.gainlo.co/index.php/2016/05/10/duplicate-elements-of-an-array/). +- Given an array of integers, find a maximum sum of non-adjacent elements. + - E.g. `[1, 0, 3, 9, 2]` should return `10 (1 + 9)`. + - [Source](http://blog.gainlo.co/index.php/2016/12/02/uber-interview-question-maximum-sum-non-adjacent-elements/) +- Given an array of integers, modify the array by moving all the zeros to the end (right side). The order of other elements doesn’t matter. + - E.g. `[1, 2, 0, 3, 0, 1, 2]`, the program can output `[1, 2, 3, 1, 2, 0, 0]`. + - [Source](http://blog.gainlo.co/index.php/2016/11/18/uber-interview-question-move-zeroes/). +- Given an array, return the length of the longest increasing contiguous subarray. + - E.g., `[1, 3, 2, 3, 4, 8, 7, 9]`, should return `4` because the longest increasing array is `[2, 3, 4, 8]`. + - [Source](http://blog.gainlo.co/index.php/2017/02/02/uber-interview-questions-longest-increasing-subarray/). diff --git a/algorithms/dynamic-programming.md b/algorithms/dynamic-programming.md new file mode 100644 index 00000000..77de6e8d --- /dev/null +++ b/algorithms/dynamic-programming.md @@ -0,0 +1,27 @@ +Dynamic Programming +== + +- Given a flight itinerary consisting of starting city, destination city, and ticket price (2D list) - find the optimal price flight path to get from start to destination. (A variation of Dynamic Programming Shortest Path) +- Given some coin denominations and a target value `M`, return the coins combination with the minimum number of coins. + - Time complexity: `O(MN)`, where N is the number of distinct type of coins. + - Space complexity: `O(M)`. +- Given a set of numbers in an array which represent number of consecutive days of AirBnb reservation requested, as a host, pick the sequence which maximizes the number of days of occupancy, at the same time, leaving at least a 1 day gap in-between bookings for cleaning. + - Problem reduces to finding the maximum sum of non-consecutive array elements. + - E.g. + ~~~ + // [5, 1, 1, 5] => 10 + The above array would represent an example booking period as follows - + // Dec 1 - 5 + // Dec 5 - 6 + // Dec 6 - 7 + // Dec 7 - 12 + + The answer would be to pick Dec 1-5 (5 days) and then pick Dec 7-12 for a total of 10 days of occupancy, at the same time, leaving at least 1 day gap for cleaning between reservations. + + Similarly, + // [3, 6, 4] => 7 + // [4, 10, 3, 1, 5] => 15 + ~~~ +- How many string representations are there for an integer where `a->1, b->2, ... z->26`. + - E.g. `26 => 2`. Because `26` can be encoded as `"z"` and `"bf"`. +- Given a list of denominations (e.g., `[1, 2, 5]` means you have coins worth $1, $2, and $5) and a target number `k`, find all possible combinations, if any, of coins in the given denominations that add up to `k`. You can use coins of the same denomination more than once. diff --git a/algorithms/geometry.md b/algorithms/geometry.md new file mode 100644 index 00000000..062f861b --- /dev/null +++ b/algorithms/geometry.md @@ -0,0 +1,7 @@ +Geometry +== + +- You have a plane with lots of rectangles on it, find out how many of them intersect. +- Which data structure would you use to query the k-nearest points of a set on a 2D plane? +- Given many points, find k points that are closest to the origin. +- How would you triangulate a polygon? diff --git a/algorithms/graph.md b/algorithms/graph.md new file mode 100644 index 00000000..89174483 --- /dev/null +++ b/algorithms/graph.md @@ -0,0 +1,12 @@ +Graph +== + +- Given a list of sorted words from an alien dictionary, find the order of the alphabet. + - Alien Dictionary Topological Sort question. +- Find if a given string matches any path in a labeled graph. A path may contain cycles. +- Given a friendship graph (an undirected graph where nodes represent people and edges means two people know each other), find all your 2nd degree connections (friends’ friends). Output these 2nd degree connections ranked by number of common friends (i.e 1st degree connections) with you, (example: if 2nd degree connection A has 10 common friends (1st degree connections) with you but 2nd degree connection B has 8 common friends (1st degree connections) with you, then A should be ranked first). +- Given a bipartite graph, separate the vertices into two sets. +- Given a list of email addresses, and a similarity function which says whether two email addresses are similar, write a function to separate the list into sets of email addresses that are similar to each other. +- You are a thief trying to sneak across a rectangular field. There are alarms placed on the fields and they each have a circular sensing radius which will trigger if anyone steps into it. Each alarm may not have the same radius. Determine if you can get from one end of the field to the other end. +- Given a graph and two nodes, determine if there exists a path between them. +- Determine if a cycle exists in the graph. diff --git a/algorithms/hash-table.md b/algorithms/hash-table.md new file mode 100644 index 00000000..c8655047 --- /dev/null +++ b/algorithms/hash-table.md @@ -0,0 +1,7 @@ +Hash Table +== + +- Describe an implementation of a least-used cache, and big-O notation of it. +- A question involving an API's integration with hash map where the buckets of hash map are made up of linked lists. +- Implement data structure `Map` storing pairs of integers (key, value) and define following member functions in O(1) runtime: `void insert(key, value)`, `void delete(key)`, `int get(key)`, `int getRandomKey()`. + - [Source](http://blog.gainlo.co/index.php/2016/08/14/uber-interview-question-map-implementation/). diff --git a/algorithms/heap.md b/algorithms/heap.md new file mode 100644 index 00000000..1e68064f --- /dev/null +++ b/algorithms/heap.md @@ -0,0 +1,5 @@ +Heap +== + +- Merge `K` sorted lists together into a single list. +- Given a stream of integers, write an efficient function that returns the median value of the integers. diff --git a/algorithms/interval.md b/algorithms/interval.md new file mode 100644 index 00000000..5dd3e373 --- /dev/null +++ b/algorithms/interval.md @@ -0,0 +1,28 @@ +Interval +== + +- Given a list of schedules, provide a list of times that are available for a meeting. + ``` + [ + [[4,5], [6,10], [12,14]], + [[4,5], [5,9], [13,16]], + [[11,14]] + ] + + Example Output: + [[0,4], [11,12], [16,23]] + ``` +- You have a number of meetings (with their start and end times). You need to schedule them using the minimum number of rooms. Return the list of meetings in every room. +- Interval ranges: + - Given 2 interval ranges, create a function to tell me if these ranges intersect. Both start and end are inclusive: `[start, end]` + - E.g. `[1, 4]` and `[5, 6]` => `false` + - E.g. `[1, 4]` and `[3, 6]` => `true` + - Given 2 interval ranges that intersect, now create a function to merge the 2 ranges into a single continuous range. + - E.g. `[1, 4]` and `[3, 6]` => `[1, 6]` + - Now create a function that takes a group of unsorted, unorganized intervals, merge any intervals that intersect and sort them. The result should be a group of sorted, non-intersecting intervals. + - Now create a function to merge a new interval into a group of sorted, non-intersecting intervals. After the merge, all intervals should remain + non-intersecting. +- Given a list of meeting times, check if any of them overlap. The follow-up question is to return the minimum number of rooms required to accommodate all the meetings. + - [Source](http://blog.gainlo.co/index.php/2016/07/12/meeting-room-scheduling-problem/) +- If you have a list of intervals, how would you merge them? + - E.g. `[1, 3], [8, 11], [2, 6]` => `[1, 6], [8-11]` diff --git a/algorithms/linked-list.md b/algorithms/linked-list.md new file mode 100644 index 00000000..67cda73f --- /dev/null +++ b/algorithms/linked-list.md @@ -0,0 +1,13 @@ +Linked List +== + +- Given a linked list, in addition to the next pointer, each node has a child pointer that can point to a separate list. With the head node, flatten the list to a single-level linked list. + - [Source](http://blog.gainlo.co/index.php/2016/06/12/flatten-a-linked-list/) +- Reverse a singly linked list. Implement it recursively and iteratively. +- Convert a binary tree to a doubly circular linked list. +- Implement an LRU cache with O(1) runtime for all its operations. +- Check distance between values in linked list. +- A question involving an API's integration with hash map where the buckets of hash map are made up of linked lists. +- Given a singly linked list (a list which can only be traversed in one direction), find the item that is located at 'k' items from the end. So if the list is a, b, c, d and k is 2 then the answer is 'c'. The solution should not search the list twice. +- How can you tell if a Linked List is a Palindrome? +- Implement a LRU cache with O(1) runtime for all its operations. diff --git a/algorithms/math.md b/algorithms/math.md new file mode 100644 index 00000000..fdff1ce5 --- /dev/null +++ b/algorithms/math.md @@ -0,0 +1,20 @@ +Math +== + +- Create a square root function. +- Given a string such as "123" or "67", write a function to output the number represented by the string without using casting. +- Make a program that can print out the text form of numbers from 1 - 1000 (ex. 20 is "twenty", 105 is "one hundred and five"). +- Write a function that parses Roman numerals. + - E.g. `XIV` returns `14`. +- Write in words for a given digit. + - E.g. `123` returns `one hundred and twenty three`. +- Given a number `N`, find the largest number just smaller than `N` that can be formed using the same digits as `N`. +- Compute the square root of `N` without using any existing functions. +- Given numbers represented as binary strings, and return the string containing their sum. + - E.g. `add('10010', '101')` returns `'10111'`. +- Take in an integer and return its english word-format. + - E.g. 1 -> "one", -10,203 -> "negative ten thousand two hundred and three". +- Write a function that returns values randomly, according to their weight. Suppose we have 3 elements with their weights: A (1), B (1) and C (2). The function should return A with probability 25%, B with 25% and C with 50% based on the weights. + - [Source](http://blog.gainlo.co/index.php/2016/11/11/uber-interview-question-weighted-random-numbers/) +- Given a number, how can you get the next greater number with the same set of digits? + - [Source](http://blog.gainlo.co/index.php/2017/01/20/arrange-given-numbers-to-form-the-biggest-number-possible/) diff --git a/algorithms/matrix.md b/algorithms/matrix.md new file mode 100644 index 00000000..6093c692 --- /dev/null +++ b/algorithms/matrix.md @@ -0,0 +1,18 @@ +Matrix +== + +- You're given a 3 x 3 board of a tile puzzle, with 8 tiles numbered 1 to 8, and an empty spot. You can move any tile adjacent to the empty spot, to the empty spot, creating an empty spot where the tile originally was. The goal is to find a series of moves that will solve the board, i.e. get `[[1, 2, 3], [4, 5, 6], [7, 8, - ]]` where - is the empty tile. +- Boggle implementation. Given a dictionary, and a matrix of letters, find all the words in the matrix that are in the dictionary. You can go across, down or diagonally. +- The values of the matrix will represent numbers of carrots available to the rabbit in each square of the garden. If the garden does not have an exact center, the rabbit should start in the square closest to the center with the highest carrot count. On a given turn, the rabbit will eat the carrots available on the square that it is on, and then move up, down, left, or right, choosing the the square that has the most carrots. If there are no carrots left on any of the adjacent squares, the rabbit will go to sleep. You may assume that the rabbit will never have to choose between two squares with the same number of carrots. Write a function which takes a garden matrix and returns the number of carrots the rabbit eats. You may assume the matrix is rectangular with at least 1 row and 1 column, and that it is populated with non-negative integers. For example, + - Example: `[[5, 7, 8, 6, 3], [0, 0, 7, 0, 4], [4, 6, 3, 4, 9], [3, 1, 0, 5, 8]]` should return `27`. +- Print a matrix in a spiral fashion. +- In the Game of life, calculate how to compute the next state of the board. Follow up was to do it if there were memory constraints (board represented by a 1 TB file). +- Grid Illumination: Given an NxN grid with an array of lamp coordinates. Each lamp provides illumination to every square on their x axis, every square on their y axis, and every square that lies in their diagonal (think of a Queen in chess). Given an array of query coordinates, determine whether that point is illuminated or not. The catch is when checking a query all lamps adjacent to, or on, that query get turned off. The ranges for the variables/arrays were about: 10^3 < N < 10^9, 10^3 < lamps < 10^9, 10^3 < queries < 10^9. +- You are given a matrix of integers. Modify the matrix such that if a row or column contains a 0, make the values in the entire row or column 0. +- Given an N x N matrix filled randomly with different colors (no limit on what the colors are), find the total number of groups of each color - a group consists of adjacent cells of the same color touching each other. +- You have a 4 x 4 board with characters. You need to write a function that finds if a certain word exists in the board. You can only jump to neighboring characters (including diagonally adjacent). +- Count the number of islands in a binary matrix of 0's and 1's. +- Check a 6 x 7 Connect 4 board for a winning condition. +- Given a fully-filled Sudoku board, check whether fulfills the Sudoku condition. +- Implement a function that checks if a player has won tic-tac-toe. +- Given an N x N matrix of 1's and 0's, figure out if all of the 1's are connected. diff --git a/algorithms/oop.md b/algorithms/oop.md new file mode 100644 index 00000000..54c32a4c --- /dev/null +++ b/algorithms/oop.md @@ -0,0 +1,4 @@ +Object-Oriented Programming +== + +- How would you design a chess game? What classes and objects would you use? What methods would they have? diff --git a/algorithms/permutation.md b/algorithms/permutation.md new file mode 100644 index 00000000..00116bf6 --- /dev/null +++ b/algorithms/permutation.md @@ -0,0 +1,12 @@ +Permutation +== + +- You are given a 7 digit phone number, and you should find all possible letter combinations based on the digit-to-letter mapping on numeric pad and return only the ones that have valid match against a given dictionary of words. +- Give all possible letter combinations from a phone number. +- Generate all subsets of a string. +- Print all possible `N` pairs of balanced parentheses. + - E.g. when `N` is `2`, the function should print `(())` and `()()`. + - E.g. when `N` is `3`, we should get `((()))`, `(()())`, `(())()`, `()(())`, `()()()`. + - [Source](http://blog.gainlo.co/index.php/2016/12/23/uber-interview-questions-permutations-parentheses/) +- Given a list of arrays, return a list of arrays, where each array is a combination of one element in each given array. + - E.g. If the input is `[[1, 2, 3], [4], [5, 6]]`, the output should be `[[1, 4, 5], [1, 4, 6], [2, 4, 5], [2, 4, 6], [3, 4, 5], [3, 4, 6]]`. diff --git a/algorithms/queue.md b/algorithms/queue.md new file mode 100644 index 00000000..7710f991 --- /dev/null +++ b/algorithms/queue.md @@ -0,0 +1,4 @@ +Queue +== + +- Implement a Queue class from scratch with an existing bug, the bug is that it cannot take more than 5 elements. diff --git a/algorithms/sorting-searching.md b/algorithms/sorting-searching.md new file mode 100644 index 00000000..49f04962 --- /dev/null +++ b/algorithms/sorting-searching.md @@ -0,0 +1,18 @@ +Sorting and Searching +== + +- Sorting search results on a page given a certain set of criteria. +- Sort a list of numbers in which each number is at a distance `K` from its actual position. +- Given an array of integers, sort the array so that all odd indexes are greater than the even indexes. +- Given users with locations in a list and a logged-in user with locations, find their travel buddies (people who shared more than half of your locations). +- Search for an element in a sorted and rotated array. + - [Source](http://blog.gainlo.co/index.php/2017/01/12/rotated-array-binary-search/) +- Sort a list where each element is no more than k positions away from its sorted position. +- If you have `N` revisions of a program, write a program that will find and return the first bad revision given a `isBad(revision i)` function. +- Search for an item in a sorted, but rotated, array. +- Merge two sorted lists together. +- Given a list of numbers and a function that returns Low, Medium, or High, sort the list by Lows, then Mediums, then Highs. +- Give 3 distinct algorithms to find the K largest values in a list of N items. +- Find the minimum element in a sorted rotated array in faster than O(n) time. +- Write a function that takes a number as input and outputs the biggest number with the same set of digits. + - [Source](http://blog.gainlo.co/index.php/2017/01/20/arrange-given-numbers-to-form-the-biggest-number-possible/) diff --git a/algorithms/stack.md b/algorithms/stack.md new file mode 100644 index 00000000..627ee2c6 --- /dev/null +++ b/algorithms/stack.md @@ -0,0 +1,8 @@ +Stack +== + +- Implementation of an interpreter for a small language that does multiplication/addition/etc. +- Design a `MinStack` data structure that supports a `min()` operation that returns the minimum value in the stack in O(1) time. +- Write an algorithm to determine if all of the delimiters in an expression are matched and closed. + - E.g. `{ac[bb]}`, `[dklf(df(kl))d]{}` and `{[[[]]]}` are matched. But `{3234[fd` and `{df][d}` are not. + - [Source](http://blog.gainlo.co/index.php/2016/09/30/uber-interview-question-delimiter-matching/) diff --git a/algorithms/string.md b/algorithms/string.md new file mode 100644 index 00000000..4458f282 --- /dev/null +++ b/algorithms/string.md @@ -0,0 +1,61 @@ +String +== + +- Output list of strings representing a page of hostings given a list of CSV strings. +- Given a list of words, find the word pairs that when concatenated form a palindrome. +- Find the most efficient way to identify what character is out of place in a non-palindrome. +- Implement a simple regex parser which, given a string and a pattern, returns a boolean indicating whether the input matches the pattern. By simple, we mean that the regex can only contain special character: `*` (star), `.` (dot), `+` (plus). The star means that there will be zero or more of previous character in that place in the pattern. The dot means any character for that position. The plus means one or more of previous character in that place in the pattern. +- Find all words from a dictionary that are x edit distance away. +- Given a string IP and number n, print all CIDR addresses that cover that range. +- Write a function called `eval`, which takes a string and returns a boolean. This string is allowed 6 different characters: `0`, `1`, `&`, `|`, `(`, and `)`. `eval` should evaluate the string as a boolean expression, where `0` is `false`, `1` is `true`, `&` is an `and`, and `|` is an `or`. + - E.g `"(0 | (1 | 0)) & (1 & ((1 | 0) & 0))"` +- Given a pattern string like `"abba"` and an input string like `"redbluebluered"`, return `true` if and only if there's a one to one mapping of letters in the pattern to substrings of the input. + - E.g. `"abba"` and `"redblueredblue"` should return `true`. + - E.g. `"aaaa"` and `"asdasdasdasd"` should return `true`. + - E.g. `"aabb"` and `"xyzabcxzyabc"` should return `false`. +- If you received a file in chunks, calculate when you have the full file. Quite an open-ended question. Can assume chunks come with start and end, or size, etc. +- Given a list of names (strings) and the width of a line, design an algorithm to display them using the minimum number of lines. +- Design a spell-checking algorithm. +- Count and say problem. +- Longest substring with `K` unique characters. + - [Source](http://blog.gainlo.co/index.php/2016/04/12/find-the-longest-substring-with-k-unique-characters/) +- Given a set of random strings, write a function that returns a set that groups all the anagrams together. + - [Source](http://blog.gainlo.co/index.php/2016/05/06/group-anagrams/) +- Given a string, find the longest substring without repeating characters. For example, for string `'abccdefgh'`, the longest substring is `'cdefgh'`. + - [Source](http://blog.gainlo.co/index.php/2016/10/07/facebook-interview-longest-substring-without-repeating-characters/) +- Given a string, return the string with duplicate characters removed. +- How many string representations are there for an integer where `a->1, b->2, ... z->26`. + - E.g. `126` can be `'az'` or `'abf'`. +- Write a function that receives a regular expression (allowed chars = from `'a'` to `'z'`, `'*'`, `'.'`) and a string containing lower case english alphabet characters and return `true` or `false` whether the string matches the regex. + - E.g. `'ab*a'`, `'abbbbba'` => `true`. + - E.g. `'ab*b.'`, `'aba'` => `true`. + - E.g. `'abc*'`, `'acccc'` => `false`. +- Given a rectangular grid with letters, search if some word is in the grid. +- Given two strings representing integer numbers (`'123'` , `'30'`) return a string representing the sum of the two numbers: `'153'`. +- Given a really big file with a lots of Facebook posts, find the ten most-used words. +- A professor wants to see if two students have cheated when writing a paper. Design a function `hasCheated(String s1, String s2, int N)` that evaluates to `true` if two strings have a common substring of length `N`. + - Follow up: Assume you don't have the possibility of using `String.contains()` and `String.substring()`. How would you implement this? +- Print all permutations of a given string. +- Parse a string containing numbers and `'+'`, `'-'` and parentheses. Evaluate the expression. `-2+(3-5)` should return `-4`. +- Output a substring with at most `K` unique characters. + - E.g. `'aabc'` and `k` = 2 => `'aab'`. +- Ensure that there are a minimum of `N` dashes between any two of the same characters of a string. + - E.g. `n = 2, string = 'ab-bcdecca'` => `'ab--bcdec--ca'`. +- Find the longest palindrome in a string. +- Give the count and the number following in the series. + - E.g. `1122344`, next: `21221324`, next: `12112211121214`. + - Count and say problem. +- Compress a string by grouping consecutive similar questions together: + - E.g. `'aaabbbcc' => `'a3b3c2'`. +- You have a string consisting of open and closed parentheses, but parentheses may be imbalanced. Make the parentheses balanced and return the new string. +- Given a set of strings, return the smallest subset that contains prefixes for every string. + - E.g. `['foo', 'foog', 'food', 'asdf']` => `['foo', 'asdf']`. +- Write a function that would return all the possible words generated when using a phone (pre-smartphone era) numpad to type. +- Given a dictionary and a word, find the minimum number of deletions needed on the word in order to make it a valid word. + - [Source](http://blog.gainlo.co/index.php/2016/04/29/minimum-number-of-deletions-of-a-string/) +- How to check if a string contains an anagram of another string? + - [Source](http://blog.gainlo.co/index.php/2016/04/08/if-a-string-contains-an-anagram-of-another-string/) +- Find all k-lettered words from a string. +- Given a string of open and close parentheses, find the minimum number of edits needed to balance a string of parentheses. +- Run length encoding - Write a string compress function that returns `'R2G1B1'` given `'RRGB'`. +- Write a function that finds all the different ways you can split up a word into a concatenation of two other words. diff --git a/algorithms/topics.md b/algorithms/topics.md new file mode 100644 index 00000000..9718e7e7 --- /dev/null +++ b/algorithms/topics.md @@ -0,0 +1,89 @@ +Topics +== + +## Arrays + +## Strings + +- Prefix trees (Tries) +- Suffix trees +- Suffix arrays +- KMP +- Rabin-Karp +- Boyer-Moore + +## Sorting + +- Bubble sort +- Insertion sort +- Merge sort +- Quick sort +- Selection sort +- Bucket sort +- Radix sort +- Counting sort + +## Linked Lists + +## Stacks + +## Queues + +## Hash tables + +- Collision resolution algorithms + +## Trees + +- BFS +- DFS (inorder, postorder, preorder) +- Height + +## Binary Search Trees + +- Insert node +- Delete a node +- Find element in BST +- Find min, max element in BST +- Get successor element in tree +- Check if a binary tree is a BST or not + +## Heaps / Priority Queues + +- Insert +- Bubble up +- Extract max +- Remove +- Heapify +- Heap sort + +## Graphs + +- Various implementations + - Adjacency matrix + - Adjacency list + - Adjacency map +- Single-source shortest path +- Dijkstra +- Bellman-Ford +- Topo sort +- MST +- Prim algorithm +- Kruskal's algorithm +- Union Find Data Structure +- Count connected components in a graph +- List strongly connected components in a graph +- Check for bipartite graph + +## Dynamic Programming + +- Count Change +- 0-1 Knapsack + +## System Design + +- http://www.hiredintech.com/system-design/ +- https://www.quora.com/How-do-I-prepare-to-answer-design-questions-in-a-technical-interview?redirected_qid=1500023 +- http://blog.gainlo.co/index.php/2015/10/22/8-things-you-need-to-know-before-system-design-interviews/ +- https://github.com/donnemartin/system-design-primer +- https://github.com/jwasham/coding-interview-university/blob/master/extras/cheat%20sheets/system-design.pdf diff --git a/algorithms/tree.md b/algorithms/tree.md new file mode 100644 index 00000000..12a62bc8 --- /dev/null +++ b/algorithms/tree.md @@ -0,0 +1,37 @@ +Tree +== + +- Find the height of a tree. +- Find the longest path from the root to leaf in a tree. +- Find the deepest left leaf of a tree. +- Print all paths of a binary tree. + - [Source](http://blog.gainlo.co/index.php/2016/04/15/print-all-paths-of-a-binary-tree/) +- Second largest element of a BST. + - [Source](http://blog.gainlo.co/index.php/2016/06/03/second-largest-element-of-a-binary-search-tree/) +- Given a binary tree and two nodes, how to find the common ancestor of the two nodes? + - [Source](http://blog.gainlo.co/index.php/2016/07/06/lowest-common-ancestor/) +- Find the lowest common ancestor of two nodes in a binary search tree. +- Print the nodes in an n-ary tree level by level, one printed line per level. +- Given a directory of files and folders (and relevant functions), how would you parse through it to find equivalent files? +- Write a basic file system and implement the commands ls, pwd, mkdir, create, rm, cd, cat, mv. +- Compute the intersection of two binary search trees. +- Given a tree, where the parent has any number of nodes and each node has a number, return the average of all the nodes on each level in an array. +- Given a binary tree, output all the node to leaf paths of it. +- Given a string of characters without spaces, is there a way to break the string into valid words without leftover characters? +- Print a binary tree level by level. +- Determine if a binary tree is "complete" (i.e, if all leaf nodes were either at the maximum depth or max depth-1, and were 'pressed' along the left side of the tree). +- Find the longest path in a binary tree. The path may start and end at any node. +- Determine if a binary tree is a BST. +- Given a binary tree, serialize it into a string. Then deserialize it. +- Print a binary tree by column. +- Given a node, find the next element in a BST. +- Find the shortest subtree that consist of all the deepest nodes. The tree is not binary. +- Print out the sum of each row in a binary tree. +- Pretty print a JSON object. +- Convert a binary tree to a doubly circular linked list. +- Find the second largest number in a binary tree. +- Given a tree, find the longest branch. +- Convert a tree to a linked list. +- Given two trees, write code to find out if tree A is a subtree of tree B. +- Deepest node in a tree. + - [Source](http://blog.gainlo.co/index.php/2016/04/26/deepest-node-in-a-tree/) diff --git a/design/README.md b/design/README.md new file mode 100644 index 00000000..7cdbc88e --- /dev/null +++ b/design/README.md @@ -0,0 +1,94 @@ +Design Questions +== + +## Guides + +- https://github.com/donnemartin/system-design-primer +- https://github.com/checkcheckzz/system-design-interview +- https://github.com/shashank88/system_design +- https://gist.github.com/vasanthk/485d1c25737e8e72759f +- http://www.puncsky.com/blog/2016/02/14/crack-the-system-design-interview/ +- https://www.palantir.com/2011/10/how-to-rock-a-systems-design-interview/ +- http://blog.gainlo.co/index.php/2017/04/13/system-design-interviews-part-ii-complete-guide-google-interview-preparation/ + +## Flow + +#### A. Understand the problem and scope + +- Define the use cases, with interviewer's help. +- Suggest additional features. +- Remove items that interviewer deems out of scope. +- Assume high availability is required, add as a use case. + +#### B. Think about constraints + +- Ask how many requests per month. +- Ask how many requests per second (they may volunteer it or make you do the math). +- Estimate reads vs. writes percentage. +- Keep 80/20 rule in mind when estimating. +- How much data written per second. +- Total storage required over 5 years. +- How much data reads per second. + +#### C. Abstract design + +- Layers (service, data, caching). +- Infrastructure: load balancing, messaging. +- Rough overview of any key algorithm that drives the service. +- Consider bottlenecks and determine solutions. + +Source: https://github.com/jwasham/coding-interview-university#system-design-scalability-data-handling + +## Grading Rubrics + +- Problem Solving - How systematic is your approach to solving the problem step-by-step? Break down a problem into its core components. +- Communication - How well do you explain your idea and communicate it with others? +- Evaluation - How do you evaluate your system? Are you aware of the trade-offs made? How can you optimize it? +- Estimation - How fast does your system need to be? How much space does it need? How much load will it experience? + +## Specific Topics + +- URL Shortener + - http://stackoverflow.com/questions/742013/how-to-code-a-url-shortener + - http://blog.gainlo.co/index.php/2016/03/08/system-design-interview-question-create-tinyurl-system/ + - https://www.interviewcake.com/question/python/url-shortener +- Collaborative Editor + - http://blog.gainlo.co/index.php/2016/03/22/system-design-interview-question-how-to-design-google-docs/ +- Photo Sharing App + - http://blog.gainlo.co/index.php/2016/03/01/system-design-interview-question-create-a-photo-sharing-app/ +- Social Network Feed + - http://blog.gainlo.co/index.php/2016/02/17/system-design-interview-question-how-to-design-twitter-part-1/ + - http://blog.gainlo.co/index.php/2016/02/24/system-design-interview-question-how-to-design-twitter-part-2/ + - http://blog.gainlo.co/index.php/2016/03/29/design-news-feed-system-part-1-system-design-interview-questions/ +- Trending Algorithm + - http://blog.gainlo.co/index.php/2016/05/03/how-to-design-a-trending-algorithm-for-twitter/ +- Facebook Chat + - http://blog.gainlo.co/index.php/2016/04/19/design-facebook-chat-function/ +- Key Value Store + - http://blog.gainlo.co/index.php/2016/06/14/design-a-key-value-store-part-i/ + - http://blog.gainlo.co/index.php/2016/06/21/design-key-value-store-part-ii/ +- Recommendation System + - http://blog.gainlo.co/index.php/2016/05/24/design-a-recommendation-system/ +- Cache System + - http://blog.gainlo.co/index.php/2016/05/17/design-a-cache-system/ +- E-commerce Website + - http://blog.gainlo.co/index.php/2016/08/22/design-ecommerce-website-part/ + - http://blog.gainlo.co/index.php/2016/08/28/design-ecommerce-website-part-ii/ +- Web Crawler + - http://blog.gainlo.co/index.php/2016/06/29/build-web-crawler/ + - http://www.makeuseof.com/tag/how-do-search-engines-work-makeuseof-explains/ + - https://www.quora.com/How-can-I-build-a-web-crawler-from-scratch/answer/Chris-Heller +- YouTube + - http://blog.gainlo.co/index.php/2016/10/22/design-youtube-part/ + - http://blog.gainlo.co/index.php/2016/11/04/design-youtube-part-ii/ +- Hit Counter + - http://blog.gainlo.co/index.php/2016/09/12/dropbox-interview-design-hit-counter/ +- Facebook Graph Search +- Design [Lyft Line](https://www.lyft.com/line). +- Design a promo code system (with same promo code, randomly generated promo code, and promo code with conditions). +- Model a university. +- How would you implement Pacman? +- Sketch out an implementation of Asteroids. +- Implement a spell checker. +- Design the rubik cube. +- Design a high-level interface to be used for card games (e.g. poker, blackjack etc). diff --git a/design/collaborative-editor.md b/design/collaborative-editor.md new file mode 100644 index 00000000..2f9427c1 --- /dev/null +++ b/design/collaborative-editor.md @@ -0,0 +1,108 @@ +Collaborative Document Editor +== + +## Variants + +- Design Google docs. +- Design a collaborative code editor like Coderpad/Codepile. +- Design a collaborative markdown editor. + +## Requirements Gathering + +- What is the intended platform? + - Web +- What features are required? + - Creating a document + - Editing a document + - Sharing a document +- Bonus features + - Document revisions and reverting + - Searching + - Commenting + - Chatting + - Executing code (in the case of code editor) +- What is in a document? + - Text + - Images +- Which metrics should we optimize for? + - Loading time + - Synchronization + - Throughput + +## Core Components + +- Front end + - WebSockets/long polling for real-time communication between front end and back end. +- Back end services behind a reverse proxy. + - Reverse proxy will proxy the requests to the right server. + - Split into a few services for different purposes. + - The benefit of this is that each service can use different languages that best suits its purpose. +- API servers for non-collaborative features and endpoints. + - Ruby/Rails/Django for the server that deals with CRUD operations on data models where performance is not that crucial. +- WebSocket servers for handling document edits and publishing updates to listeners. + - Possibly Node/Golang for WebSocket server which will need high performance as updates are frequent. +- Task queue to persist document updates to the database. +- ELB in front of back end servers. +- MySQL database. +- S3 and CDN for images. + +## Data Modeling + +- What kind of database to use? + - Data is quite structured. Would go with SQL. +- Design the necessary tables, its columns and its relations. + - `users` + - `id` + - `name` + - `document` + - `id` + - `owner_id` + - `permissions` + - `id` + - `name` + - `document_permissions` + - `id` + - `document_id` + - `user_id` + +## Collaborative Editing - Client + +- Upon loading of the page and document, the client should connect to the WebSocket server over the WebSocket protocol `ws://`. +- Upon connection, perform a time sync with the server, possibly via Network Time Protocol (NTP). +- The most straightforward way is to send the whole updated document content to the back end, and all users currently viewing the document will receive the updated document. However, there are a few problems with this approach: + - Race condition. If two users editing the document at the same time, the last one to edit will overwrite the changes by the previous user. One workaround is to lock the document when a user is currently editing it, but that will not make it real-time collaborative. + - A large payload (the whole document) is being sent to servers and published to users on each change, and the user is likely to already have most of the content. A lot of redundant data being sent. +- A feasible approach would be to use operational transforms and send just the action deltas to the back end. The back end publishes the action deltas to the listeners. What is considered an action delta? + - (a) Changing a character/word, (b) inserting a character/word/image, (c) deleting a character/word. + - With this approach, the payload will contain only small amount of data, such as (a) type of change, (b) character/word, (c) position in document: line/column, (d) timestamp. Why is the timestamp needed? Read on to find out. +- Updates can also be throttled and batched, to avoid flooding the web server with requests. For example, if a user inserts a + +## Back End + +The back end is split into a few portions: WebSocket server for receiving and broadcasting document updates, CRUD server for reading and writing non-document-related data, and a task queue for persistence of the document. + +## WebSocket Server + +- Languages and frameworks that support async requests and non-blocking I/O will be suitable for the collaborative editor server. Node and Golang comes to my mind. +- However, the WebSocket server is not stateless, so is it not that straightforward to scale horizontally. One approach would be for a Load Balancer to use Redis to maintain a map of the client to the WebSocket server instance IP, such that subsequent requests from the same client will be routed to the same server. +- Each document corresponds to a room (more of namespace). Users can subscribe to the events happening within a room. +- When a action delta is being received, blast it out to the listeners within the room and add it to the task queue. + +## CRUD Server + +- Provides APIs for reading and writing non-document-related data, such as users, permissions. + +## Task Queue + Worker Service + +- Worker service retrieves messages from the task queue and writes the updated documents to the database in an async fashion. +- Batch the actions together and perform one larger write that consists of multiple actions. For example, instead of persisting to the database once per addition of a word, combine these additions and write them into the database at once. +- Publish the save completion event to the WebSocket server to be published to the listeners, informing that the latest version of the document is being saved. +- Benefit of using a task queue is that as the amount of tasks in the queue goes up, we can scale up the number of worker services to clear the backlog of work faster. + +## Document Persistence + +TODO + +###### References + +- http://blog.gainlo.co/index.php/2016/03/22/system-design-interview-question-how-to-design-google-docs/ diff --git a/design/news-feed.md b/design/news-feed.md new file mode 100644 index 00000000..c64114ff --- /dev/null +++ b/design/news-feed.md @@ -0,0 +1,164 @@ +News Feed +== + +## Variants + +- Design Facebook news feed. +- Design Twitter news feed. +- Design Quora feed. +- Design Instagram feed. + +## Requirements Gathering + +- What is the intended platform? + - Mobile (mobile web or native)? Web? Desktop? +- What features are required? + - CRUD posts. + - Commenting on posts. + - Sharing posts. + - Trending posts? + - Tag people? + - Hashtags? +- What is in a news feed post? + - Author. + - Content. + - Media. + - Tags? + - Hashtags? + - Comments/Replies. + - Operations: + - CRUD + - Commenting/replying to a post. +- What is in a news feed? + - Sequence of posts. + - Query pattern: query for a user's ranked news feed. + - Operations: + - Append - Fetch more posts. + - Delete - I don't want to see this. +- Which metrics should we optimize for? + - User retention. + - Ads revenue. + - Fast loading time. + - Bandwidth. + - Server costs. + +## Core Components + +TODO + +## Data modeling + +- What kind of database to use? + - Data is quite structured. Would go with SQL. +- Design the necessary tables, its columns and its relations. + - `users` + - `posts` + - `likes` + - `follows` + - `comments` + +> There are two basic objects: user and feed. For user object, we can store userID, name, registration date and so on so forth. And for feed object, there are feedId, feedType, content, metadata etc., which should support images and videos as well. +> +> If we are using a relational database, we also need to model two relations: user-feed relation and friend relation. The former is pretty straightforward. We can create a user-feed table that stores userID and corresponding feedID. For a single user, it can contain multiple entries if he has published many feeds. +> +> For friend relation, adjacency list is one of the most common approaches. If we see all the users as nodes in a giant graph, edges that connect nodes denote friend relation. We can use a friend table that contains two userIDs in each entry to model the edge (friend relation). By doing this, most operations are quite convenient like fetch all friends of a user, check if two people are friends. +> +> The system will first get all userIDs of friends from friend table. Then it fetches all feedIDs for each friend from user-feed table. Finally, feed content is fetched based on feedID from feed table. You can see that we need to perform 3 joins, which can affect performance. +> +> A common optimization is to store feed content together with feedID in user-feed table so that we don’t need to join the feed table any more. This approach is called denormalization, which means by adding redundant data, we can optimize the read performance (reducing the number of joins). +> +> The disadvantages are obvious: +> - Data redundancy. We are storing redundant data, which occupies storage space (classic time-space trade-off). +> - Data consistency. Whenever we update a feed, we need to update both feed table and user-feed table. Otherwise, there is data inconsistency. This increases the complexity of the system. +> - Remember that there’s no one approach always better than the other (normalization vs denormalization). It’s a matter of whether you want to optimize for read or write. + +## Feed Display + +- The most straightforward way is to fetch posts from all the people you follow and render them sorted by time. +- There can be many posts to fetch. How many posts should you fetch? + - What are the pagination approaches and the pros and cons of each approach? + - Offset by page size + - Offset by time +- What data should the post contain when you initially fetch them? +- Lazy loading approach for loading associated data: media, comments, people who liked the post. +- Media + - If the post contains media such as images and videos, how should they be handled? Should they be loaded on the spot? + - A better way would be to fetch images only when they are about to enter the viewport. + - Videos should not autoplay. Only fetch the thumbnail for the video, and only play the video when user clicks play. + - If the content is being refetched, the media should be cached and not fetched over the wire again. This is especially important on mobile connections where data can be expensive. +- Comments + - Should you fetch all the comments for a post? For posts by celebrities, they can contain a few hundred or thousand comments. + - Maybe fetch the top few comments and display them under the post, and the user is given the choice to "show all comments". +- How does the user request for new content? + - Infinite scrolling. + - User has to tap next page. + +## Feed Ranking + +- First select features/signals that are relevant and then figure out how to combine them to calculate a final score. +- How do you show the relevant posts that the user is interested in? + - Chronological - While a chronological approach works, it may not be the most engaging approach. For example, if a person posts 30 times within the last hour, his followers will have their news feed clogged up with his posts. Maybe set a cap on the number of time a person's posts can appear within the feed. + - Popularity - How many likes and comments does the post have? Does the user usually like posts by that person? +- How do you determine which are the more important posts? A user might be more interested in a few-hour old post from a good friend than a very recent post from an acquaintance. +- A common strategy is to calculate a post score based on various features and rank posts by its score. +- Prior to 2013, Facebook was using the [EdgeRank](https://www.wikiwand.com/en/EdgeRank) algorithm to determine what articles should be displayed in a user's News Feed. +- Edge Rank basically is using three signals: affinity score, edge weight and time decay. + - Affinity score (u) - For each news feed, affinity score evaluates how close you are with this user. For instance, you are more likely to care about feed from your close friends instead of someone you just met once. + - Edge weight (e) - Edge weight basically reflects importance of each edge. For instance, comments are worth more than likes. + - Time decay (d) - The older the story, the less likely users find it interesting. +- Affinity score + - Various factors can be used to reflect how close two people are. First of all, explicit interactions like comment, like, tag, share, click etc. are strong signals we should use. Apparently, each type of interaction should have different weight. For instance, comments should be worth much more than likes. + - Secondly, we should also track the time factor. Perhaps you used to interact with a friend quite a lot, but less frequent recently. In this case, we should lower the affinity score. So for each interaction, we should also put the time decay factor. +- A good ranking system can improve some core metrics - user retention, ads revenue, etc. + +## Feed Publishing + +TODO. Refer to http://blog.gainlo.co/index.php/2016/04/05/design-news-feed-system-part-2/. + +## Additional Features + +#### Tagging feature + +- Have a `tags` table that stores the relation between a post and the people tagged in it. + +#### Sharing feature + +- Add a column to `posts` table called `original_post_id`. +- What should happen when the original post is deleted? + - The shared `posts` have to be deleted too. + +#### Notifications feature + +- When should notifications happen? +- Can the user subscribe to only certain types of notifications? + +#### Trending feature + +- What constitutes trending? What signals would you look at? What weight would you give to each signal? +- Most frequent hashtags over the last N hours. +- Hottest search queries. +- Fetch the recent most popular feeds and extract some common words or phrases. + +#### Search feature + +- How would you index the data? + +## Scalability + +- Master-slave replication. + - Write to master database and read from replica databases/in-memory data store. + - Post contents are being read more than they are updated. It is acceptable to have a slight lag between a user updating a post and followers seeing the updated content. Tweets are not even editable. +- Data for real-time queries should be in memory, disk is for writes only. +- Pre-computation offline. +- Tracking number of likes and comments. + - Expensive to do a `COUNT` on the `likes` and `comments` for a post. + - Use Redis/Memcached for keeping track of how many likes/comments a post has. Increment when there's new activity, decrement when someone unlikes/deletes the comment. +- Load balancer in front of your API servers. +- Partitioning the data. + +###### References + +- [Design News Feed System (Part 1)](http://blog.gainlo.co/index.php/2016/03/29/design-news-feed-system-part-1-system-design-interview-questions/) +- [Design News Feed System (Part 1)](http://blog.gainlo.co/index.php/2016/04/05/design-news-feed-system-part-2/) +- [Etsy Activity Feeds Architecture](https://www.slideshare.net/danmckinley/etsy-activity-feeds-architecture) +- [Big Data in Real-Time at Twitter](https://www.slideshare.net/nkallen/q-con-3770885) diff --git a/design/search-engine.md b/design/search-engine.md new file mode 100644 index 00000000..4703313b --- /dev/null +++ b/design/search-engine.md @@ -0,0 +1,6 @@ +Search Engine +== + +###### References + +- [How Do Search Engines Work?](http://www.makeuseof.com/tag/how-do-search-engines-work-makeuseof-explains/) diff --git a/domain/async-loading/index.html b/domain/async-loading/index.html new file mode 100644 index 00000000..d422c0c6 --- /dev/null +++ b/domain/async-loading/index.html @@ -0,0 +1,61 @@ + + + + + +
+
+
+
+ + + diff --git a/domain/databases.md b/domain/databases.md new file mode 100644 index 00000000..80b17257 --- /dev/null +++ b/domain/databases.md @@ -0,0 +1,8 @@ +Databases +== + +## General + +- How should you store passwords in a database? + - http://www.geeksforgeeks.org/store-password-database/ + - https://nakedsecurity.sophos.com/2013/11/20/serious-security-how-to-store-your-users-passwords-safely/ diff --git a/domain/networking.md b/domain/networking.md new file mode 100644 index 00000000..cc56fc9d --- /dev/null +++ b/domain/networking.md @@ -0,0 +1,6 @@ +Networking +== + +- Given an IPv4 IP address p and an integer n, return a list of CIDR strings that most succinctly represents the range of IP addresses from p to (p + n). +- Describe what happens when you enter a url in the web browser. +- Define UDP/TCP and give an example of both. diff --git a/domain/pagination-sorting/data.js b/domain/pagination-sorting/data.js new file mode 100644 index 00000000..6001ce83 --- /dev/null +++ b/domain/pagination-sorting/data.js @@ -0,0 +1,677 @@ +const data = [ + { + "_id": "591ad7e2d1b6119887420af5", + "age": 36, + "name": "Salas Mccarthy", + "gender": "male", + "company": "MAKINGWAY", + "email": "salasmccarthy@makingway.com", + "phone": "+1 (905) 546-3931" + }, + { + "_id": "591ad7e2918b0ba1231b582d", + "age": 29, + "name": "Jodi Graham", + "gender": "female", + "company": "BITREX", + "email": "jodigraham@bitrex.com", + "phone": "+1 (861) 596-2691" + }, + { + "_id": "591ad7e2bc19d181dcb64d57", + "age": 34, + "name": "Hollie Hardin", + "gender": "female", + "company": "ZENSURE", + "email": "holliehardin@zensure.com", + "phone": "+1 (822) 410-2433" + }, + { + "_id": "591ad7e224d2fa7216132561", + "age": 26, + "name": "Bullock Cole", + "gender": "male", + "company": "ZILPHUR", + "email": "bullockcole@zilphur.com", + "phone": "+1 (839) 563-3350" + }, + { + "_id": "591ad7e206c3fb3043ccf8a8", + "age": 30, + "name": "Peterson Mosley", + "gender": "male", + "company": "MOBILDATA", + "email": "petersonmosley@mobildata.com", + "phone": "+1 (974) 547-3461" + }, + { + "_id": "591ad7e20a180de908b0960e", + "age": 34, + "name": "Lucille Jackson", + "gender": "female", + "company": "KINETICUT", + "email": "lucillejackson@kineticut.com", + "phone": "+1 (845) 443-3594" + }, + { + "_id": "591ad7e2768b0cf4837ecf35", + "age": 40, + "name": "Cooper Leonard", + "gender": "male", + "company": "CHORIZON", + "email": "cooperleonard@chorizon.com", + "phone": "+1 (817) 418-2290" + }, + { + "_id": "591ad7e270a640cf8f8a3e44", + "age": 27, + "name": "Iris Shepherd", + "gender": "female", + "company": "IDEALIS", + "email": "irisshepherd@idealis.com", + "phone": "+1 (858) 599-3628" + }, + { + "_id": "591ad7e222ddd244aaba0315", + "age": 31, + "name": "Oneil Head", + "gender": "male", + "company": "LIMOZEN", + "email": "oneilhead@limozen.com", + "phone": "+1 (894) 440-3396" + }, + { + "_id": "591ad7e2d36847b5e648587b", + "age": 28, + "name": "Swanson Singleton", + "gender": "male", + "company": "NSPIRE", + "email": "swansonsingleton@nspire.com", + "phone": "+1 (817) 510-2480" + }, + { + "_id": "591ad7e2b78092f9fb866779", + "age": 37, + "name": "Sutton Odom", + "gender": "male", + "company": "GLUKGLUK", + "email": "suttonodom@glukgluk.com", + "phone": "+1 (961) 424-2812" + }, + { + "_id": "591ad7e222736aa66a9b35cc", + "age": 31, + "name": "Chandler Kirk", + "gender": "male", + "company": "EWAVES", + "email": "chandlerkirk@ewaves.com", + "phone": "+1 (902) 524-3712" + }, + { + "_id": "591ad7e2ac3f9abecd5175a7", + "age": 28, + "name": "Gentry Stanley", + "gender": "male", + "company": "GINK", + "email": "gentrystanley@gink.com", + "phone": "+1 (878) 592-2331" + }, + { + "_id": "591ad7e2f79787dc605d61fb", + "age": 28, + "name": "Paulette Guthrie", + "gender": "female", + "company": "SEQUITUR", + "email": "pauletteguthrie@sequitur.com", + "phone": "+1 (820) 473-2926" + }, + { + "_id": "591ad7e2ff61553783737bc0", + "age": 37, + "name": "Lesley Nash", + "gender": "female", + "company": "MELBACOR", + "email": "lesleynash@melbacor.com", + "phone": "+1 (849) 414-2115" + }, + { + "_id": "591ad7e26f9c3fc95fa8e6f1", + "age": 22, + "name": "Frye Oneil", + "gender": "male", + "company": "OVERPLEX", + "email": "fryeoneil@overplex.com", + "phone": "+1 (999) 412-2403" + }, + { + "_id": "591ad7e2eee8b7cd857a29d0", + "age": 40, + "name": "Christina Zamora", + "gender": "female", + "company": "REPETWIRE", + "email": "christinazamora@repetwire.com", + "phone": "+1 (860) 504-3885" + }, + { + "_id": "591ad7e2be93544c81761053", + "age": 25, + "name": "Earlene Dunn", + "gender": "female", + "company": "PHARMEX", + "email": "earlenedunn@pharmex.com", + "phone": "+1 (831) 582-2974" + }, + { + "_id": "591ad7e2cb672de927940ef4", + "age": 25, + "name": "Hoffman Pittman", + "gender": "male", + "company": "ENOMEN", + "email": "hoffmanpittman@enomen.com", + "phone": "+1 (882) 420-3186" + }, + { + "_id": "591ad7e2aabaae50d75c21a3", + "age": 35, + "name": "Wilson Tran", + "gender": "male", + "company": "CAXT", + "email": "wilsontran@caxt.com", + "phone": "+1 (842) 529-3085" + }, + { + "_id": "591ad7e2bdd6104be85bdd87", + "age": 22, + "name": "Hensley Hawkins", + "gender": "male", + "company": "LUDAK", + "email": "hensleyhawkins@ludak.com", + "phone": "+1 (902) 566-3308" + }, + { + "_id": "591ad7e24796cbdc0eacb6c0", + "age": 36, + "name": "Gray Schultz", + "gender": "male", + "company": "EBIDCO", + "email": "grayschultz@ebidco.com", + "phone": "+1 (910) 532-2845" + }, + { + "_id": "591ad7e204425cc3b121279a", + "age": 25, + "name": "Odonnell Livingston", + "gender": "male", + "company": "ANOCHA", + "email": "odonnelllivingston@anocha.com", + "phone": "+1 (991) 422-2754" + }, + { + "_id": "591ad7e2997ac59b446d3a30", + "age": 36, + "name": "Shelton Lindsay", + "gender": "male", + "company": "DENTREX", + "email": "sheltonlindsay@dentrex.com", + "phone": "+1 (899) 567-2928" + }, + { + "_id": "591ad7e2646dad2ff456b035", + "age": 40, + "name": "Mcclain Larson", + "gender": "male", + "company": "GENMOM", + "email": "mcclainlarson@genmom.com", + "phone": "+1 (934) 508-3477" + }, + { + "_id": "591ad7e21e908e847ee6d3ea", + "age": 27, + "name": "Miranda Branch", + "gender": "female", + "company": "ROCKYARD", + "email": "mirandabranch@rockyard.com", + "phone": "+1 (989) 446-2387" + }, + { + "_id": "591ad7e2d7905d2fcaa1ce18", + "age": 36, + "name": "Valencia Moreno", + "gender": "male", + "company": "KIOSK", + "email": "valenciamoreno@kiosk.com", + "phone": "+1 (807) 449-2626" + }, + { + "_id": "591ad7e245e3c4ce18a92054", + "age": 40, + "name": "Witt Leblanc", + "gender": "male", + "company": "ENERSAVE", + "email": "wittleblanc@enersave.com", + "phone": "+1 (809) 491-3886" + }, + { + "_id": "591ad7e27f2c70583d1cf34f", + "age": 32, + "name": "Glover Mccoy", + "gender": "male", + "company": "BYTREX", + "email": "glovermccoy@bytrex.com", + "phone": "+1 (928) 470-2426" + }, + { + "_id": "591ad7e22768012595c933c9", + "age": 25, + "name": "Billie Dunlap", + "gender": "female", + "company": "INQUALA", + "email": "billiedunlap@inquala.com", + "phone": "+1 (903) 415-2136" + }, + { + "_id": "591ad7e22df8d07559a29904", + "age": 21, + "name": "Bridget Hill", + "gender": "female", + "company": "TRIBALOG", + "email": "bridgethill@tribalog.com", + "phone": "+1 (954) 461-3390" + }, + { + "_id": "591ad7e201b2bddbc81ae05e", + "age": 30, + "name": "Dawson Clements", + "gender": "male", + "company": "EURON", + "email": "dawsonclements@euron.com", + "phone": "+1 (926) 570-3444" + }, + { + "_id": "591ad7e2c2aeb820e77c1d9a", + "age": 31, + "name": "Valdez Johnson", + "gender": "male", + "company": "RETRACK", + "email": "valdezjohnson@retrack.com", + "phone": "+1 (841) 506-2428" + }, + { + "_id": "591ad7e2370ff047048bd688", + "age": 31, + "name": "Nichole Dickerson", + "gender": "female", + "company": "NEOCENT", + "email": "nicholedickerson@neocent.com", + "phone": "+1 (945) 437-3591" + }, + { + "_id": "591ad7e2b77affba5a8f8603", + "age": 34, + "name": "Madden Weaver", + "gender": "male", + "company": "VERBUS", + "email": "maddenweaver@verbus.com", + "phone": "+1 (810) 598-3145" + }, + { + "_id": "591ad7e21e692cc8247b1d83", + "age": 26, + "name": "Larsen Benson", + "gender": "male", + "company": "ISOSURE", + "email": "larsenbenson@isosure.com", + "phone": "+1 (890) 435-2219" + }, + { + "_id": "591ad7e2de06284f2d2a8d19", + "age": 37, + "name": "Barnes Gaines", + "gender": "male", + "company": "RODEOCEAN", + "email": "barnesgaines@rodeocean.com", + "phone": "+1 (824) 484-2693" + }, + { + "_id": "591ad7e20d9d8224dfbd292f", + "age": 28, + "name": "Pacheco Landry", + "gender": "male", + "company": "METROZ", + "email": "pachecolandry@metroz.com", + "phone": "+1 (813) 525-3821" + }, + { + "_id": "591ad7e2c5348da6a981a148", + "age": 21, + "name": "Richard Alexander", + "gender": "male", + "company": "GADTRON", + "email": "richardalexander@gadtron.com", + "phone": "+1 (911) 513-3007" + }, + { + "_id": "591ad7e2ca9ef31ba3b67d4f", + "age": 22, + "name": "Stone Campos", + "gender": "male", + "company": "TETAK", + "email": "stonecampos@tetak.com", + "phone": "+1 (879) 440-3672" + }, + { + "_id": "591ad7e2ff5a9796c8470151", + "age": 31, + "name": "Walter Randall", + "gender": "male", + "company": "EQUITAX", + "email": "walterrandall@equitax.com", + "phone": "+1 (927) 563-3777" + }, + { + "_id": "591ad7e272c6b6c1241b3a4a", + "age": 27, + "name": "Copeland Compton", + "gender": "male", + "company": "MAXIMIND", + "email": "copelandcompton@maximind.com", + "phone": "+1 (829) 599-2574" + }, + { + "_id": "591ad7e2220c6caa9e2c444d", + "age": 30, + "name": "Lorie Irwin", + "gender": "female", + "company": "ELPRO", + "email": "lorieirwin@elpro.com", + "phone": "+1 (938) 584-2368" + }, + { + "_id": "591ad7e2942d603032f1ebda", + "age": 35, + "name": "Buck Roth", + "gender": "male", + "company": "COMTRACT", + "email": "buckroth@comtract.com", + "phone": "+1 (830) 585-3168" + }, + { + "_id": "591ad7e2c23efccd2fcd5b2e", + "age": 31, + "name": "Mamie Robinson", + "gender": "female", + "company": "ZOMBOID", + "email": "mamierobinson@zomboid.com", + "phone": "+1 (845) 481-3222" + }, + { + "_id": "591ad7e285109fadbf0f9c01", + "age": 33, + "name": "Bryan Stone", + "gender": "male", + "company": "PODUNK", + "email": "bryanstone@podunk.com", + "phone": "+1 (971) 469-3832" + }, + { + "_id": "591ad7e25abc328c2cc42283", + "age": 37, + "name": "Benson Lyons", + "gender": "male", + "company": "FUTURITY", + "email": "bensonlyons@futurity.com", + "phone": "+1 (964) 536-3522" + }, + { + "_id": "591ad7e28fa81611cdf5066a", + "age": 37, + "name": "Sally Finley", + "gender": "female", + "company": "ZYTRAC", + "email": "sallyfinley@zytrac.com", + "phone": "+1 (953) 506-2428" + }, + { + "_id": "591ad7e271c5882aa5452601", + "age": 26, + "name": "Callahan Mckee", + "gender": "male", + "company": "ACCUPRINT", + "email": "callahanmckee@accuprint.com", + "phone": "+1 (842) 400-3570" + }, + { + "_id": "591ad7e2275e787c5d9eea26", + "age": 25, + "name": "Ines Hamilton", + "gender": "female", + "company": "PROSURE", + "email": "ineshamilton@prosure.com", + "phone": "+1 (976) 485-2663" + }, + { + "_id": "591ad7e2d8790e386b671ec6", + "age": 38, + "name": "Mcknight Abbott", + "gender": "male", + "company": "MAXEMIA", + "email": "mcknightabbott@maxemia.com", + "phone": "+1 (960) 600-3920" + }, + { + "_id": "591ad7e2a1cae1e4f17d8db2", + "age": 28, + "name": "Tanisha Ross", + "gender": "female", + "company": "SYNKGEN", + "email": "tanishaross@synkgen.com", + "phone": "+1 (920) 502-3204" + }, + { + "_id": "591ad7e2f816fd6848bc03a3", + "age": 22, + "name": "Pam Webb", + "gender": "female", + "company": "VALREDA", + "email": "pamwebb@valreda.com", + "phone": "+1 (926) 442-2683" + }, + { + "_id": "591ad7e208585adb332ac653", + "age": 29, + "name": "Bobbi Mays", + "gender": "female", + "company": "PARLEYNET", + "email": "bobbimays@parleynet.com", + "phone": "+1 (940) 577-3487" + }, + { + "_id": "591ad7e2f4b29cbd1744b14e", + "age": 27, + "name": "Snider Sandoval", + "gender": "male", + "company": "GRONK", + "email": "snidersandoval@gronk.com", + "phone": "+1 (903) 526-2655" + }, + { + "_id": "591ad7e23a3f54209ec9deba", + "age": 33, + "name": "Fitzpatrick Weiss", + "gender": "male", + "company": "MICROLUXE", + "email": "fitzpatrickweiss@microluxe.com", + "phone": "+1 (894) 494-2135" + }, + { + "_id": "591ad7e229d21e1b7723c044", + "age": 27, + "name": "Kimberly Brown", + "gender": "female", + "company": "SPACEWAX", + "email": "kimberlybrown@spacewax.com", + "phone": "+1 (832) 481-2926" + }, + { + "_id": "591ad7e247e285f0c563ac94", + "age": 20, + "name": "Fisher Kent", + "gender": "male", + "company": "ORBIN", + "email": "fisherkent@orbin.com", + "phone": "+1 (962) 523-3956" + }, + { + "_id": "591ad7e274439c6eb52e99df", + "age": 26, + "name": "Arlene Carroll", + "gender": "female", + "company": "ELITA", + "email": "arlenecarroll@elita.com", + "phone": "+1 (998) 497-3751" + }, + { + "_id": "591ad7e271852e5766fad809", + "age": 22, + "name": "Tamra Spence", + "gender": "female", + "company": "ARTWORLDS", + "email": "tamraspence@artworlds.com", + "phone": "+1 (963) 516-2492" + }, + { + "_id": "591ad7e28c9165c4372744f7", + "age": 29, + "name": "Alice Goodman", + "gender": "female", + "company": "ZILLIDIUM", + "email": "alicegoodman@zillidium.com", + "phone": "+1 (829) 577-2972" + }, + { + "_id": "591ad7e2c2a9121f713f15c3", + "age": 27, + "name": "Clay Washington", + "gender": "male", + "company": "EXOSTREAM", + "email": "claywashington@exostream.com", + "phone": "+1 (926) 460-3699" + }, + { + "_id": "591ad7e2c99d41ce0347ac8f", + "age": 38, + "name": "Navarro Walsh", + "gender": "male", + "company": "ILLUMITY", + "email": "navarrowalsh@illumity.com", + "phone": "+1 (896) 564-3270" + }, + { + "_id": "591ad7e25e7c6ff0785d1337", + "age": 22, + "name": "Alexandra Hughes", + "gender": "female", + "company": "TASMANIA", + "email": "alexandrahughes@tasmania.com", + "phone": "+1 (977) 509-3599" + }, + { + "_id": "591ad7e2acb8a5785b6fd512", + "age": 40, + "name": "Vonda Oliver", + "gender": "female", + "company": "NEPTIDE", + "email": "vondaoliver@neptide.com", + "phone": "+1 (836) 595-2260" + }, + { + "_id": "591ad7e20543a9fc7bc33515", + "age": 36, + "name": "Christa Nixon", + "gender": "female", + "company": "GLASSTEP", + "email": "christanixon@glasstep.com", + "phone": "+1 (966) 551-2149" + }, + { + "_id": "591ad7e21b8e73ba4dbf7c9a", + "age": 34, + "name": "Weber Hurst", + "gender": "male", + "company": "SLUMBERIA", + "email": "weberhurst@slumberia.com", + "phone": "+1 (943) 516-3449" + }, + { + "_id": "591ad7e282b9cb56357b42db", + "age": 27, + "name": "Rasmussen Kramer", + "gender": "male", + "company": "MUSIX", + "email": "rasmussenkramer@musix.com", + "phone": "+1 (891) 553-3264" + }, + { + "_id": "591ad7e2287c587e3a04e80c", + "age": 30, + "name": "Mayra Beasley", + "gender": "female", + "company": "BRAINCLIP", + "email": "mayrabeasley@brainclip.com", + "phone": "+1 (958) 576-2642" + }, + { + "_id": "591ad7e26245135989367fa8", + "age": 20, + "name": "Nunez Ortiz", + "gender": "male", + "company": "KYAGURU", + "email": "nunezortiz@kyaguru.com", + "phone": "+1 (906) 591-3563" + }, + { + "_id": "591ad7e23248cee3b84bde40", + "age": 39, + "name": "Michael Foreman", + "gender": "male", + "company": "DANCITY", + "email": "michaelforeman@dancity.com", + "phone": "+1 (848) 400-3539" + }, + { + "_id": "591ad7e23ae236a1d59d07ee", + "age": 36, + "name": "Virgie Wheeler", + "gender": "female", + "company": "XIIX", + "email": "virgiewheeler@xiix.com", + "phone": "+1 (831) 400-2814" + }, + { + "_id": "591ad7e2d8b9e3c0c31d770d", + "age": 31, + "name": "Levy Hoover", + "gender": "male", + "company": "CANDECOR", + "email": "levyhoover@candecor.com", + "phone": "+1 (843) 428-2780" + }, + { + "_id": "591ad7e2badb7200ca06a483", + "age": 21, + "name": "Battle Stanton", + "gender": "male", + "company": "CALCULA", + "email": "battlestanton@calcula.com", + "phone": "+1 (861) 522-3951" + }, + { + "_id": "591ad7e27fd1a4455ccf406c", + "age": 24, + "name": "Church Lynn", + "gender": "male", + "company": "DAISU", + "email": "churchlynn@daisu.com", + "phone": "+1 (896) 425-3854" + } +] diff --git a/domain/pagination-sorting/index.html b/domain/pagination-sorting/index.html new file mode 100644 index 00000000..09e61f4d --- /dev/null +++ b/domain/pagination-sorting/index.html @@ -0,0 +1,144 @@ + + + + + +

People

+ + + + + + + + +
NameAgeEmail
+ + Page / + + + + + + diff --git a/domain/security.md b/domain/security.md new file mode 100644 index 00000000..1c5216ae --- /dev/null +++ b/domain/security.md @@ -0,0 +1,67 @@ +Security +== + +## Encryption + +#### Symmetrical Encryption + +- Symmetrical encryption is a type of encryption where one key can be used to encrypt messages and also decrypt the same message. +- Symmetrical encryption is usually much less computationally expensive as compared to asymmetric encryption. +- Often called "shared secret" encryption, or "secret key" encryption. +- To use a symmetric encryption scheme, the sender and receiver must securely share a key in advance. This sharing can be done via asymmetric encryption. + +#### Asymmetric Encryption + +- A pair of keys are required: a **private key** and a **public key**. Public keys can be shared with anyone while private keys should be kept secret and known only to the owner. +- A private key can be used to decrypt a message encrypted by a public key. A successful decryption verifies that the holder possesses the private key. +- Also known as public key cryptography. + +## Public Key Infrastructure + +A public key infrastructure (PKI) is a system for the creation, storage, and distribution of digital certificates which are used to verify that a particular public key belongs to a certain entity. The PKI creates digital certificates which map public keys to entities, securely stores these certificates in a central repository and revokes them if needed. + +###### References + +- https://www.wikiwand.com/en/Public_key_infrastructure + +## SSH + +An SSH session consists of two stages, **Negotiating Encryption** and **User Authentication**. + +#### Negotiating Encryption + +The goal of this stage is for the client and server to agree upon and establish encryption to protect future communication, by generating an identical session key. One possible algorithm to generate the key is the Diffie–Hellman key exchange scheme. Each party generates a public/private key pair and exchanges the public key. After obtaining an authentic copy of each other's public keys, each party can compute a shared secret offline. + +The basis of this procedure for classic Diffie-Hellman is: + +1. Both parties agree on a large prime number, which will serve as a seed value. +1. Both parties agree on an encryption generator (typically AES), which will be used to manipulate the values in a predefined way. +1. Independently, each party comes up with another prime number which is kept secret from the other party. This number is used as the private key for this interaction (different than the private SSH key used for authentication). +1. The generated private key, the encryption generator, and the shared prime number are used to generate a public key that is derived from the private key, but which can be shared with the other party. +1. Both participants then exchange their generated public keys. +1. The receiving entity uses their own private key, the other party's public key, and the original shared prime number to compute a shared secret key. +1. Although this is independently computed by each party, using opposite private and public keys, it will result in the same shared secret key. +1. The shared secret is then used to encrypt all communication that follows. + +The purpose of the shared secret key is to wrap all further communication in an encrypted tunnel that cannot be deciphered by outsiders. + +#### User Authentication + +The goal of this stage is to authenticate the user and discover whether access to the server should be granted. There are two approaches for authenticating, either by using passwords, or SSH key pairs. + +For password authentication, the server simply prompts the client for the password of the account they are attempting to login with. The password is sent through the negotiated encryption, so it is secure from outside parties. + +Authentication using SSH key pairs begins after the symmetric encryption has been established as described in the last section. The procedure happens like this: + +1. The client begins by sending an ID for the key pair it would like to authenticate with to the server. +1. The server check's the `authorized_keys` file of the account that the client is attempting to log into for the key ID. +1. If a public key with matching ID is found in the file, the server generates a random number and uses the public key to encrypt the number. +1. The server sends the client this encrypted message. +1. If the client actually has the associated private key, it will be able to decrypt the message using that key, revealing the original number. +1. The client combines the decrypted number with the shared session key that is being used to encrypt the communication, and calculates the MD5 hash of this value. +1. The client then sends this MD5 hash back to the server as an answer to the encrypted number message. +1. The server uses the same shared session key and the original number that it sent to the client to calculate the MD5 value on its own. It compares its own calculation to the one that the client sent back. If these two values match, it proves that the client was in possession of the private key and the client is authenticated. + +###### References + +- https://www.digitalocean.com/community/tutorials/understanding-the-ssh-encryption-and-connection-process diff --git a/domain/snake-game/snake-game.md b/domain/snake-game/snake-game.md new file mode 100644 index 00000000..6f7f1fc4 --- /dev/null +++ b/domain/snake-game/snake-game.md @@ -0,0 +1,77 @@ +Snake Game +== + +Design a snake game that is to be played in web browser. + +Client: React + Redux + +Rendering: +Pixel-based graphics. Depending on the intended resolution, can divide the screen into N * M pixels. Can dynamically calculate the size of each pixel. + +Fruit: One pixel. +Snake body: One pixel width made up of connected pixels. + +Model: +{ + fruit: { + x, y + }, + snake: { + points: [(x, y), ...] # head is at index 0 + direction: north/south/east/west + } + speed: 500, + points: 0 +} + +function update() { + next_loc = points[0] + (x, y) # Depends on the direction + if (snake.points.find(next_loc) > 0) { + // die + } + let pts = snake.points; + if (!isEqual(next_loc, fruit)) { + pts = points.removeLast(); + } else { + generate_fruit(); + points++; + } + snake.points = [next_loc, ...pts]; + + // Boundary checking -> die +} + +function generate_fruit() { + // Cannot generate on my own body. + + // First approach: while on body, generate + let next_fruit_location = random_location(); + while (snake.points.find(next_fruit_location) > 0) { + next_fruit_location = random_location(); + } + fruit = next_fruit_location + + // Second approach: brute force + for (let i = 0; i < rows; i++) { + for (let j = 0; j < cols; j++) { + let point = { x: i, y: j } + if (snake.points.find(next_fruit_location) === -1) { + fruit = point + } + } + } + + // Third approach: brute force with random + const available_points = [] + for (let i = 0; i < rows; i++) { + for (let j = 0; j < cols; j++) { + let point = { x: i, y: j } + if (snake.points.find(next_fruit_location) === -1) { + available_points.push(point); + } + } + } + fruit = _.sample(available_points); +} + +setInterval(update, speed); diff --git a/domain/software-engineering.md b/domain/software-engineering.md new file mode 100644 index 00000000..dfb3251c --- /dev/null +++ b/domain/software-engineering.md @@ -0,0 +1,22 @@ +Software Engineering +== + +## What is the difference between an interface and abstract class? + +**Abstract Class** + +- For an abstract class, a method must be declared as abstract. An abstract method doesn't have an implementation. +- The Abstract methods can be declared with Access modifiers like public, internal, protected, etc. When implementing these methods in a subclass, you must define them with the same (or a less restricted) visibility. +- Abstract classes can contain variables and concrete methods. +- A class can Inherit only one Abstract class. Hence multiple inheritance is not possible for an Abstract class. +- Abstract is object-oriented. It offers the basic data an 'object' should have and/or functions it should be able to do. It is concerned with the object's basic characteristics: what it has and what it can do. Hence objects which inherit from the same abstract class share the basic characteristics (generalization). +- Abstract class establishes "is a" relation with concrete classes. + +**Interface** + +- For an interface, all the methods are abstract by default. So one cannot declare variables or concrete methods in interfaces. +- All methods declared in an interface must be public. +- Interfaces cannot contain variables and concrete methods except constants. +- A class can implement many interfaces. Hence multiple interface inheritance is possible. +- Interface is functionality-oriented. It defines functionalities an object should have. Regardless what object it is, as long as it can do these functionalities, which are defined in the interface, it's fine. It ignores everything else. An object/class can contain several (groups of) functionalities; hence it is possible for a class to implement multiple interfaces. +- Interface provides "has a" capability for classes. diff --git a/domain/tic-tac-toe/index.html b/domain/tic-tac-toe/index.html new file mode 100644 index 00000000..ade23e18 --- /dev/null +++ b/domain/tic-tac-toe/index.html @@ -0,0 +1,178 @@ + + + + + +

Tic Tac Toe

+

Current player turn:

+
+ + + + diff --git a/front-end/accessibility.md b/front-end/accessibility.md new file mode 100644 index 00000000..dd470f0e --- /dev/null +++ b/front-end/accessibility.md @@ -0,0 +1,201 @@ +Accessibility +== + +## Glossary + +- **Accessibility** - +- **WAI-ARIA** - Web Accessibility Initiative - Accessible Rich Internet Applications. Commonly shortened to ARIA. + +## What is Accessibility? + +Making sure that the content and the websites we create are usable to people with impairments or disabilities. + +## WebAIM Checklist + +The following is a checklist that contains recommendations for implementing HTML-related principles and techniques for those seeking WCAG 2.0 conformance (it is NOT the Web Content Accessibility Guidelines (WCAG) 2.0). + +- **Perceivable** - Web content is made available to the senses - sight, hearing, and/or touch. + - Text Alternatives: Provide text alternatives for any non-text content. + - Time-based Media: Provide alternatives for time-based media. + - Adaptable: Create content that can be presented in different ways (for example simpler layout) without losing information or structure. + - Distinguishable: Make it easier for users to see and hear content including separating foreground from background. +- **Operable** - Interface forms, controls, and navigation are operable. + - Keyboard Accessible: Make all functionality available from a keyboard. + - Enough Time: Provide users enough time to read and use content. + - Seizures: Do not design content in a way that is known to cause seizures. + - Navigable: Provide ways to help users navigate, find content, and determine where they are. +- **Understandable** - Content and interface are understandable. + - Readable: Make text content readable and understandable. + - Predictable: Make Web pages appear and operate in predictable ways. + - Input Assistance: Help users avoid and correct mistakes. +- **Robust** - Content can be used reliably by a wide variety of user agents, including assistive technologies. + - Compatible: Maximize compatibility with current and future user agents, including assistive technologies. + +**Source:** http://webaim.org/standards/wcag/checklist + +## Focus + +- Making sure your application has a sensible tab order is important. +- HTML forms and inputs are focusable and handle keyboard events by default. +- Focus tab order relies on the DOM order in the HTML. +- Be careful when using CSS when changing the order of elements on the screen, it can cause the order to be unintuitive and messy. +- `tabindex` attribute: + - `-1`: Not in the natural tab order, but programatically focusable using JavaScript with `focus()` method. Useful for off-screen content which later appears on screen. Children elements are **NOT** pulled out of the tab order. + - `0`: In the natural tab order and can be programatically focused. + - `1` (bigger than 1): In the natural tab order but jumped in front of the tab order regardless of where it is in the DOM. It can be considered an anti-pattern. +- Add focus behavior to interactive controls, like buttons, tabs, dropdowns, stuff that users will interactive with. +- Use skip links to allow users to skip directly to the main content without having to tab through all the navigation. +- `document.activeElement` is useful in tracking the current element that has focus on. + +## Semantics + +- Using proper labeling not only helps accessibility but it makes the element easier to target for all users! +- Use `