Preparing Interviews - Common Tricks in Leetcode
Patterns
- Arrays & Hashing
- Sliding Window & 2 pointers
- Tree
- Subset level by level (BFS/DFS)
- Modified Binary Search (O(log(n))) / O(log(m * n))
- Top K elements (Heap)
- Linked List
- DP
Sliding Window and Two Pointers
1 | class Solution: |
Tree
- Recursive Solutions and DFS/BFS are often used
- queue is used to implement BFS. In python its
deque()
which hasappend()
,popleft()
andpop()
(pop right) function
1 | # Definition for a binary tree node. |
1 | class Solution: |
1 | class Solution: |
Modified Binary Search
- Usually be aware of
pivot = low + 1
to check do you really need to move the pivot + 1 or - 1.
Subset
To figure out if it is a subset question, plot a decision tree
Questions will be related to combination subset, permutation subset etc.
- Usually Recursive Solutions (Backtracking) with BFS or DFS or slack. In implementation usually a list is used.
- Usually O(n2^n)
1 | class Solution: |
Make sure to use .copy()
when appending in such questions.
LinkedList
1 | # Definition for singly-linked list. |
- LinkedList insert and remove are O(1) because it is done in the last node (or first node in Double Linked List).
Floyd’s Tortoise and Hare Algorithm
- Two pointers in Linked List
LRU (Least Recent Used cache)
A popular question using LinkedList.
- Double Linked List <—>
- Leftmost Least Recent <------> Most Recent Rightmost
1 | # Create a customized Node implementation |
DP
- Bottom Up approach (storing base cases values)
Usage
Try to use range all the time, less confusion
1 | prices = [1,2,3,4] |
Playing with strings
Sort and Reverse string
- You cannot sort a string or reverse a string using default functions (e.g.
reversed()
,.sort()
). - You can use
s[::-1]
to reverse the string.
Checking a char is not special character, we can use this .isalnum()
(built-in function). Otherwise you can leverage ascii code ord()
and char()
.
1 | def isPalindrome(self, s: str) -> bool: |
Sets
Sets cannot contain duplicate. Therefore we can simply check the length of list.
- Useful to check duplicates
List
enumerate
allows you to get the index and the item easily.
1 | for i, n in enumerate(nums): |
Dropping list in-place can do
1 | your_list = [1, 2, 3, 4, 5, 6, 7, 8, 9] |
Linked List
- Most questions are swapping questions.
Stack
List can be acted as a stack, using .pop()
to throw away the last value
Dict
your_dict.get(item, 0)
the 0 marks the default value if item is not found.
loop through dict is also easy
1 | def topKFrequent(self, nums: List[int], k: int) -> List[int]: |
defaultdict
defaultdict
streamline handling of missing keys by avoiding repetitive checks or KeyError handling.
1 | d = defaultdict(list) |
Moreover it can handle tuples, unlike dict()
1 | def groupAnagrams(self, strs: List[str]) -> List[List[str]]: |
English
- Anagram: means two strings contain same number of characters. Can be solved by comparing two dicts, or even single defaultdict (using the difference properties)
- Palindrome: means the inverted string would be the same. Can be solved by
s[::-1]