├── .gitignore ├── Python └── LengOfTheLastWord.py └── README.md /.gitignore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HarunMbaabu/Data-Structures-and-Algorithms/0767a68f2569598a97b396c0e0ffcddec8961fda/.gitignore -------------------------------------------------------------------------------- /Python/LengOfTheLastWord.py: -------------------------------------------------------------------------------- 1 | def length_of_last_word(s): 2 | words = s.split() 3 | if len(words) == 0: 4 | return 0 5 | return len(words[-1]) 6 | 7 | print(length_of_last_word("Python Exercises")) 8 | print(length_of_last_word("Python")) 9 | print(length_of_last_word("")) 10 | print(length_of_last_word(" ")) -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## **Data Structures and Algorithms.** 2 | 3 | This repository contains over 200 data structures and algorithm questions and their solution for interview preparation from different websites and Cracking the Coding Interview book by Laakmann Gayle McDowell 4 | 5 | 6 | ```python 7 | 8 | # Stack implementation in python 9 | 10 | 11 | # Creating a stack 12 | def create_stack(): 13 | stack = [] 14 | return stack 15 | 16 | 17 | # Creating an empty stack 18 | def check_empty(stack): 19 | return len(stack) == 0 20 | 21 | 22 | # Adding items into the stack 23 | def push(stack, item): 24 | stack.append(item) 25 | print("pushed item: " + item) 26 | 27 | 28 | # Removing an element from the stack 29 | def pop(stack): 30 | if (check_empty(stack)): 31 | return "stack is empty" 32 | 33 | return stack.pop() 34 | 35 | 36 | stack = create_stack() 37 | push(stack, str(1)) 38 | push(stack, str(2)) 39 | push(stack, str(3)) 40 | push(stack, str(4)) 41 | print("popped item: " + pop(stack)) 42 | print("stack after popping an element: " + str(stack)) 43 | ``` 44 | 45 | 46 | Hash Table - in simple terms we can say that a hasktable is a key value look up, It is a data structure that maps a key to a value for high efficient look up. 47 | 48 | 49 | 50 | 51 | **Function to display hashtable** 52 | 53 | ```python 54 | # Function to display hashtable 55 | def display_hash(hashTable): 56 | 57 | for i in range(len(hashTable)): 58 | print(i, end = " ") 59 | 60 | for j in hashTable[i]: 61 | print("-->", end = " ") 62 | print(j, end = " ") 63 | 64 | print() 65 | 66 | # Creating Hashtable as 67 | # a nested list. 68 | HashTable = [[] for _ in range(10)] 69 | 70 | # Hashing Function to return 71 | # key for every value. 72 | def Hashing(keyvalue): 73 | return keyvalue % len(HashTable) 74 | 75 | 76 | # Insert Function to add 77 | # values to the hash table 78 | def insert(Hashtable, keyvalue, value): 79 | 80 | hash_key = Hashing(keyvalue) 81 | Hashtable[hash_key].append(value) 82 | 83 | # Driver Code 84 | insert(HashTable, 10, 'Harun') 85 | insert(HashTable, 25, 'Mwenda') 86 | insert(HashTable, 20, 'Linet') 87 | insert(HashTable, 9, 'Patrick') 88 | insert(HashTable, 21, 'Mwendwa') 89 | insert(HashTable, 21, 'Mwangi') 90 | 91 | display_hash (HashTable) 92 | ``` 93 | 94 | 95 | **Check duplicate.** 96 | 97 | ```python 98 | def get_squared_number(numbers): 99 | squared_numbers = [] 100 | for n in numbers: 101 | squared_numbers.append(n*n) 102 | return squared_numbers 103 | 104 | numbers = [10,20,30] 105 | 106 | get_squared_number(numbers) 107 | ``` 108 | 109 | --- 110 | ### **Day 1** 111 | --- 112 | 113 | 1). Question 1: Implement an algorithm using Python to determine if all characters in a string are unique, What if you can not use additional data structures. 114 | 115 | ```python 116 | MAX_CHAR = 256; 117 | 118 | def uniqueCharacters(string): 119 | n = len(string) 120 | 121 | # If length is greater than 256, 122 | # some characters must have 123 | # been repeated 124 | if n > MAX_CHAR: 125 | return False 126 | 127 | chars = [False] * MAX_CHAR 128 | 129 | for i in range(n): 130 | index = ord(string[i]) 131 | 132 | ''' 133 | * If the value is already True, 134 | string has duplicate characters, 135 | return False''' 136 | if (chars[index] == True): 137 | return False 138 | 139 | chars[index] = True 140 | 141 | ''' No duplicates encountered, 142 | return True ''' 143 | return True 144 | 145 | # Driver code 146 | if __name__ == '__main__': 147 | 148 | input = input("Enter your Text: ") 149 | if (uniqueCharacters(input)): 150 | print(f"The String {input} has all unique characters") 151 | 152 | else: 153 | print("The String {input} has duplicate characters") 154 | 155 | 156 | ``` 157 | 158 | 159 | **Note:** The time complexity is 0(n) while space complexity is 0(1) 160 | 161 | 162 | --- 163 | 164 | 2). Question 2: Given two strings write a method to decide if one os a permutation or the other. 165 | 166 | 167 | ```python 168 | def arePermutation(str1, str2): 169 | 170 | # Get lengths of both strings 171 | n1 = len(str1) 172 | n2 = len(str2) 173 | 174 | # If length of both strings is not same, 175 | # then they cannot be Permutation 176 | if (n1 != n2): 177 | return False 178 | 179 | # Sort both strings 180 | a = sorted(str1) 181 | str1 = " ".join(a) 182 | b = sorted(str2) 183 | str2 = " ".join(b) 184 | 185 | # Compare sorted strings 186 | for i in range(0, n1, 1): 187 | if (str1[i] != str2[i]): 188 | return False 189 | 190 | return True 191 | 192 | # Driver Code 193 | if __name__ == '__main__': 194 | str1 = "test" 195 | str2 = "ttew" 196 | if (arePermutation(str1, str2)): 197 | print("Yes") 198 | else: 199 | print("No") 200 | ``` 201 | 202 | --- 203 | 204 | 3). Question 3: Write an algorithm such that if an element in a MxN matrix is O, it entire row and column is set to 0. 205 | 206 | 207 | --- 208 | 4). Question 4: Implement an algorithm to find the 4th to the last element of a singly linked list. 209 | 210 | 211 | --- 212 | 213 | 5). Question 5: Write code to remove duplicates from an unsorted linked list. How would you solve this problem if a temporary buffer is not allowed? 214 | 215 | 216 | 217 | --- 218 | 219 | ### **Day 2** 220 | 221 | --- 222 | 1). Question 1: Write a program that finds the length of the last word. 223 | 224 | **Solution : Python3** 225 | 226 | ```python 227 | def length_of_last_word(s): 228 | words = s.split() 229 | if len(words) == 0: 230 | return 0 231 | return len(words[-1]) 232 | 233 | print(length_of_last_word("Python Exercises")) 234 | print(length_of_last_word("Python")) 235 | print(length_of_last_word("")) 236 | print(length_of_last_word(" ")) 237 | 238 | #Output: 239 | #9 240 | #6 241 | #0 242 | #0 243 | 244 | ``` 245 | ---- 246 | 247 | 2). Question 2: Implement a function to check if a linked list is a palindrome( example of a palindrome is madam). 248 | 249 | --- 250 | 251 | 3). Question 3: Given a linked list which might contain a loop, implement an algorithm that returns the node at the beginning of the loop (if one doesn’t exist) 252 | 253 | --- 254 | 255 | 256 | 4). Question 4: Explain how you could use a single array to implement three stacks. 257 | 258 | 259 | --- 260 | 261 | 5). Question 5: Given a sorted (increasing order) array with unique integer elements, write an algorithm to create a binary search tree with minimal height. 262 | 263 | --- 264 | 265 | ### **Day 3** 266 | 267 | --- 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 277 | 278 | 279 | --------------------------------------------------------------------------------