└── README.md /README.md: -------------------------------------------------------------------------------- 1 | # Leetcode Study Guide 2 | 3 | This guide aims to *comprehensively* prepare you for Leetcode (algorithms/data structures) interviews. 4 | 5 | ## Prerequisite Knowledge 6 | - Learn these data structures: array, linked list, hash table, matrix, stack, heap, graph, tree. 7 | - For each of these, what happens conceptually when you add, remove, and select an item? 8 | - Big-O notation 9 | - Know both time and space complexity 10 | - For example, a “for” loop takes O(n) time. An array takes O(n) space. 11 | - For each of the above data structures, learn the time complexity of adding, removing, and selecting an item 12 | - Sorting algorithms 13 | - Bubble sort, insertion sort, selection sort, merge sort, quick sort 14 | - Know and understand how they work, and the time and space complexity for each 15 | 16 | ## Study Guide 17 | Taken from [Danny’s Discord group](https://discord.com/channels/739845668582981683/739845892579786822/942949178055733298) & modified. 18 | 19 | **Consistency.** These problems are conceptually difficult and take time for our brains to digest. Doing 1 problem a day for a month is superior to doing 30 problems in 3 days. Also, focus on learning the problem-solving techniques, rather than memorizing specific problems. 20 | 21 | **One step at a time.** Do the Easy problems first. It might take two hours to finish your first Easy problem, or you might just give up and look at the solution. That's okay. Once you can do Easy in about 5 minutes, start on Medium. Repeat. But don't worry about Hard, you probably won't ever need it. 22 | 23 | **RTFM.** Read the docs/spec for your programming language of choice. Learn how common data structures are implemented. Learn how libraries are implemented. Learn default settings and how they might affect performance. The more expertise you have in your chosen language, the more impressed your interviewers will be. **Note: Most interviews today are done in Python, regardless of the language used on the job, because it is concise and easy to learn.** 24 | 25 | **Draw it out.** It can help to use a pen and paper to draw diagrams. (For some data structures like linked lists, drawing out diagrams **really** helps.) 26 | 27 | **Practice thinking out loud.** While you're doing coding problems, practice explaining your approach (out loud). Explain why you're making the choices you are and why you're not making other choices. 28 | 29 | **Practice iterating on your solution.** Once you solve a problem, before you look at others' solutions, look at your own solution with a critical eye. How could it be made "better"? Could it be made more readable? More efficient? Shorter? Better organized? Better variable names? Lots of these work against each other, so play around with different paths and see what feels best to you. Practice justifying your choices out loud. 30 | 31 | **Read others' solutions.** Once you submit your solution, most sites will show you other users' solutions. Don't just go to the next problem. Spend at least a few minutes reading others' solutions and understanding them. Think of whether their solution is better or worse than yours. Also, make sure that your solution has the best time and space complexity possible - sometimes this isn't obvious. 32 | 33 | **Test your code without an IDE.** Stay away from the "run my code" button when solving problems if you can. One way to be confident your code will work is to run through a test case or two (again, out loud). Pick test data that's short enough to get through in <5 minutes, but still exercises each branch of your code. Use multiple simple cases when needed. Finally, briefly discuss edge cases and how your code will handle them. For example, if the input is an array of integers, what happens if the array is empty? Or only contains one single element? What about all positives, all negatives, all 0s, max and min integers? 34 | 35 | **Analyze the runtime of your solution.** Determine the Big-O complexity of your code, for both time and space. 36 | 37 | **Time yourself and pace yourself.** Once you've done a few problems, simulate the time pressure in an interview. Most interviews will require you to completely finish the problem in about 30 minutes, but clarify this beforehand. Keep checking the clock to make sure you're pacing yourself properly. Here's how you can utilize the 30 minutes: 38 | - Spend the first 5 minutes clarifying the problem and how it should handle edge cases. 39 | - Then spend up to 15 minutes on the algorithm, discussing the approach with your interviewer. 40 | - Only once you're certain of what to write, spend 5 minutes coding the solution. 41 | - Spend the last 5 minutes on test cases and fixing bugs. 42 | 43 | 44 | ## Practice Problems 45 | Source: https://us.teamblind.com/s/OaM1orEU. Remember, do the Easy ones first. 46 | 47 | ### Array 48 | 49 | - Two Sum - https://leetcode.com/problems/two-sum/ 50 | - Best Time to Buy and Sell Stock - https://leetcode.com/problems/best-time-to-buy-and-sell-stock/ 51 | - Contains Duplicate - https://leetcode.com/problems/contains-duplicate/ 52 | - Product of Array Except Self - https://leetcode.com/problems/product-of-array-except-self/ 53 | - Maximum Subarray - https://leetcode.com/problems/maximum-subarray/ 54 | - Maximum Product Subarray - https://leetcode.com/problems/maximum-product-subarray/ 55 | - Find Minimum in Rotated Sorted Array - https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/ 56 | - Search in Rotated Sorted Array - https://leetcode.com/problems/search-in-rotated-sorted-array/ 57 | - 3Sum - https://leetcode.com/problems/3sum/ 58 | - Container With Most Water - https://leetcode.com/problems/container-with-most-water/ 59 | 60 | ### Tree 61 | 62 | - Maximum Depth of Binary Tree - https://leetcode.com/problems/maximum-depth-of-binary-tree/ 63 | - Same Tree - https://leetcode.com/problems/same-tree/ 64 | - Invert/Flip Binary Tree - https://leetcode.com/problems/invert-binary-tree/ 65 | - Binary Tree Maximum Path Sum - https://leetcode.com/problems/binary-tree-maximum-path-sum/ 66 | - Binary Tree Level Order Traversal - https://leetcode.com/problems/binary-tree-level-order-traversal/ 67 | - Serialize and Deserialize Binary Tree - https://leetcode.com/problems/serialize-and-deserialize-binary-tree/ 68 | - Subtree of Another Tree - https://leetcode.com/problems/subtree-of-another-tree/ 69 | - Construct Binary Tree from Preorder and Inorder Traversal - https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ 70 | - Validate Binary Search Tree - https://leetcode.com/problems/validate-binary-search-tree/ 71 | - Kth Smallest Element in a BST - https://leetcode.com/problems/kth-smallest-element-in-a-bst/ 72 | - Lowest Common Ancestor of BST - https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/ 73 | - Implement Trie (Prefix Tree) - https://leetcode.com/problems/implement-trie-prefix-tree/ 74 | - Add and Search Word - https://leetcode.com/problems/add-and-search-word-data-structure-design/ 75 | - Word Search II - https://leetcode.com/problems/word-search-ii/ 76 | 77 | ### Binary 78 | 79 | - Sum of Two Integers - https://leetcode.com/problems/sum-of-two-integers/ 80 | - Number of 1 Bits - https://leetcode.com/problems/number-of-1-bits/ 81 | - Counting Bits - https://leetcode.com/problems/counting-bits/ 82 | - Missing Number - https://leetcode.com/problems/missing-number/ 83 | - Reverse Bits - https://leetcode.com/problems/reverse-bits/ 84 | 85 | ### Graph 86 | 87 | - Clone Graph - https://leetcode.com/problems/clone-graph/ 88 | - Course Schedule - https://leetcode.com/problems/course-schedule/ 89 | - Pacific Atlantic Water Flow - https://leetcode.com/problems/pacific-atlantic-water-flow/ 90 | - Number of Islands - https://leetcode.com/problems/number-of-islands/ 91 | - Longest Consecutive Sequence - https://leetcode.com/problems/longest-consecutive-sequence/ 92 | - Alien Dictionary (Leetcode Premium) - https://leetcode.com/problems/alien-dictionary/ 93 | - Graph Valid Tree (Leetcode Premium) - https://leetcode.com/problems/graph-valid-tree/ 94 | - Number of Connected Components in an Undirected Graph (Leetcode Premium) - https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 95 | 96 | ### Interval 97 | 98 | - Insert Interval - https://leetcode.com/problems/insert-interval/ 99 | - Merge Intervals - https://leetcode.com/problems/merge-intervals/ 100 | - Non-overlapping Intervals - https://leetcode.com/problems/non-overlapping-intervals/ 101 | - Meeting Rooms (Leetcode Premium) - https://leetcode.com/problems/meeting-rooms/ 102 | - Meeting Rooms II (Leetcode Premium) - https://leetcode.com/problems/meeting-rooms-ii/ 103 | 104 | ### Linked List 105 | 106 | - Reverse a Linked List - https://leetcode.com/problems/reverse-linked-list/ 107 | - Detect Cycle in a Linked List - https://leetcode.com/problems/linked-list-cycle/ 108 | - Merge Two Sorted Lists - https://leetcode.com/problems/merge-two-sorted-lists/ 109 | - Merge K Sorted Lists - https://leetcode.com/problems/merge-k-sorted-lists/ 110 | - Remove Nth Node From End Of List - https://leetcode.com/problems/remove-nth-node-from-end-of-list/ 111 | - Reorder List - https://leetcode.com/problems/reorder-list/ 112 | 113 | ### Matrix 114 | 115 | - Set Matrix Zeroes - https://leetcode.com/problems/set-matrix-zeroes/ 116 | - Spiral Matrix - https://leetcode.com/problems/spiral-matrix/ 117 | - Rotate Image - https://leetcode.com/problems/rotate-image/ 118 | - Word Search - https://leetcode.com/problems/word-search/ 119 | 120 | ### String 121 | 122 | - Longest Substring Without Repeating Characters - https://leetcode.com/problems/longest-substring-without-repeating-characters/ 123 | - Longest Repeating Character Replacement - https://leetcode.com/problems/longest-repeating-character-replacement/ 124 | - Minimum Window Substring - https://leetcode.com/problems/minimum-window-substring/ 125 | - Valid Anagram - https://leetcode.com/problems/valid-anagram/ 126 | - Group Anagrams - https://leetcode.com/problems/group-anagrams/ 127 | - Valid Parentheses - https://leetcode.com/problems/valid-parentheses/ 128 | - Valid Palindrome - https://leetcode.com/problems/valid-palindrome/ 129 | - Longest Palindromic Substring - https://leetcode.com/problems/longest-palindromic-substring/ 130 | - Palindromic Substrings - https://leetcode.com/problems/palindromic-substrings/ 131 | - Encode and Decode Strings (Leetcode Premium) - https://leetcode.com/problems/encode-and-decode-strings/ 132 | 133 | ### Heap 134 | 135 | - Merge K Sorted Lists - https://leetcode.com/problems/merge-k-sorted-lists/ 136 | - Top K Frequent Elements - https://leetcode.com/problems/top-k-frequent-elements/ 137 | - Find Median from Data Stream - https://leetcode.com/problems/find-median-from-data-stream/ 138 | 139 | ### Dynamic Programming 140 | 141 | Do these last. Many companies don't ask them anymore. 142 | 143 | - Climbing Stairs - https://leetcode.com/problems/climbing-stairs/ 144 | - Coin Change - https://leetcode.com/problems/coin-change/ 145 | - Longest Increasing Subsequence - https://leetcode.com/problems/longest-increasing-subsequence/ 146 | - Longest Common Subsequence - 147 | - Word Break Problem - https://leetcode.com/problems/word-break/ 148 | - Combination Sum - https://leetcode.com/problems/combination-sum-iv/ 149 | - House Robber - https://leetcode.com/problems/house-robber/ 150 | - House Robber II - https://leetcode.com/problems/house-robber-ii/ 151 | - Decode Ways - https://leetcode.com/problems/decode-ways/ 152 | - Unique Paths - https://leetcode.com/problems/unique-paths/ 153 | - Jump Game - https://leetcode.com/problems/jump-game/ 154 | --------------------------------------------------------------------------------