├── 1. Arrays and Strings ├── New Text Document.o ├── New Text Document.exe ├── 1.3.txt ├── 1.1.txt ├── 1.2.txt └── New Text Document.cpp ├── README.md └── 2. Linked List ├── 2.3.md ├── 2.6.md ├── 2.2.md ├── 2.5.md ├── 2.1.md └── 2.4.md /1. Arrays and Strings/New Text Document.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrishi84/CrackingTheCodingInterview/HEAD/1. Arrays and Strings/New Text Document.o -------------------------------------------------------------------------------- /1. Arrays and Strings/New Text Document.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrishi84/CrackingTheCodingInterview/HEAD/1. Arrays and Strings/New Text Document.exe -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Cracking The Coding Interview 2 | Chapter wise solutions for the book Cracking the Coding Interview 3 | 4 | In this repository you'll find solutions to the questions given in the book 5 | 6 | All the solutions are in a .md file 7 | A short description of logic along with the C++ code is given 8 | 9 | Feel free to raise any issues, I'll be happy to resolve them 10 | -------------------------------------------------------------------------------- /2. Linked List/2.3.md: -------------------------------------------------------------------------------- 1 | Delete the Middle Node 2 | 3 | 1 -> 2 -> 3 -> 4 ->5 4 | 5 | delete: 3 6 | output linked list: 1 -> 2 -> 4 ->5 7 | 8 | Method 1: 9 | 10 | void deleteMiddle(Node* mid){ 11 | 12 | if(mid == NULL || mid->next == NULL) 13 | return; 14 | while(mid->next->next){ 15 | mid->data = mid->next->data; 16 | mid = mid->next; 17 | } 18 | mid->next = NULL: 19 | return; 20 | } 21 | 22 | > Space Complexity : O(1) 23 | > Time Complexity: O(N) -------------------------------------------------------------------------------- /2. Linked List/2.6.md: -------------------------------------------------------------------------------- 1 | Check if a linked list is a pallindrome or not 2 | 3 | Stack approach 4 | 5 | bool isPallindrome(Node* head){ 6 | stack pall_stack; 7 | 8 | Node * fast = head; 9 | Node* slow = head; 10 | 11 | while(fast && fast->next){ 12 | pall_stack.push(slow->data); 13 | slow = slow->next; 14 | fast = fast->next->next; 15 | } 16 | 17 | if(fast){ 18 | slow = slow->next; 19 | } 20 | 21 | while(slow){ 22 | int top = pall_stack.pop(); 23 | 24 | if(top != slow->data) 25 | return false; 26 | slow = slow->next; 27 | } 28 | 29 | return true; 30 | } 31 | 32 | > Space Complexity: O(N) 33 | > Time Complexity: O(N) -------------------------------------------------------------------------------- /1. Arrays and Strings/1.3.txt: -------------------------------------------------------------------------------- 1 | Urilify the given string 2 | 3 | input : "happy birthday lorem " 4 | output: "happy%20birthday%20lorem" 5 | 6 | the sting has enough no of spaces at the end of the string to compensate the extra characters 7 | 8 | Method 1: 9 | Storing each word seperately and then iterating to get urilfied string 10 | 11 | string getURLify(string s){ 12 | if(s.length() < 0) 13 | return ""; 14 | vector words; 15 | string word = ""; 16 | for(int i=0; i< s.length(); i++){ 17 | if(s[i] != ' ') 18 | word += s[i]; 19 | else{ 20 | if(word.length()) 21 | words.push_back(word); 22 | word = ""; 23 | } 24 | } 25 | if(word.length()) 26 | words.push_back(word); 27 | 28 | word = ""; 29 | reverse(words.begin(),words.end()); 30 | for(int i=0; inext, k, i); 9 | i = i+1; 10 | if(i == k) 11 | return head; 12 | return n; 13 | } 14 | 15 | Node* kthLastNode(Node* head, int k){ 16 | int i =0; 17 | return kthLastNode(head, k, i); 18 | } 19 | 20 | >Space Complexity: O(N) 21 | >Time Complexity: O(N) 22 | 23 | Method 2: Iterative 24 | 25 | Create two pointers 26 | Send a pointer k positions ahead 27 | Now iterate both the pointers till the second pointer reaches last pointer 28 | the first pointer will be at the kth last position 29 | 30 | Node* kthLastNode(Node* head, int k){ 31 | Node* p1=head, p2 =head; 32 | for(int i=0; inext; 34 | } 35 | while(p2){ 36 | p1 = p1->next; 37 | p2 = p2->next; 38 | } 39 | 40 | return p2; 41 | } 42 | 43 | > Space Complexity: O(1) 44 | > Time Complexity: O(N) -------------------------------------------------------------------------------- /1. Arrays and Strings/1.1.txt: -------------------------------------------------------------------------------- 1 | Implement an algo to determine if a string has all unique characters. 2 | 3 | Implementation using a Set. 4 | 5 | Step 1: Create an empty set char_set 6 | 7 | Step 2: Iterate over the string and insert all characters to the set 8 | 9 | Step 3: Check if the size of set is equal to the length of string 10 | if equal the string is unique 11 | if not then string is unequal 12 | 13 | Time Complexity : O(n) 14 | Space Complexity: O(n) 15 | 16 | Implementation using knowlwdge of ASCII values 17 | 18 | if a string is of ASCII values or extended ASCII values 19 | We can create an array of boolean value of size 128/256 20 | 21 | for 128 ASCII 22 | Step 1: Check if size of string is less than 128 23 | 24 | Step 2: if greater then return false else continue 25 | 26 | Step 3: Intialise a boolean array of size 128 with intial value as false 27 | 28 | Step 4: iterate over the string 29 | Check if the current element is true or not from the boolean array // true means its already been visited 30 | if yes return false 31 | if not set the current element as false in boolean array 32 | 33 | Step 5: return true as no repeating element is found 34 | 35 | Time Complexity : O(n) 36 | 37 | Space Complexity : O(128) / O(256) -------------------------------------------------------------------------------- /1. Arrays and Strings/1.2.txt: -------------------------------------------------------------------------------- 1 | Check if two given strings are permutation of each other 2 | 3 | Method 1: 4 | 5 | bool isPermutation(string s1, string s2){ 6 | if(s1.length()!= s2.length()) 7 | return false; 8 | 9 | sort(s1.begin(), s1.end()); 10 | sort(s2.begin(), s2.end()); 11 | 12 | return s1 == s2; 13 | } 14 | 15 | space Complexity : O(0) 16 | time complexity: O(nlogn + nlogn) = O(nlogn) 17 | 18 | Method 2: 19 | 20 | bool isPermutation(string s1, string s2){ 21 | if(s1.length()!= s2.length()) 22 | return false; 23 | 24 | unordered_map char_map; 25 | 26 | int i=0, n = s1.length(); 27 | for(i=0; i1->6 + 5->9->2 = 2->1->9 11 | 12 | 617 + 295 = 912 13 | 14 | ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { 15 | 16 | ListNode* temp; 17 | ListNode* res = NULL; ListNode* prev= NULL; 18 | int sum =0, carry =0; 19 | 20 | while(l1 != NULL || l2 != NULL){ 21 | 22 | sum = carry + (l1 ? l1->val :0) + (l2 ? l2->val : 0); 23 | 24 | carry = (sum/10); 25 | sum = sum%10; 26 | 27 | temp = new ListNode(sum); 28 | if(res == NULL) 29 | res = temp; 30 | else 31 | prev->next = temp; 32 | prev= temp; 33 | 34 | 35 | if(l1) 36 | l1 = l1->next; 37 | if(l2) 38 | l2 = l2->next; 39 | } 40 | 41 | if(carry >0){ 42 | temp->next = new ListNode(carry); 43 | } 44 | 45 | return res; 46 | 47 | } 48 | 49 | 50 | Type 2: 51 | 52 | If digits are in forward order 53 | 54 | 6->1->7 + 2->9->5 = 9->1->2 55 | 56 | 617 + 295 = 912 57 | 58 | -------------------------------------------------------------------------------- /2. Linked List/2.1.md: -------------------------------------------------------------------------------- 1 | Remove duplicates from an unsorted Linked list 2 | 3 | Create a HashSet / unordered_set and store the elements 4 | if element is repeated delete the node 5 | 6 | Node* removeDuplicates(Node * head){ 7 | unordered_set no_set; 8 | Node * prev = NULL, curr = head; 9 | 10 | while(curr){ 11 | if(no_set.find(curr->data) != no_set.end()){ 12 | prev->next = curr->next; 13 | Node* temp = curr; 14 | free(temp); 15 | }else{ 16 | no_set.insert(curr->data); 17 | prev = curr; 18 | } 19 | curr = curr->next; 20 | } 21 | 22 | return head; 23 | 24 | } 25 | 26 | >Time Complexity :O(N) 27 | 28 | >Space Complexity : O(1) 29 | 30 | 31 | If temporary buffer is not allowed 32 | 33 | Select the curr node and compare it with all the remaining nodes 34 | if a duplicate node is found delete it 35 | 36 | 37 | 38 | Node* removeDuplicates(Node * head){ 39 | Node* curr = head; 40 | Node* runnerNode; 41 | while(curr){ 42 | runnerNode=curr; 43 | while(runnerNode->next){ 44 | if(curr->data == runnerNode->next->data){ 45 | Node * temp = runnerNode; 46 | runnerNode->next = runnerNode->next->next; 47 | }else 48 | runnerNode = runnerNode->next; 49 | } 50 | curr = curr->next; 51 | } 52 | return head; 53 | } 54 | 55 | 56 | 57 | >Space Complexity: O(1) 58 | 59 | >Time Complexity: O(N*N) 60 | 61 | -------------------------------------------------------------------------------- /2. Linked List/2.4.md: -------------------------------------------------------------------------------- 1 | Partition linked list 2 | 3 | > Input : 3 -> 5 -> 8 -> 5 -> 10 ->2 -> 1 [partitionelement = 5] 4 | > Output : 3->1->2->10->5->5->8 5 | 6 | Create two Linked List lessThan , greaterEqual 7 | Iterate through inital linked list and fill these two LLs 8 | Once completed iterating merge the two linked lists 9 | 10 | Node* (Node* head, int partition){ 11 | Node* lessStart = NULL; 12 | Node* lessEnd = NULL; 13 | Node* greaterStart = NULL; 14 | Node* greaterEnd = NULL; 15 | 16 | while(head){ 17 | 18 | if(head->data >=x){ 19 | if(greaterStart == NULL){ 20 | greaterStart = head; 21 | greaterEnd = greaterStart; 22 | } 23 | else{ 24 | greaterEnd->next = head; 25 | greaterEnd = head; 26 | } 27 | } 28 | 29 | else{ 30 | if(lessStart == NULL){ 31 | lessStart = head; 32 | lessEnd = lessStart; 33 | }else{ 34 | lessEnd->next = head; 35 | lessEnd = head; 36 | } 37 | } 38 | head = head->next; 39 | } 40 | 41 | if(lessStart == NULL) 42 | return greaterStart 43 | lessEnd->next = greaterStart; 44 | greater->next = NULL; 45 | 46 | return lessStart; 47 | } 48 | 49 | 50 | Space Complexity : O(N) 51 | Time Complexity: O(N) -------------------------------------------------------------------------------- /1. Arrays and Strings/New Text Document.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | bool isPermutation(string s1, string s2){ 4 | if(s1.length()!= s2.length()) 5 | return false; 6 | 7 | unordered_map char_map; 8 | 9 | int i=0, n = s1.length(); 10 | for(i=0; i words; 40 | string word = ""; 41 | for(int i=0; i< s.length(); i++){ 42 | if(s[i] != ' ') 43 | word += s[i]; 44 | else{ 45 | if(word.length()) 46 | words.push_back(word); 47 | word = ""; 48 | } 49 | } 50 | if(word.length()) 51 | words.push_back(word); 52 | 53 | word = ""; 54 | //reverse(words.begin(),words.end()); 55 | for(int i=0; i