├── Ch-1-Arrays-and-Strings ├── 1.1-IsUnique │ └── isUnique.go ├── 1.2-Check-Permutation │ └── checkPermutation.go ├── 1.3-URLify │ └── URLify.go ├── 1.4-Palindrome-Permutation │ └── palindromePermutation.go ├── 1.5-One-Away │ ├── oneAway.go │ └── oneWay.go ├── 1.6-String-Compression │ └── stringCompression.go ├── 1.7-RotateMatrix │ └── rotateMatrix.go └── 1.9-String-Rotation │ └── stringRotation.go ├── Ch-2-LinkedList ├── 2.1-Remove-Dups │ └── removeDups.go ├── 2.2-Return-Kth-to-Last │ └── returnKthToLast.go ├── 2.3-Delete-Middle-Node │ └── deleteMiddleNode.go ├── 2.4-Partition │ └── Partition.go ├── 2.5-Sum-Lists │ └── sumLists.go ├── 2.6-Palindrome │ └── palindrome.go ├── 2.7-Intersection │ └── intersection.go ├── 2.8-Loop-Detection │ └── loopDetection.go └── SinglyLinkedList │ └── singlyLinkedList.go ├── Ch-4-Trees&Graphs └── BinaryTreeImplementation │ └── binaryTree.go ├── Ch3-Stacks-and-Queues ├── 3.2-Stack-Min │ └── stackMin.go ├── 3.3-Stack-of-Plates │ └── stackOfPlates.go ├── 3.4-Queue-via-Stacks │ └── queueViaStacks.go ├── 3.5-Sort-Stack │ └── sortStack.go ├── Queue │ └── queue.go └── Stack │ └── stack.go └── README.md /Ch-1-Arrays-and-Strings/1.1-IsUnique/isUnique.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | /* 6 | Is Unique: Implement an algorithm to determine if a string has all unique characters. 7 | What if you cannot use additional data structures? 8 | */ 9 | 10 | // What if you cannot use additional data structures? O(n^2) 11 | func isUniqueInefficient(s string) bool { 12 | for i := 0; i < len(s); i++ { 13 | for j := i + 1; j < len(s); j++ { 14 | if s[i] == s[j] { 15 | return false 16 | } 17 | } 18 | } 19 | return true 20 | } 21 | 22 | // O(n) 23 | func isUnique(s string) bool { 24 | m := make(map[string]int) 25 | for i := 0; i < len(s); i++ { 26 | if _, ok := m[string(s[i])]; ok { 27 | return false 28 | } else { 29 | m[string(s[i])]++ 30 | } 31 | } 32 | return true 33 | } 34 | 35 | func main() { 36 | fmt.Println(isUnique("abcdefghijklmnopqrstuvwxyza")) 37 | } 38 | -------------------------------------------------------------------------------- /Ch-1-Arrays-and-Strings/1.2-Check-Permutation/checkPermutation.go: -------------------------------------------------------------------------------- 1 | /* 2 | Check Permutation: Given two strings, write a method to decide if one is a permutation of the 3 | other. 4 | 5 | Observe first that strings of different lengths cannot be permutations of each other. 6 | If two strings are permutations, then we know they have the same characters, but in different orders. 7 | Therefore, sorting the strings will put the characters from two permutations in the same order. We just need to 8 | compare the sorted versions of the strings. 9 | 10 | How to sort a string in golang 11 | Go 1.8 or before 12 | The steps: 13 | Convert string to []rune 14 | Define a new type ByRune, which is actually []rune. Implement Len, Swap, and Less methods of type ByRune. 15 | Call sort.Sort to sort the slice of runes. 16 | Convert []rune back to string and return the string. 17 | 18 | Go 1.8 or later 19 | The sort package in Go 1.8 introduces new methods for sorting slices. 20 | We can use sort.Slice method directly without defining a new type. 21 | Convert string to []rune 22 | Define a less method and call sort.Slice() with the slice of runes and the less method as parameters. 23 | sort.Slice(r, func(i, j int) bool { 24 | return r[i] < r[j] 25 | }) 26 | Convert []rune back to string and return the string. 27 | */ 28 | 29 | package main 30 | 31 | import ( 32 | "fmt" 33 | "sort" 34 | ) 35 | 36 | func stringsToRunes(s string) []rune { 37 | r := []rune{} 38 | for _, v := range s { 39 | r = append(r, rune(v)) 40 | } 41 | return r 42 | } 43 | 44 | func Permutation(a,b string) bool { 45 | if len(a) != len(b) { 46 | return false 47 | } 48 | c := stringsToRunes(a) 49 | d := stringsToRunes(b) 50 | sort.SliceStable(c, func(i, j int) bool { 51 | return c[i] < c[j] 52 | }) 53 | sort.SliceStable(d, func(i, j int) bool { 54 | return d[i] < d[j] 55 | }) 56 | if string(c) != string(d) { 57 | return false 58 | } 59 | return true 60 | } 61 | 62 | func main() { 63 | fmt.Println(Permutation("abcd","bcda")) 64 | } -------------------------------------------------------------------------------- /Ch-1-Arrays-and-Strings/1.3-URLify/URLify.go: -------------------------------------------------------------------------------- 1 | /* 2 | Write a method to replace all spaces in a string with '%20'. You may assume that the string 3 | has sufficient space at the end to hold the additional characters, and that you are given the "true" 4 | length of the string. (Note: If implementing in Java, please use a character array so that you can 5 | perform this operation in place.) 6 | EXAMPLE 7 | Input: "Mr John Smith ", 13 8 | Output: "Mr%20John%20Smith" 9 | 10 | A common approach in string manipulation problems is to edit the string starting from the end and working 11 | backwards.This is useful because we have an extra buffer at the end, which allows us to change characters 12 | without worrying about what we're overwriting. We will use this approach in this problem. 13 | 14 | The algorithm employs a two-scan approach. In the first scan, we count the number of spaces. 15 | By tripling this number, we can compute how many extra characters we will have in the final string. 16 | In the second pass, which is done in reverse order, we actually edit the string. When we see a space, 17 | we replace it with %20. If there is no space, then we copy the original character. 18 | */ 19 | 20 | package main 21 | 22 | import ( 23 | "fmt" 24 | ) 25 | 26 | /* 27 | Strings are immutable in go so convert it into a rune. 28 | */ 29 | func urlify(a string, truelength int) string { 30 | space := 0 31 | r := []rune{} 32 | for i := 0; i < truelength; i++ { 33 | if string(a[i]) == " " { 34 | space++ 35 | } 36 | } 37 | for _, v := range a { 38 | r = append(r, v) 39 | } 40 | originalLength := space*2 + truelength 41 | for i := truelength - 1; i >= 0; i-- { 42 | if r[i] == ' ' { 43 | r[originalLength-1] = '0' 44 | r[originalLength-2] = '2' 45 | r[originalLength-3] = '%' 46 | originalLength = originalLength - 3 47 | } else { 48 | r[originalLength-1] = r[i] 49 | originalLength-- 50 | } 51 | } 52 | 53 | return string(r) 54 | } 55 | 56 | func main() { 57 | fmt.Println(urlify("Mr John Smith ", 13)) 58 | } 59 | -------------------------------------------------------------------------------- /Ch-1-Arrays-and-Strings/1.4-Palindrome-Permutation/palindromePermutation.go: -------------------------------------------------------------------------------- 1 | /* 2 | Palindrome Permutation: Given a string, write a function to check if it is a permutation of a palindrome. 3 | A palindrome is a word or phrase that is the same forwards and backwards. A permutation is a 4 | rearrangement of letters. The palindrome does not need to be limited to just dictionary words. 5 | EXAMPLE 6 | Input: Tact Coa 7 | Output: True (permutations: "taco cat", "atco eta", etc.) 8 | 9 | What does it take to be able to write a set of characters the same way forwards and backwards? 10 | We need to have an even number of almost all characters, so that half can be on one side and half can be on the other 11 | side. 12 | Very Important :- At most one character (the middle character) can have an odd count. 13 | eg: racecar 14 | Palindrome := racecar 15 | Permutaion := rac e rac 16 | This one odd letter e will always be placed in the middle to make a permutation, 17 | All letter on the left and right side of it will be same but arranged differently. 18 | 19 | 20 | To be more precise, strings with even length (after removing all non-letter characters, like spaces) 21 | must have all even counts of characters. 22 | Strings of an odd length must have exactly one character with an odd count. 23 | Of course, an "even" string can't have an odd number of exactly one character, 24 | otherwise it wouldn't be an even-length string (an odd number+ many even numbers= an odd number). 25 | Likewise, a string with odd length can't have all characters with even counts (sum of evens is even). 26 | It's therefore sufficient to say that, to be a permutation ot a palindrome, a string can have 27 | no more than one character that is odd. This will cover both the odd and the even cases. 28 | */ 29 | 30 | package main 31 | 32 | import ( 33 | "fmt" 34 | "strings" 35 | ) 36 | 37 | func palindromePermutation(s string) bool { 38 | s = strings.ToLower(strings.Replace(s, " ", "", -1)) 39 | oddCount := 0 40 | m := make(map[string]int) 41 | for _, v := range s { 42 | if _,ok := m[string(v)]; ok { 43 | m[string(v)]++ 44 | } else { 45 | m[string(v)] = 1 46 | } 47 | } 48 | for _, v := range m { 49 | if v % 2 != 0 && oddCount == 1 { 50 | oddCount++ 51 | if oddCount > 1 { 52 | return false 53 | } 54 | } 55 | } 56 | return true 57 | } 58 | 59 | func main() { 60 | 61 | fmt.Println(palindromePermutation("Tact Coa ")) 62 | } 63 | -------------------------------------------------------------------------------- /Ch-1-Arrays-and-Strings/1.5-One-Away/oneAway.go: -------------------------------------------------------------------------------- 1 | /* 2 | There are three types of edits that can be performed on strings: insert a character, 3 | remove a character, or replace a character. Given two strings, write a function to check if they are 4 | one edit (or zero edits) away. 5 | EXAMPLE 6 | pale, ple -> true 7 | pales, pale -> true 8 | pale, bale -> true 9 | pale, bake -> false 10 | */ 11 | 12 | package main 13 | 14 | import "fmt" 15 | 16 | func oneWay(a, b string) bool { 17 | m := make(map[string]int) 18 | 19 | for _, v := range a { 20 | if _, ok := m[string(v)]; ok { 21 | m[string(v)]++ 22 | } else { 23 | m[string(v)] = 1 24 | } 25 | } 26 | 27 | // case 1 insert 28 | if len(a) > len(b) { 29 | for i := 0; i < len(b); i++ { 30 | if _, ok := m[string(b[i])]; ok { 31 | m[string(b[i])]-- 32 | if m[string(b[i])] == 0 { 33 | delete(m, string(b[i])) 34 | } 35 | } else { 36 | continue 37 | } 38 | } 39 | fmt.Println("case 1:", m) 40 | if len(m) == 1 { 41 | return true 42 | } 43 | } 44 | 45 | // case 2 remove 46 | if len(a) < len(b) { 47 | for i := 0; i < len(a); i++ { 48 | if _, ok := m[string(b[i])]; ok { 49 | m[string(b[i])]-- 50 | if m[string(b[i])] == 0 { 51 | delete(m, string(b[i])) 52 | } 53 | } else { 54 | continue 55 | } 56 | } 57 | fmt.Println("case 2:", m) 58 | if len(m) == 0 { 59 | return true 60 | } 61 | } 62 | 63 | // case 3 replace 64 | if len(a) == len(b) { 65 | for i := 0; i < len(b); i++ { 66 | if _, ok := m[string(b[i])]; ok { 67 | m[string(b[i])]-- 68 | if m[string(b[i])] == 0 { 69 | delete(m, string(b[i])) 70 | } 71 | } else { 72 | continue 73 | } 74 | } 75 | fmt.Println("case 3:", m) 76 | if len(m) == 1 { 77 | return true 78 | } 79 | } 80 | 81 | return false 82 | } 83 | 84 | func main() { 85 | fmt.Println(oneWay("pale", "nake")) 86 | } 87 | -------------------------------------------------------------------------------- /Ch-1-Arrays-and-Strings/1.5-One-Away/oneWay.go: -------------------------------------------------------------------------------- 1 | /* 2 | There are three types of edits that can be performed on strings: insert a character, 3 | remove a character, or replace a character. Given two strings, write a function to check if they are 4 | one edit (or zero edits) away. 5 | EXAMPLE 6 | pale, ple -> true 7 | pales, pale -> true 8 | pale, bale -> true 9 | pale, bake -> false 10 | */ 11 | 12 | package main 13 | 14 | import "fmt" 15 | 16 | func oneWay(a, b string) bool { 17 | m := make(map[string]int) 18 | 19 | for _, v := range a { 20 | if _, ok := m[string(v)]; ok { 21 | m[string(v)]++ 22 | } else { 23 | m[string(v)] = 1 24 | } 25 | } 26 | 27 | // case 1 insert 28 | if len(a) > len(b) { 29 | for i := 0; i < len(b); i++ { 30 | if _, ok := m[string(b[i])]; ok { 31 | m[string(b[i])]-- 32 | if m[string(b[i])] == 0 { 33 | delete(m, string(b[i])) 34 | } 35 | } else { 36 | continue 37 | } 38 | } 39 | fmt.Println("case 1:", m) 40 | if len(m) == 1 { 41 | return true 42 | } 43 | } 44 | 45 | // case 2 remove 46 | if len(a) < len(b) { 47 | for i := 0; i < len(a); i++ { 48 | if _, ok := m[string(b[i])]; ok { 49 | m[string(b[i])]-- 50 | if m[string(b[i])] == 0 { 51 | delete(m, string(b[i])) 52 | } 53 | } else { 54 | continue 55 | } 56 | } 57 | fmt.Println("case 2:", m) 58 | if len(m) == 0 { 59 | return true 60 | } 61 | } 62 | 63 | // case 3 replace 64 | if len(a) == len(b) { 65 | for i := 0; i < len(b); i++ { 66 | if _, ok := m[string(b[i])]; ok { 67 | m[string(b[i])]-- 68 | if m[string(b[i])] == 0 { 69 | delete(m, string(b[i])) 70 | } 71 | } else { 72 | continue 73 | } 74 | } 75 | fmt.Println("case 3:", m) 76 | if len(m) == 1 { 77 | return true 78 | } 79 | } 80 | 81 | return false 82 | } 83 | 84 | func main() { 85 | fmt.Println(oneWay("pale", "nake")) 86 | } 87 | -------------------------------------------------------------------------------- /Ch-1-Arrays-and-Strings/1.6-String-Compression/stringCompression.go: -------------------------------------------------------------------------------- 1 | /* 2 | String Compression: Implement a method to perform basic string compression using the counts 3 | of repeated characters. For example, the string aabcccccaaa would become a2blc5a3. 4 | If the "compressed" string would not become smaller than the original string, your method should return 5 | the original string. You can assume the string has only uppercase and lowercase letters (a - z). 6 | */ 7 | 8 | package main 9 | 10 | import ( 11 | "fmt" 12 | "strconv" 13 | ) 14 | 15 | func compression(s string) string { 16 | count := 1 17 | str := "" 18 | for i:=0; i len(s) { 35 | return s 36 | } 37 | return str 38 | } 39 | 40 | 41 | 42 | func main() { 43 | 44 | fmt.Println(compression("aaaabccdddeeee")) 45 | } 46 | -------------------------------------------------------------------------------- /Ch-1-Arrays-and-Strings/1.7-RotateMatrix/rotateMatrix.go: -------------------------------------------------------------------------------- 1 | /* 2 | Rotate Matrix: Given an image represented by an NxN matrix, where each pixel in the image is 4 3 | bytes, write a method to rotate the image by 90 degrees. Can you do this in place? 4 | 5 | 1) Reverse the matrix 6 | 2) Take transpose of the reversed matrix 7 | In transpose row becomes column and column becomes row 8 | 9 | 1 2 3 7 8 9 7 4 1 10 | 4 5 6 --> 4 5 6 --> 8 5 2 11 | 7 8 9 1 2 3 9 6 3 12 | 13 | */ 14 | 15 | package main 16 | 17 | import "fmt" 18 | 19 | func rotateMatrix(matrix [][]int) [][]int { 20 | 21 | // reverse the matrix 22 | for i, j := 0, len(matrix)-1; ib->c->d->e->f 8 | Result: nothing is returned, but the new linked list looks like a->b->d->e->f 9 | 10 | Solution: 11 | Since we are only given the node to be deleted, what we can do here is replace the next node with the node 12 | to be deleted. 13 | eg. 14 | Delete c 15 | a->b->c->d->e->f 16 | Replace c with d 17 | c.data = c.next.data 18 | c.next = c.next.next 19 | a->b->d->e->f 20 | */ 21 | 22 | package main 23 | 24 | import ( 25 | linkedList "algorithms-in-golang/CTCI/Ch-2-LinkedList/SinglyLinkedList" 26 | ) 27 | 28 | type LinkedList struct { 29 | list linkedList.LinkedList 30 | } 31 | 32 | func (l *LinkedList) deleteMiddleNode(node *linkedList.Node) { 33 | if node != nil && node.Next != nil { 34 | node.Key = node.Next.Key 35 | node.Next = node.Next.Next 36 | } else { 37 | return 38 | } 39 | } 40 | 41 | func main() { 42 | l := LinkedList{} 43 | values := []int{2, 1, 3, 6, 3, 4, 5} 44 | for _, v := range values { 45 | l.list.Append(v) 46 | 47 | } 48 | l.deleteMiddleNode(l.list.Head.Next.Next.Next) 49 | l.list.Traversal() 50 | } 51 | -------------------------------------------------------------------------------- /Ch-2-LinkedList/2.4-Partition/Partition.go: -------------------------------------------------------------------------------- 1 | /* 2 | Partition: Write code to partition a linked list around a value x, such that all nodes less than x come 3 | before all nodes greater than or equal to x. If x is contained within the list, the values of x only need 4 | to be after the elements less than x (see below). The partition element x can appear anywhere in the 5 | "right partition"; it does not need to appear between the left and right partitions. 6 | EXAMPLE 7 | Input: 3 -> 5 -> 8 -> 5 -> 10 -> 2 -> 1 [partition = 5] 8 | Output: 3 -> 1 -> 2 -> 10 -> 5 -> 5 -> 8 9 | 10 | we create two different linked lists: one for elements less than x, and one for elements greater than or equal to x. 11 | We iterate through the linked list, inserting elements into our before list or our after list. 12 | Once we reach the end of the linked list and have completed this splitting, we merge the two lists. 13 | 14 | */ 15 | 16 | package main 17 | 18 | import ( 19 | linkedList "algorithms-in-golang/CTCI/Ch-2-LinkedList/SinglyLinkedList" 20 | ) 21 | 22 | func partition(l *linkedList.LinkedList, partition int) linkedList.LinkedList { 23 | current := l.Head 24 | less := linkedList.LinkedList{} 25 | more := linkedList.LinkedList{} 26 | for current != nil { 27 | if current.Key < partition { 28 | less.Append(current.Key) 29 | } else if current.Key >= partition { 30 | more.Append(current.Key) 31 | } 32 | current = current.Next 33 | } 34 | less.Tail.Next = more.Head 35 | less.Traversal() 36 | return less 37 | } 38 | 39 | func main() { 40 | l := linkedList.LinkedList{} 41 | values := []int{1, 4, 3, 2, 5, 2} 42 | for _, v := range values { 43 | l.Append(v) 44 | } 45 | partition(&l, 3) 46 | } 47 | -------------------------------------------------------------------------------- /Ch-2-LinkedList/2.5-Sum-Lists/sumLists.go: -------------------------------------------------------------------------------- 1 | /* 2 | Sum Lists: You have two numbers represented by a linked list, where each node contains a single digit. 3 | The digits are stored in reverse order, such that the 1 's digit is at the head of the list. 4 | Write a function that adds the two numbers and returns the sum as a linked list. 5 | EXAMPLE 6 | Input: (7-> 1 -> 6) + (5 -> 9 -> 2).That is,617 + 295. 7 | Output: 2 -> 1 -> 9. That is, 912. 8 | FOLLOW UP 9 | Suppose the digits are stored in forward order. Repeat the above problem. 10 | EXAMPLE 11 | lnput:(6 -> 1 -> 7) + (2 -> 9 -> 5).That is,617 + 295. 12 | Output: 9 -> 1 -> 2. That is, 912. 13 | 14 | Define two pointers p and q for list l1 and l2. 15 | Check is both they are not nil, if one of the list is shorter than the other one make sure to add two to it, 16 | to make both of them equal in length. 17 | If there is carry add it to the next sum just like classical addition. 18 | If the carry is > 0 19 | carry = sum/10 20 | remainder = sum % 10 21 | */ 22 | 23 | package main 24 | 25 | import ( 26 | linkedList "algorithms-in-golang/CTCI/Ch-2-LinkedList/SinglyLinkedList" 27 | ) 28 | 29 | func sumList(l1, l2 *linkedList.LinkedList) linkedList.LinkedList { 30 | sumList := linkedList.LinkedList{} 31 | p := l1.Head 32 | q := l2.Head 33 | carry, i, j := 0,0,0 34 | 35 | for p != nil || q != nil { 36 | // to make sure both the list are equal in length 37 | if p == nil { 38 | i = 0 39 | } else { 40 | i = p.Key 41 | p = p.Next 42 | } 43 | if q == nil { 44 | j = 0 45 | } else { 46 | j = q.Key 47 | q = q.Next 48 | } 49 | sum := i + j + carry 50 | if sum >= 10 { 51 | // will give the carry tens position of the number, 30/10 = 3 52 | carry = sum/10 53 | // will give the ones position of the number, 12 % 10 = 2 54 | remainder := sum % 10 55 | sumList.Append(remainder) 56 | } else { 57 | carry = 0 58 | sumList.Append(sum) 59 | } 60 | } 61 | 62 | return sumList 63 | } 64 | 65 | func main() { 66 | l1 := linkedList.LinkedList{} 67 | l1.Append(5) 68 | l1.Append(6) 69 | l1.Append(3) 70 | l2 := linkedList.LinkedList{} 71 | l2.Append(8) 72 | l2.Append(4) 73 | l2.Append(2) 74 | result := sumList(&l1, &l2) 75 | result.Traversal() 76 | } 77 | 78 | -------------------------------------------------------------------------------- /Ch-2-LinkedList/2.6-Palindrome/palindrome.go: -------------------------------------------------------------------------------- 1 | /* 2 | Palindrome: Implement a function to check if a linked list is a palindrome. 3 | 4 | 5 | Method 1 -> Reverse and Compare 6 | Convert the link elements into a string, reverse that string and compare it to the original string. 7 | 8 | Method 2 -> Using Stack 9 | Iterate over the linked list and push the elements onto a STACK. 10 | Then we are going to iterate through once more and check whether or not the POPPED element is equal to the 11 | element we are encountering in our second iteration. 12 | */ 13 | 14 | package main 15 | 16 | import ( 17 | linkedList "algorithms-in-golang/CTCI/Ch-2-LinkedList/SinglyLinkedList" 18 | "fmt" 19 | ) 20 | 21 | type Stack struct { 22 | value interface{} 23 | container []interface{} 24 | } 25 | 26 | func(s *Stack) push(value interface{}) { 27 | s.container = append(s.container, value) 28 | } 29 | 30 | func(s *Stack) pop() interface{} { 31 | popped := s.container[len(s.container)-1] 32 | s.container = append(s.container[:len(s.container)-1]) 33 | return popped 34 | } 35 | 36 | 37 | // Method 1, Reverse and Compare 38 | func palindrome(l *linkedList.LinkedList) bool { 39 | // convert the link elements into a strings 40 | // node pointer 41 | current := l.Head 42 | var s string 43 | for current != nil { 44 | s+= current.Key.(string) 45 | current = current.Next 46 | } 47 | // reverse the string and compare it to the original string 48 | rvrse := func(s string) string { 49 | r := []rune(s) 50 | for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 { 51 | r[i], r[j] = r[j], r[i] 52 | } 53 | return string(r) 54 | }(s) 55 | 56 | if rvrse != s { 57 | return false 58 | } 59 | 60 | return true 61 | } 62 | 63 | 64 | // Method 2, Using Stack 65 | func stackPalindome(l*linkedList.LinkedList) bool { 66 | s := Stack{} 67 | current := l.Head 68 | // Push the elements into the stack 69 | for current != nil { 70 | s.push(current.Key) 71 | current = current.Next 72 | } 73 | 74 | // reinitialize current again to the head, because current will point to NIL after the first iteration and we will 75 | // never enter the second loop 76 | current = l.Head 77 | 78 | // Pop the element and compare it to the linked list elements 79 | for current != nil { 80 | pop := s.pop() 81 | if pop != current.Key { 82 | return false 83 | } 84 | current = current.Next 85 | } 86 | 87 | return true 88 | } 89 | 90 | 91 | func main() { 92 | l := linkedList.LinkedList{} 93 | values := []string{"a","b","c","d","c","b","a"} 94 | for _, v := range values { 95 | l.Append(string(v)) 96 | } 97 | //Method 1 98 | fmt.Println(palindrome(&l)) 99 | 100 | //Method 2 101 | fmt.Println(stackPalindome(&l)) 102 | 103 | 104 | } -------------------------------------------------------------------------------- /Ch-2-LinkedList/2.7-Intersection/intersection.go: -------------------------------------------------------------------------------- 1 | /* 2 | Intersection: Given two (singly) linked lists, determine if the two lists intersect. 3 | Return the intersecting node. Note that the intersection is defined based on reference, not value. 4 | That is, if the kth node of the first linked list is the exact same node (by reference) as the 5 | jth node of the second linked list, then they are intersecting. 6 | 7 | The two intersecting linked lists will always have the same last node. 8 | Therefore, we can just traverse to the end of each linked list and compare the last nodes. 9 | 10 | If the linked lists were the same length, you could just traverse through them at the same time. 11 | When they collide, that's your intersection. 12 | 13 | When they're not the same length, we'd like to just"chop off ignore-those excess nodes. 14 | How can we do this? 15 | If we know the lengths of the two linked lists, then the difference between those 16 | two linked lists will tell us how much to chop off. 17 | 18 | 1. Run through each linked list to get the lengths and the tails. 19 | 2. Compare the tails. If they are different (by reference, not by value), return immediately. 20 | There is no inter section. 21 | 3. Set two pointers to the start of each linked list. 22 | 4. On the longer linked list, advance its pointer by the difference in lengths. 23 | 5. Now, traverse on each linked list until the pointers are the same. 24 | 25 | */ 26 | 27 | package main 28 | 29 | import ( 30 | linkedList "algorithms-in-golang/CTCI/Ch-2-LinkedList/SinglyLinkedList" 31 | "fmt" 32 | "math" 33 | ) 34 | 35 | func Tail(l *linkedList.LinkedList) *linkedList.Node { 36 | current := l.Head 37 | tail := &linkedList.Node{} 38 | for current != nil { 39 | if current.Next == nil { 40 | tail = current 41 | } 42 | current = current.Next 43 | } 44 | return tail 45 | } 46 | 47 | func intersection(l1 *linkedList.LinkedList, l2 *linkedList.LinkedList) *linkedList.Node { 48 | list1, list2 := l1.Head, l2.Head 49 | lengthL1, tail1 := lengthAndTail(l1) 50 | lengthL2, tail2 := lengthAndTail(l2) 51 | if &tail1 != &tail2 { 52 | return nil 53 | } 54 | difference := abs(lengthL1,lengthL2) 55 | if lengthL1 > lengthL2 { 56 | counter := 0 57 | for list1 != nil && counter < difference { 58 | list1 = list1.Next 59 | counter++ 60 | } 61 | for list1 != nil && list2 != nil { 62 | if &list1 == &list2 { 63 | return list1 64 | } 65 | list1 = list1.Next 66 | list2 = list2.Next 67 | } 68 | } else if lengthL2 > lengthL1 { 69 | counter := 0 70 | for list2 != nil && counter < difference { 71 | list2 = list2.Next 72 | counter++ 73 | } 74 | for list1 != nil && list2 != nil { 75 | if &list1 == &list2 { 76 | return list1 77 | } 78 | list1 = list1.Next 79 | list2 = list2.Next 80 | } 81 | } 82 | return nil 83 | } 84 | 85 | 86 | func abs(a,b int) int { 87 | out := math.Abs(float64(a)-float64(b)) 88 | return int(out) 89 | } 90 | 91 | func lengthAndTail( l *linkedList.LinkedList) (length int, tail *linkedList.Node) { 92 | current := l.Head 93 | prev := &linkedList.Node{} 94 | for current != nil { 95 | prev = current 96 | current = prev.Next 97 | if current == nil { 98 | tail = prev 99 | } 100 | length++ 101 | } 102 | return length,tail 103 | } 104 | 105 | func main() { 106 | l1 := linkedList.LinkedList{} 107 | l2 := linkedList.LinkedList{} 108 | values1 := []int{1,2,3,4,5,6,7,8,9} 109 | values2 := []int{10,20,30,40,5,6,7,8,9} 110 | 111 | for _, v := range values1 { 112 | l1.Append(v) 113 | } 114 | for _, v := range values2 { 115 | l2.Append(v) 116 | } 117 | fmt.Print(intersection(&l1, &l2)) 118 | } -------------------------------------------------------------------------------- /Ch-2-LinkedList/2.8-Loop-Detection/loopDetection.go: -------------------------------------------------------------------------------- 1 | /* 2 | Loop Detection: Given a circular linked list, implement an algorithm that returns the node at the beginning of the loop. 3 | 4 | DEFINITION 5 | Circular linked list: A (corrupt) linked list in which a node's next pointer points to an earlier node, so 6 | as to make a loop in the linked list. 7 | 8 | Solution: 9 | Create two runner traversing the linked list, with one runner running two nodes ahead, i.e., 10 | If slow is node1 then fast should be on node3, if they collide then there is a loop and we return that node. 11 | If they are only one node head of each other they will never be able to collide. 12 | 13 | Fast moves two nodes each time slow moves one node. 14 | 15 | 16 | eg: 17 | 1->2->3->4->5->6->7 18 | \____/ 19 | 20 | Let us assume 7 is pointing to 5 the previous node, not to NIL. The runner will traverse the linked list as follows 21 | 22 | R1 R2 23 | 1 2 24 | 2 5 25 | 3 7 26 | 4 5 27 | 5 7 28 | 6 5 29 | 7 7 30 | 31 | When R1 will point to node6 R2 will also be pointing to node6 because of the loop. 32 | 33 | If R1 and R2 are just one node ahead they will never collide and we will never be able to detect the loop. 34 | R1 R2 35 | 1 2 36 | 2 3 37 | 3 4 38 | 4 5 39 | 5 6 40 | 6 7 41 | */ 42 | 43 | package main 44 | 45 | import ( 46 | linkedList "algorithms-in-golang/CTCI/Ch-2-LinkedList/SinglyLinkedList" 47 | "fmt" 48 | ) 49 | 50 | // Method 2 51 | func Loop(l *linkedList.LinkedList) *linkedList.Node { 52 | if l.Head == nil { 53 | return nil 54 | } 55 | slow := l.Head 56 | 57 | // Initially start Fast one node ahead for the condition on line 63 doesn't match, 58 | // since initially both will point to the same node 59 | fast := l.Head.Next 60 | 61 | for slow != nil && fast != nil && fast.Next != nil { 62 | if slow == fast { 63 | return nil 64 | } 65 | slow = slow.Next // Moves one jump 66 | fast = fast.Next.Next // Moves two jumps 67 | } 68 | 69 | // There is mathematical solution for it. I'm not sure if I understand that :/ 70 | // https://stackoverflow.com/questions/2936213/explain-how-finding-cycle-start-node-in-cycle-linked-list-work 71 | // https://www.youtube.com/watch?v=-YiQZi3mLq0 72 | 73 | traverse := l.Head 74 | for slow != traverse { 75 | slow = slow.Next 76 | traverse = traverse.Next 77 | } 78 | return slow 79 | } 80 | 81 | func main() { 82 | l := linkedList.LinkedList{} 83 | fmt.Println(Loop(&l)) 84 | } 85 | -------------------------------------------------------------------------------- /Ch-2-LinkedList/SinglyLinkedList/singlyLinkedList.go: -------------------------------------------------------------------------------- 1 | /* 2 | Singly LinkedList Implementation 3 | */ 4 | 5 | package SinglyLinkedList 6 | 7 | import "fmt" 8 | 9 | type LinkedList struct { 10 | Head *Node 11 | Tail *Node 12 | Size int 13 | } 14 | 15 | type Node struct { 16 | Key int 17 | Next *Node 18 | } 19 | 20 | func(l *LinkedList) Length() int { 21 | return l.Size 22 | } 23 | 24 | func(l *LinkedList) IsEmpty() bool { 25 | if l.Length() != 0 { 26 | return false 27 | } 28 | return true 29 | } 30 | 31 | func(l *LinkedList) Append(value int) { 32 | node := &Node{Key:value} 33 | if l.Size == 0 { 34 | l.Head = node 35 | l.Tail = node 36 | } else { 37 | l.Tail.Next = node 38 | l.Tail = node 39 | } 40 | l.Size++ 41 | } 42 | 43 | func(l *LinkedList) Traversal() { 44 | for l.Head != nil { 45 | fmt.Println(l.Head.Key) 46 | l.Head = l.Head.Next 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /Ch-4-Trees&Graphs/BinaryTreeImplementation/binaryTree.go: -------------------------------------------------------------------------------- 1 | package BinaryTree 2 | 3 | type BinaryTree struct { 4 | Root *Node 5 | } 6 | 7 | type Node struct { 8 | Key int 9 | Left *Node 10 | Right *Node 11 | } 12 | 13 | 14 | func(b *BinaryTree) Insert(value int) { 15 | if b == nil { 16 | b.Root = &Node{Key: value} 17 | } else { 18 | b.Root.insert(value) 19 | } 20 | } 21 | 22 | func(n *Node) insert(value int) { 23 | // Left Insert 24 | if n.Key > value { 25 | if n.Left == nil { 26 | n.Left = &Node{Key:value} 27 | } else { 28 | n.Left.insert(value) 29 | } 30 | 31 | // Right Insert 32 | } else if n.Key < value { 33 | if n.Right == nil { 34 | n.Right = &Node{Key:value} 35 | } else { 36 | n.Right.insert(value) 37 | } 38 | } 39 | } 40 | 41 | func(b *BinaryTree) Search(value int) bool { 42 | if b.Root == nil { 43 | return false 44 | } else { 45 | return b.Root.search(value) 46 | } 47 | return true 48 | } 49 | 50 | func(n *Node) search(value int) bool { 51 | if n.Key > value { 52 | if n.Left == nil { 53 | return false 54 | } else { 55 | return n.Left.search(value) 56 | } 57 | } else if n.Key < value { 58 | if n.Right == nil { 59 | return false 60 | } else { 61 | return n.Right.search(value) 62 | } 63 | } 64 | return true 65 | } 66 | 67 | func(b *BinaryTree) Delete(value int) *Node { 68 | if b.Root == nil { 69 | return nil 70 | } else { 71 | return b.Root.delete(value) 72 | } 73 | } 74 | 75 | func(n *Node) delete(value int) *Node { 76 | if n.Key < value { 77 | n.Right = n.Right.delete(value) 78 | } else if n.Key > value { 79 | n.Left = n.Left.delete(value) 80 | } else { 81 | if n.Left == nil { 82 | return n.Right 83 | } else if n.Right == nil { 84 | return n.Left 85 | } 86 | min := n.Right.min() 87 | n.Key = min 88 | n.Right = n.Right.delete(min) 89 | } 90 | return n 91 | } 92 | 93 | 94 | func(n *Node) min() int { 95 | if n.Left != nil { 96 | return n.Key 97 | } 98 | return n.Left.min() 99 | } 100 | 101 | -------------------------------------------------------------------------------- /Ch3-Stacks-and-Queues/3.2-Stack-Min/stackMin.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | type Stack struct { 6 | Key interface{} 7 | Container []interface{} 8 | Minimum []interface{} 9 | } 10 | 11 | func (s *Stack) Pop() { 12 | pop := s.Container[len(s.Container)-1] 13 | min := s.Minimum[len(s.Minimum)-1] 14 | if pop.(int) == min.(int) { 15 | s.PopMin() 16 | } 17 | s.Container = append(s.Container[:len(s.Container)-1]) 18 | } 19 | 20 | func (s *Stack) Push(value interface{}) { 21 | s.Container = append(s.Container, value) 22 | // Add element in the min stack 23 | if len(s.Minimum) != 0 { 24 | min := s.PeekMin() 25 | if value.(int) > min.(int) { 26 | return 27 | } else { 28 | s.PushMin(value) 29 | } 30 | } else { 31 | s.PushMin(value) 32 | } 33 | } 34 | 35 | func (s *Stack) Peek() interface{} { 36 | return s.Container[len(s.Container)-1] 37 | } 38 | 39 | func (s *Stack) IsEmpty() bool { 40 | if len(s.Container) != 0 { 41 | return false 42 | } 43 | return true 44 | } 45 | 46 | func (s *Stack) PeekMin() interface{} { 47 | return s.Minimum[len(s.Minimum)-1] 48 | } 49 | 50 | func (s *Stack) PushMin(value interface{}) { 51 | s.Minimum = append(s.Minimum, value) 52 | } 53 | 54 | func (s *Stack) PopMin() { 55 | s.Minimum = append(s.Minimum[:len(s.Minimum)-1]) 56 | } 57 | 58 | func (s *Stack) Min() interface{} { 59 | min := s.Minimum[len(s.Minimum)-1] 60 | return min 61 | } 62 | 63 | func main() { 64 | s := Stack{} 65 | values := []int{7, 8, 5, 9, 5, 2} 66 | for _, v := range values { 67 | s.Push(v) 68 | } 69 | for _, v := range s.Container { 70 | fmt.Println(v) 71 | } 72 | fmt.Println("--- Minimum ---") 73 | for _, v := range s.Minimum { 74 | fmt.Println(v) 75 | } 76 | fmt.Println("--") 77 | fmt.Println(s.Min()) 78 | s.Pop() 79 | fmt.Println("--") 80 | fmt.Println(s.Min()) 81 | s.Pop() 82 | fmt.Println("--") 83 | fmt.Println(s.Min()) 84 | fmt.Println("+++") 85 | for _, v := range s.Container { 86 | fmt.Print(v) 87 | } 88 | s.Pop() 89 | fmt.Println("--") 90 | fmt.Println(s.Min()) 91 | fmt.Println("+++") 92 | for _, v := range s.Container { 93 | fmt.Print(v) 94 | } 95 | fmt.Println("===") 96 | for _, v := range s.Minimum { 97 | fmt.Print(v) 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /Ch3-Stacks-and-Queues/3.3-Stack-of-Plates/stackOfPlates.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "algorithms-in-golang/CTCI/Ch3-Stacks-and-Queues/Stack" 5 | ) 6 | 7 | type StackList struct { 8 | value *Stack.Stack 9 | next *StackList 10 | } 11 | 12 | type StackSet struct { 13 | maxCapacity int 14 | itemCount int 15 | stackCount int 16 | stacks *StackList 17 | } 18 | 19 | func NewStackSet(max int) *StackSet { 20 | stack := &Stack.Stack{} 21 | s := &StackSet{ 22 | stacks: &StackList{ 23 | value:stack, 24 | }, 25 | stackCount: 1, 26 | maxCapacity: max, 27 | } 28 | return s 29 | } 30 | 31 | func(s *StackSet) Push(value interface{}) { 32 | // Total Capacity is maxCapacity * StackCount 33 | if (s.maxCapacity * s.stackCount) == s.itemCount { 34 | stack := &Stack.Stack{} 35 | stack.Push(value) 36 | 37 | list := &StackList{ 38 | value: stack, 39 | next: s.stacks, 40 | } 41 | s.stacks = list 42 | s.stackCount++ 43 | } else { 44 | s.stacks.value.Push(value) 45 | } 46 | s.itemCount++ 47 | } 48 | 49 | func(s *StackSet) Pop() interface{} { 50 | if s.itemCount == 0 { 51 | return nil 52 | } 53 | pop := s.stacks.value.Pop() 54 | s.itemCount-- 55 | 56 | if s.itemCount == ((s.stackCount-1)*s.maxCapacity) { 57 | s.stacks = s.stacks.next 58 | s.stackCount-- 59 | } 60 | return pop 61 | } -------------------------------------------------------------------------------- /Ch3-Stacks-and-Queues/3.4-Queue-via-Stacks/queueViaStacks.go: -------------------------------------------------------------------------------- 1 | /* 2 | Queue via Stacks: Implement a MyQueue class which implements a queue using two stacks. 3 | 4 | The idea is to reverse the order of the elements of the stack by pushing them into a the stack. 5 | */ 6 | 7 | package main 8 | 9 | import ( 10 | "algorithms-in-golang/CTCI/Ch3-Stacks-and-Queues/Stack" 11 | "fmt" 12 | ) 13 | 14 | type MyQueue struct { 15 | stack1 Stack.Stack 16 | stack2 Stack.Stack 17 | } 18 | 19 | func(m *MyQueue) front() interface{} { 20 | if m.stack2.IsEmpty() { 21 | for !m.stack1.IsEmpty() { 22 | m.stack2.Push(m.stack1.Pop()) 23 | } 24 | } 25 | return m.stack2.Pop() 26 | } 27 | 28 | func (m *MyQueue) size() int { 29 | return len(m.stack1.Container) + len(m.stack2.Container) 30 | } 31 | 32 | func (m *MyQueue) enque(value interface{}) { 33 | m.stack1.Push(value) 34 | } 35 | 36 | func (m *MyQueue) dequeue() interface{} { 37 | if m.stack2.IsEmpty() { 38 | for !m.stack1.IsEmpty() { 39 | m.stack2.Push(m.stack1.Pop()) 40 | } 41 | } 42 | return m.stack2.Pop() 43 | } 44 | 45 | func main() { 46 | m := MyQueue{} 47 | value := []int{1, 2, 3, 4, 5, 6, 7, 8, 9} 48 | for _, v := range value { 49 | m.enque(v) 50 | } 51 | for _, v := range m.stack1.Container { 52 | fmt.Print(v) 53 | } 54 | fmt.Println() 55 | fmt.Println(m.dequeue()) 56 | for _, v := range m.stack2.Container { 57 | fmt.Print(v) 58 | } 59 | fmt.Println(m.front()) 60 | } 61 | -------------------------------------------------------------------------------- /Ch3-Stacks-and-Queues/3.5-Sort-Stack/sortStack.go: -------------------------------------------------------------------------------- 1 | /* 2 | Sort Stack: Write a program to sort a stack such that the smallest items are on the top. 3 | You can use an additional temporary stack, but you may not copy the elements into any other data structure 4 | (such as an array). 5 | 6 | The stack supports the following operations: push, pop, peek, and isEmpty. 7 | */ 8 | 9 | package main 10 | 11 | import ( 12 | "algorithms-in-golang/CTCI/Ch3-Stacks-and-Queues/Stack" 13 | "fmt" 14 | ) 15 | 16 | var newStack Stack.Stack 17 | 18 | type sortStack struct { 19 | stack *Stack.Stack 20 | } 21 | 22 | func Sort(stack *Stack.Stack) *Stack.Stack { 23 | for !stack.IsEmpty() { 24 | temp := stack.Pop() 25 | for !newStack.IsEmpty() && newStack.Peek().(int) > temp.(int) { 26 | stack.Push(newStack.Pop()) 27 | } 28 | newStack.Push(temp) 29 | } 30 | stack = &newStack 31 | return stack 32 | } 33 | 34 | func main() { 35 | s := &Stack.Stack{} 36 | values := []int{1, 2, 9, 6, 5} 37 | for _, v := range values { 38 | s.Push(v) 39 | } 40 | fmt.Println() 41 | s = Sort(s) 42 | for _, v := range s.Container { 43 | fmt.Print(v) 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Ch3-Stacks-and-Queues/Queue/queue.go: -------------------------------------------------------------------------------- 1 | package Queue 2 | 3 | type Queue struct { 4 | Key interface{} 5 | Container []interface{} 6 | } 7 | 8 | 9 | func(q *Queue) Add(value interface{}) { 10 | q.Container = append(q.Container, value) 11 | } 12 | 13 | func(q *Queue) Remove() { 14 | q.Container = append(q.Container[1:]) 15 | } 16 | 17 | func(q *Queue) Peek() interface{} { 18 | return q.Container[len(q.Container)-1] 19 | } 20 | 21 | func(q *Queue) IsEmpty() bool { 22 | if len(q.Container) != 0 { 23 | return false 24 | } 25 | return true 26 | } 27 | 28 | -------------------------------------------------------------------------------- /Ch3-Stacks-and-Queues/Stack/stack.go: -------------------------------------------------------------------------------- 1 | package Stack 2 | 3 | type Stack struct { 4 | Key interface{} 5 | Container []interface{} 6 | } 7 | 8 | func(s *Stack) Size() int { 9 | return len(s.Container) 10 | } 11 | 12 | func(s *Stack) Pop() interface{} { 13 | pop := s.Container[len(s.Container)-1] 14 | s.Container = append(s.Container[:len(s.Container)-1]) 15 | return pop 16 | } 17 | 18 | func(s *Stack) Push(value interface{}) { 19 | s.Container = append(s.Container, value) 20 | } 21 | 22 | func(s *Stack) Peek() interface{} { 23 | return s.Container[len(s.Container)-1] 24 | } 25 | 26 | func(s *Stack) IsEmpty() bool { 27 | if len(s.Container) != 0 { 28 | return false 29 | } 30 | return true 31 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Cracking The Coding Interview - Golang 2 | Solutions to Cracking The Coding Interview Questions in **Golang** 3 | 4 | ## Chapter 1 5 | #### Arrays and Strings 6 | 1.1 [IsUnique](https://github.com/procrypt/CrackingTheCodingInterview-Golang/blob/master/Ch-1-Arrays-and-Strings/1.1-IsUnique/isUnique.go) 7 | 8 | 1.2 [Check Permutation](https://github.com/procrypt/CrackingTheCodingInterview-Golang/blob/master/Ch-1-Arrays-and-Strings/1.2-Check-Permutation/checkPermutation.go) 9 | 10 | 1.3 [URLify](https://github.com/procrypt/CrackingTheCodingInterview-Golang/blob/master/Ch-1-Arrays-and-Strings/1.3-URLify/URLify.go) 11 | 12 | 1.4 [Palindrome Permutation](https://github.com/procrypt/CrackingTheCodingInterview-Golang/blob/master/Ch-1-Arrays-and-Strings/1.4-Palindrome-Permutation/palindromePermutation.go) 13 | 14 | 1.5 [One Away](https://github.com/procrypt/CrackingTheCodingInterview-Golang/blob/master/Ch-1-Arrays-and-Strings/1.5-One-Away/oneAway.go) 15 | 16 | 1.6 [String Compression](https://github.com/procrypt/CrackingTheCodingInterview-Golang/blob/master/Ch-1-Arrays-and-Strings/1.6-String-Compression/stringCompression.go) 17 | 18 | 1.7 [Rotate Matrix](https://github.com/procrypt/CrackingTheCodingInterview-Golang/blob/master/Ch-1-Arrays-and-Strings/1.7-RotateMatrix/rotateMatrix.go) 19 | 20 | 1.8 Not Yet Implemented 21 | 22 | 1.9 [String Rotation](https://github.com/procrypt/CrackingTheCodingInterview-Golang/blob/master/Ch-1-Arrays-and-Strings/1.9-String-Rotation/stringRotation.go) 23 | 24 | ## Chapter 2 25 | #### Linked List 26 | [Singly Linked List Implementation](https://github.com/procrypt/CrackingTheCodingInterview-Golang/blob/master/Ch-2-LinkedList/SinglyLinkedList/singlyLinkedList.go) 27 | 28 | 2.1 [Remove Dups](https://github.com/procrypt/CrackingTheCodingInterview-Golang/blob/master/Ch-2-LinkedList/2.1-Remove-Dups/removeDups.go) 29 | 30 | 2.2 [Return Kth to Last](https://github.com/procrypt/CrackingTheCodingInterview-Golang/blob/master/Ch-2-LinkedList/2.2-Return-Kth-to-Last/returnKthToLast.go) 31 | 32 | 2.3 [Delete Middle Node](https://github.com/procrypt/CrackingTheCodingInterview-Golang/blob/master/Ch-2-LinkedList/2.3-Delete-Middle-Node/deleteMiddleNode.go) 33 | 34 | 2.4 [Partition](https://github.com/procrypt/CrackingTheCodingInterview-Golang/blob/master/Ch-2-LinkedList/2.4-Partition/Partition.go) 35 | 36 | 2.5 [Sum Lists](https://github.com/procrypt/CrackingTheCodingInterview-Golang/blob/master/Ch-2-LinkedList/2.5-Sum-Lists/sumLists.go) 37 | 38 | 2.6 [Palindrome](https://github.com/procrypt/CrackingTheCodingInterview-Golang/blob/master/Ch-2-LinkedList/2.6-Palindrome/palindrome.go) 39 | 40 | 2.7 [Intersection](https://github.com/procrypt/CrackingTheCodingInterview-Golang/blob/master/Ch-2-LinkedList/2.7-Intersection/intersection.go) 41 | 42 | 2.8 [Loop Detection](https://github.com/procrypt/CrackingTheCodingInterview-Golang/blob/master/Ch-2-LinkedList/2.8-Loop-Detection/loopDetection.go) 43 | 44 | ## Chapter 3 45 | #### Stacks and Queues 46 | [Stack Implementation](https://github.com/procrypt/CrackingTheCodingInterview-Golang/blob/master/Ch3-Stacks-and-Queues/Stack/stack.go) 47 | 48 | [Queue Implementation](https://github.com/procrypt/CrackingTheCodingInterview-Golang/blob/master/Ch3-Stacks-and-Queues/Queue/queue.go) 49 | 50 | 3.1 Not Yet Implemented 51 | 52 | 3.2 [Stack Min](https://github.com/procrypt/CrackingTheCodingInterview-Golang/blob/master/Ch3-Stacks-and-Queues/3.2-Stack-Min/stackMin.go) 53 | 54 | 3.3 [Stack of Plates](https://github.com/procrypt/CrackingTheCodingInterview-Golang/blob/master/Ch3-Stacks-and-Queues/3.3-Stack-of-Plates/stackOfPlates.go) 55 | 56 | 3.4 [Queue Via Stack](https://github.com/procrypt/CrackingTheCodingInterview-Golang/blob/master/Ch3-Stacks-and-Queues/3.4-Queue-via-Stacks/queueViaStacks.go) 57 | 58 | 3.5 [Sort Stack](https://github.com/procrypt/CrackingTheCodingInterview-Golang/blob/master/Ch3-Stacks-and-Queues/3.5-Sort-Stack/sortStack.go) 59 | --------------------------------------------------------------------------------