--- tags: Software Engineer, Leetcode, --- # Interview Leetcode Notes > Writing code for Interview != writing code at work/school. ## Common Data Structures - List, Array, LinkedList - Map - Set - Graph - Tree - Binary Tree - Binary Search Tree - Stack, Queue, Hash, Heap - Union Find - Trie ## Common Algorithm Templates ### Binary Search ``` begin, end while begin + 1 < end: mid = (begin + end) // 2 some logic to assign mid to begin or end ``` - [First Bad Version](https://leetcode.com/problems/first-bad-version/) - [First & Last Position](https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/) - [Peak element](https://leetcode.com/problems/find-peak-element/) - [All Binary Search problems](https://leetcode.com/problemset/all/?topicSlugs=binary-search&page=1&sorting=W3sic29ydE9yZGVyIjoiREVTQ0VORElORyIsIm9yZGVyQnkiOiJGUkVRVUVOQ1kifV0%3D) ### Two pointer ``` left, right while left <= right      while left <= right and <left's condition>:          left += 1      while left <= right and <right's condition>:          right -= 1      if left <= right:          do something (ex. swap left & right)          left += 1          right -= 1 ``` - Reverse - https://leetcode.com/problems/valid-palindrome/ - https://leetcode.com/problems/valid-palindrome-ii/ - Two Sum - https://leetcode.com/problems/two-sum/ - https://leetcode.com/problems/3sum/ - https://leetcode.com/problems/4sum/ - Partition - Quick Select ### BFS, topological sort https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ ### Binary Tree https://leetcode.com/problems/binary-tree-inorder-traversal https://leetcode.com/problems/closest-binary-search-tree-value-ii/solution/ ### DFS Combination https://leetcode.com/problems/combination-sum/ https://leetcode.com/problems/subarray-sum-equals-k/ https://leetcode.com/problems/wildcard-matching/ ### DFS Permutation ### Interval ## Python data structures Map: dict `{}` `set()` ```python x = [] # List, Stack y = list(x) _Map = {} _Dict = {} x_set = set() from collections import Counter Counter() ``` queue heap ## Java data structures List ```java List<Integer> list = new ArrayList<>(); list.add(); list.addAll(); list.get(i) list.remove(object); list.remove(index); ``` Map ```java Map<Integer, String> map = new HashMap<>(); Map<Integer, String> map = new TreeMap<>(); map.put(key, value); map.containsKey(key); map.get(key); map.getOrDefault(key, defaultValue); map.remove ``` Set ```java Set<Integer> set = new HashSet<>(); set1.addAll(set2); // union set1.retainAll(set2); // intersection ``` LinkedList Stack ```java Stack<Integer> stack = new Stack<>(); stack.push(1); stack.pop(); stack.peek(); stack.isEmpty(); ``` ```java Deque<TreeNode> stack = new ArrayDeque<>(); ``` Queue ```java Queue<Integer> queue = new LinkedList<>(); ``` PriorityQueue Utilities ```java Character.isLetterOrDigit() Character.toLowerCase() Arrays.sort(list, (a, b) -> a - b); // ascending Arrays.sort(list, (a, b) -> b - a); // descending Collections.sort(); Collections.reverse(); ```