|
| 1 | +--- |
| 2 | +title: 'Data Structure and Algorithms 101' |
| 3 | +summary: 'Data Structure and Algorithms Notes' |
| 4 | +description: 'Data Structure and Algorithms Notes' |
| 5 | +disableShare: true |
| 6 | +editPost: |
| 7 | + disabled: true |
| 8 | +hideSummary: false |
| 9 | +comments: false |
| 10 | +searchHidden: true |
| 11 | +hiddenInHomeList: true |
| 12 | +private: true |
| 13 | +--- |
| 14 | + |
| 15 | +## Solving a Coding Question in an Interview |
| 16 | + |
| 17 | +1. If we are dealing with **top/maximum/minimum/closest** 'K' elements among 'N' elements, we will be using a **Heap**. |
| 18 | +2. If the given input is a **sorted array or a list**, we will either be using **Binary Search** or the **Two Pointers** strategy. |
| 19 | +3. If we need to try all **combinations (or permutations)** of the input, we can either use **Backtracking** or **Breadth First Search**. |
| 20 | +4. Most of the questions related to **Trees** or **Graphs** can be solved either through **Breadth First Search** or **Depth First Search**. |
| 21 | +5. Every **recursive** solution can be converted to an **iterative** solution using **Stack**. |
| 22 | +6. For a problem involving arrays, if there exists a solution in **O(n^2)** time and **O(1)** space, there must exist two other solutions: |
| 23 | + |
| 24 | + - Using a **HashMap or a Set** for **O(n)** time and **O(n)** space, |
| 25 | + - Using **sorting** for **O(n log n)** time and **O(1)** space. |
| 26 | + |
| 27 | +7. If a problem is asking for **optimization** (e.g., maximization or minimization), we will be using **Dynamic Programming**. |
| 28 | +8. If we need to find some common **substring** among a set of strings, we will be using a **HashMap** or a **Trie**. |
| 29 | +9. If we need to **search/manipulate** a bunch of strings, **Trie** will be the best data structures. |
| 30 | +10. If the problem is related to a **LinkedList** and we can't use extra space, then use the **Fast & Slow Pointer** approach. |
| 31 | + |
| 32 | +## Big O - Time Complexity |
| 33 | + |
| 34 | +Big-O notation is a mathematical notation that is used to describe the performance or complexity of an algorithm, specifically how long an algorithm takes to run as the input size grows. |
| 35 | + |
| 36 | +Understanding Big-O notation is essential for software engineers, as it allows them to analyze and compare the efficiency of different algorithms and make informed decisions about which one to use in a given situation. |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | +## Space Complexity |
| 43 | + |
| 44 | +- Primitive variables (booleans, numbers, undefined, null) are constant space O(1) |
| 45 | +- Strings require O(n) space (where n is the string length) |
| 46 | +- Reference types are generally O(n), where n is the length (for arrays) or the number or keys (for objects) |
| 47 | + |
| 48 | +## Searching Algorithms |
| 49 | + |
| 50 | +### Linear Search |
| 51 | + |
| 52 | +### Binary Search |
| 53 | + |
| 54 | +## Sorting Algorithms |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | +### Bubble sort |
| 59 | + |
| 60 | +### Selection sort |
| 61 | + |
| 62 | +### Insertion sort |
| 63 | + |
| 64 | +### Merge sort |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | +### Quick sort |
| 69 | + |
| 70 | +### Radix sort |
| 71 | + |
| 72 | +## Data Structures |
| 73 | + |
| 74 | +Data structures are collections of values, the relationships among them, and the functions or |
| 75 | +operations that can be applied to the data. Commonly used data structures in javascript: array and object |
| 76 | + |
| 77 | +### Singly Linked List |
| 78 | + |
| 79 | +#### Linked List |
| 80 | + |
| 81 | +- Do not have indexes |
| 82 | +- Connected via nodes with a next pointer |
| 83 | +- Random access is not allowed |
| 84 | + |
| 85 | +#### Arrays |
| 86 | + |
| 87 | +- Indexed in order |
| 88 | +- Insertion and deletion can be expensive |
| 89 | +- Can quickly be accessed at a specific index |
| 90 | +- Searching a value could be expensive while using linked list, however if you need to |
| 91 | + perform insertion and deletion in a large data set then it could be the better option |
| 92 | + than an array |
| 93 | + |
| 94 | +#### Time complexity |
| 95 | + |
| 96 | +- Insertion: O(1) |
| 97 | +- Removal: Depends O(1) or O(n) |
| 98 | +- Search: O(n) |
| 99 | +- Access: O(n) |
| 100 | + |
| 101 | +### Doubly Linked List |
| 102 | + |
| 103 | +- Has one more pointer previous than singly link list |
| 104 | +- Takes up more memory but has more flexibility than singly link list |
| 105 | + |
| 106 | +Time complexity: |
| 107 | + |
| 108 | +- Insertion: O(1) |
| 109 | +- Removal: O(1) |
| 110 | +- Search: O(n) |
| 111 | +- Access: O(n) |
| 112 | + |
| 113 | +### Stack & Queues |
| 114 | + |
| 115 | +#### Stack |
| 116 | + |
| 117 | +- Implementation using Singly Link list |
| 118 | +- Insert at the beginning and remove at the beginning (shift and unShift) |
| 119 | + |
| 120 | +Time complexity: |
| 121 | + |
| 122 | +- Insertion: O(1) |
| 123 | +- Removal: O(1) |
| 124 | +- Searching: O(n) |
| 125 | +- Access: O(n) |
| 126 | + |
| 127 | +#### Queue |
| 128 | + |
| 129 | +- Implementation using Singly Link list |
| 130 | +- Insert at the end and remove at the beginning (push and shift) |
| 131 | + |
| 132 | +Time complexity: |
| 133 | + |
| 134 | +- Insertion: O(1) |
| 135 | +- Removal: O(1) |
| 136 | +- Searching: O(n) |
| 137 | +- Access: O(n) |
| 138 | + |
| 139 | +### Trees |
| 140 | + |
| 141 | +### Binary Heap |
| 142 | + |
| 143 | +### Priority Queue |
| 144 | + |
| 145 | +### Hash Tables |
| 146 | + |
| 147 | +### Graph |
| 148 | + |
| 149 | +## Algorithms and it's Applications |
| 150 | + |
| 151 | + |
| 152 | + |
| 153 | +## Reference |
| 154 | + |
| 155 | +- https://dev.to/arslan_ah/20-essential-coding-patterns-to-ace-your-next-coding-interview-32a3 |
0 commit comments