The author makes clever use of the fact that interviewees will have limited time to program meaningful solutions which in turn, limits the options an interviewer has. So the author covers those bases. Readers will improve their interview performance after reading this book. It will be beneficial for them even after they get offers, because its topics, such as approaches to analyzing difficult problems, writing robust code and optimizing, are all essential for high-performing coders.
Harry has been a senior software engineer at Cisco since September Over the years, he has interviewed more than candidates for different corporations. His interest lies in coding interview questions. He has written dozens of blogs on this topic. Harry's published works include a book about programming interview questions in Chinese, which was released in December with PHEI, China. He has exhaustive knowledge, experience and understanding of code related questions and interviews. Informatik Software Engineering. Data structures can be augmented to achieve efficient time complexity across different operations.
For example, a HashMap 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.
Editorial Reviews. About the Author. Harry has been a senior software engineer at Cisco since. Page 1. Page 2. Page 3. |||||||||||||||. Page 4. Page 5. Page 6. Page 7. Page 8. Page 9. Page Page Page Page Page Page Page Page
HashMaps are probably the most commonly used data structure for algorithm questions. This has worked for me at times. If you are cutting corners in your code, state that out loud to your interviewer, and explain to them what you would do outside of an interview setting no time constraints.
For example, explain that you would write a regex to parse a string rather than using split , which does not cover all cases. Arrays and strings are considered sequences a string is a sequence of characters. There are tips for dealing with both arrays and strings, which will be covered here. 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 or substring where possible.
Master the sliding window technique that applies to many substring or subarray problems. When you are given two sequences to process, it is common to have one index per sequence to traverse. For example, we use the same approach to merge two sorted arrays.
Is the array sorted or partially sorted? If it is either, some form of binary search should be possible. This 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 to sort it. For questions where summation or multiplication of a subarray is involved, pre-computation using hashing or a prefix, suffix sum, or 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 has values only from 1 to N, where N is the length of the array, negate the value at that index minus one to indicate the presence of that number. Questions involving binary representations and bitwise operations are asked sometimes. You must know how to convert a number from decimal form into binary form, and vice versa, in your chosen programming language.
Dynamic Programming DP is usually used to solve optimization problems. Alaina Kafkes has written an awesome post on tackling DP problems. You should read it. The only way to get better at DP is with practice. It takes lots of practice to recognize that a problem can be solved by DP. To optimize space, sometimes you do not have to store the entire DP table in memory. The last two values or the last two rows of the matrix will suffice.
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. Be familiar with the various graph representations and graph search algorithms, and with 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.
Some inputs look like they are trees, but they are actually graphs. Clarify this with your interviewer. In that case, you will have to handle cycles and keep a set of visited nodes when traversing. 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, and right. Hence it is important to 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 DFS on a matrix can be found here. A simple template for doing DFS on a matrix appears something like this:. Interval questions are questions that give an array of two-element arrays an interval. The two values represent a start and an end value.
Interval questions are considered to be part of the array family, but they involve some common techniques. Hence, they have their own special section.
Interval questions can be tricky for those who do not have experience with them. This is because of the sheer number of cases to consider when interval arrays overlap. Clarify with the interviewer whether [1, 2] and [2, 3] are considered overlapping intervals, because it affects how you will write your equality checks.
A common routine for interval questions is to sort the array of intervals by the start value of each interval. Be familiar with writing code to check if two intervals overlap and to merge two overlapping intervals:. Like arrays, linked lists are used to represent sequential data. The benefit of linked lists is that insertion and deletion of code from anywhere in the list is O 1 , whereas in arrays, the elements have to be shifted.
The presence of dummy nodes ensures that operations will never have be executed on the head or the tail. Dummy nodes remove the headache of writing conditional checks to deal with null pointers. Be sure to remove them at the end of the operation. Sometimes linked lists problem can be solved without additional storage.
Try to borrow ideas from the for 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. Linked lists problems share similarities with array problems. Think about how you would solve an array problem and apply it to a linked list. Be familiar with the following routines because many linked list questions make use of one or more of these routines in their solution. At the very least, mention that overflow or underflow is possible and ask whether you need to handle it.
Consider negative numbers and floating point numbers. This may sound obvious, but when you are under pressure in an interview, many obvious points go unnoticed. If the question asks to implement an operator such as power, squareroot, or division, and it is to be faster than O n , binary search is usually the approach. A matrix is a 2-dimensional array. Questions involving matrices are usually related to dynamic programming or graph traversal.
For questions involving traversal or dynamic programming, make a copy of the matrix with the same dimensions that are initialized to empty values. Use these values to store the visited state or dynamic programming table. Be familiar with this routine:. Recursion is useful for permutation, because it generates all combinations and tree-based questions. You should know 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 You may get bonus points for pointing this out to the interviewer.
Recursion will never be O 1 space complexity because a stack is involved, unless there is tail call optimization TCO. Find out if your chosen language supports TCO. Please read the above tips on sequence. They apply to strings too. Ask about input character set and case sensitivity. Usually the characters are limited to lowercase Latin characters, for example a to z. 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 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. This is because the upper bound is the range of characters, which is usually a fixed constant of The input set is just lowercase Latin characters. An anagram is word switch or word play. It is the result of re-arranging the letters of a word or phrase to produce a new word or phrase, while using all the original letters only once.
In interviews, usually we are only bothered with words without spaces in them. A palindrome is a word, phrase, number , or other sequence of characters that reads the same backward and forward, such as madam or racecar. When a question is about counting the number of palindromes, a common trick is to have two pointers that move outward, away from the middle. Note that palindromes can be even or odd length. For each middle pivot position, you need to check it twice: Once that includes the character and once without the character.
Recursion is a common approach for trees. When you notice that the subtree problem can be used to solve the entire problem, try using recursion. When using recursion, always remember to check for the base case, usually where the node is null. 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 ask candidates for the iterative approach, especially if the candidate finishes writing the recursive approach too quickly.
In-order traversal of a binary tree is insufficient to uniquely serialize a tree. Pre-order or post-order traversal is also required. Be very familiar with the properties of a BST. Validate 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. Tries are special trees prefix trees that make searching and storing strings more efficient. Tries have many practical applications, such as conducting searches and providing autocomplete. It is helpful to know these common applications so that you can easily identify when a problem can be efficiently solved using a trie.
Sometimes preprocessing a dictionary of words given in a list into a trie, will improve the efficiency of searching for a word of length k, among n words. Searching becomes O k instead of O n. Be familiar with implementing, from scratch, a Trie class and its add , remove , and search methods. If you see a top or lowest k mentioned in the question, it is usually a sign that a heap can be used to solve the problem, such as in Top K Frequent Elements. 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.
Coding interviews are tough. But fortunately, you can get better at them by studying and practicing for them, and doing mock interviews. By following these steps, you will improve your coding interview skills, and be one step closer or probably more to landing your dream job. Future updates will be posted there. Algorithms, front end and behavioral content for rocking your coding interview. Do you know that you can clap more than once? Try it and see for yourself! You can also follow me on GitHub and Twitter.
Learn Forum News. Tweet this to your followers.
At companies like Facebook and Google, the people are software engineers first, domain experts second. The breakdown of coding interviews, and how to prepare for them. Helpful tips and hints for each algorithm topic arrays, trees, dynamic programming, etc. Hide content and notifications from this user. Learn more about blocking users Block user. Learn more about reporting abuse Report abuse.
Overview Repositories 17 Projects 0 Stars 2 Followers 1. Popular repositories CodingInterviewChinese2. Learn how we count contributions. Less More. July - September zhedahht has no activity yet for this period. First issue option "tls-cipher- suites" should be "tls-cipher-suites".