Coding Interview

February 3, 2024 · View on GitHub

Algorithms study cheatsheets

Basics

Data structures

Advanced data structures

Additional

General interview tips

Clarify any assumptions you made subconsciously. Many questions are under-specified on purpose.

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 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: (1) choose a more appropriate data structure/algorithm; or (2) use more memory. The latter demonstrates a classic space vs. time tradeoff, but it is not necessarily the case that you can only achieve better speed at the expense of space. Also, note that there is often a theoretical limit to how fast your program can run (in terms of time complexity). For instance, a question that requires you to find the smallest/largest element in an unsorted array cannot run faster than O(N).

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.

Hash table is 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 common 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 may not cover all cases.

Study and practice plan

Week 1 - 4: Topical study + practice

Week 1

TopicPriorityTime required
ArrayHigh2 hours
StringHigh3 hours
Hash tableMid3 hours
RecursionMid3 hours

Week 2

TopicPriorityTime required
Sorting and searchingHigh3 hours
MatrixHigh1 hour
Linked listMid3 hours
QueueMid2 hours
StackMid2 hours

Week 3

TopicPriorityTime required
TreeHigh4 hours
GraphHigh4 hours
HeapMid3 hours
TrieMid3 hours

Week 4

TopicPriorityTime required
IntervalMid2 hours
Dynamic programmingLow4 hours
BinaryLow2 hours
MathLow1 hour
GeometryLow1 hour

Week 5 - 12: In-depth practice

To track progress: Grind 75

Week 5

ProblemDifficultyDuration
Two SumEasy15 mins
Valid ParenthesesEasy20 mins
Merge Two Sorted ListsEasy20 mins
Best Time to Buy and Sell StockEasy20 mins
Valid PalindromeEasy15 mins
Invert Binary TreeEasy15 mins
Valid AnagramEasy15 mins
Binary SearchEasy15 mins
Flood FillEasy20 mins
Lowest Common Ancestor of a Binary Search TreeEasy20 mins
Balanced Binary TreeEasy15 mins
Linked List CycleEasy20 mins

Week 6

ProblemDifficultyDuration
Implement Queue using StacksEasy20 mins
First Bad VersionEasy20 mins
Ransom NoteEasy15 mins
Climbing StairsEasy20 mins
Longest PalindromeEasy20 mins
Reverse Linked ListEasy20 mins
Majority ElementEasy20 mins
Add BinaryEasy15 mins
Diameter of Binary TreeEasy30 mins
Middle of the Linked ListEasy20 mins
Maximum Depth of Binary TreeEasy15 mins
Contains DuplicateEasy15 mins

Week 7

ProblemDifficultyDuration
Min StackMedium20 mins
Maximum SubarrayMedium20 mins
Insert IntervalMedium25 mins
01 MatrixMedium30 mins
K Closest Points to OriginMedium30 mins
Longest Substring Without Repeating CharactersMedium30 mins
3SumMedium30 mins
Binary Tree Level Order TraversalMedium20 mins
Clone GraphMedium25 mins
Evaluate Reverse Polish NotationMedium30 mins

Week 8

ProblemDifficultyDuration
Course ScheduleMedium30 mins
Implement Trie (Prefix Tree)Medium35 mins
Coin ChangeMedium25 mins
Product of Array Except SelfMedium30 mins
Validate Binary Search TreeMedium20 mins
Number of IslandsMedium25 mins
Rotting OrangesMedium30 mins
Search in Rotated Sorted ArrayMedium30 mins

Week 9

ProblemDifficultyDuration
Combination SumMedium30 mins
PermutationsMedium30 mins
Merge IntervalsMedium30 mins
Lowest Common Ancestor of a Binary TreeMedium25 mins
Time Based Key-Value StoreMedium35 mins
Accounts MergeMedium30 mins
Sort ColorsMedium25 mins
Word BreakMedium30 mins

Week 10

ProblemDifficultyDuration
Partition Equal Subset SumMedium30 mins
String to Integer (atoi)Medium25 mins
Spiral MatrixMedium25 mins
SubsetsMedium30 mins
Binary Tree Right Side ViewMedium20 mins
Longest Palindromic SubstringMedium25 mins
Unique PathsMedium20 mins
Construct Binary Tree from Preorder and Inorder TraversalMedium25 mins
Container With Most WaterMedium35 mins

Week 11

ProblemDifficultyDuration
Letter Combinations of a Phone NumberMedium30 mins
Word SearchMedium30 mins
Find All Anagrams in a StringMedium30 mins
Minimum Height TreesMedium30 mins
Task SchedulerMedium35 mins
LRU CacheMedium30 mins
Kth Smallest Element in a BSTMedium25 mins
Minimum Window SubstringHard30 mins

Week 12

ProblemDifficultyDuration
Serialize and Deserialize Binary TreeHard40 mins
Trapping Rain WaterHard35 mins
Find Median from Data StreamHard30 mins
Word LadderHard45 mins
Basic CalculatorHard40 mins
Maximum Profit in Job SchedulingHard45 mins
Merge k Sorted ListsHard30 mins
Largest Rectangle in HistogramHard35 mins

License

Copyright (c) 2017-Present Yangshun Tay