Skip to content

Commit 9d64b2e

Browse files
committed
create a hashtable(hashmap)
1 parent cd4bff8 commit 9d64b2e

File tree

8 files changed

+297
-86
lines changed

8 files changed

+297
-86
lines changed

README.md

+86-86
Original file line numberDiff line numberDiff line change
@@ -1,123 +1,123 @@
11
# GO - Data Structures and Algorithms
22

3-
Inspired by the **[Geeksforgeeks - Top 10 Algorithms in Interview Questions](https://www.geeksforgeeks.org/top-10-algorithms-in-interview-questions/amp/)** article, the intent of this repository is to solve these questions using the **Go Language**. GO is a great language choice for technical interviews and hopefully you can find these solutions to the common algorithms/problems easy to understand.
3+
Inspired by the **[Geeksforgeeks - Top 10 Algorithms in Interview Questions](https://www.geeksforgeeks.org/top-10-algorithms-in-interview-questions/amp/)** article, the intent of this repository is to solve these questions using the **Go Language**. GO is a great language choice for technical interviews and hopefully you can find these solutions to the common algorithms/problems easy to understand.
44

5-
Although this is an introductory material to algorithms and data structures, it assumes that you are familiar with GO programming language syntax and basic concepts.
5+
Although this is an introductory material to algorithms and data structures, it assumes that you are familiar with GO programming language syntax and basic concepts.
66

77
WIP, the descriptions of the below `unsolved yet` problems can be found in the [orginal article](https://www.geeksforgeeks.org/top-10-algorithms-in-interview-questions/amp/).
88

99
> ***Contributions are welcomed - solve a problem and submit a PR***.
1010
> ***Contribution guidelines***
11+
>
1112
> * keep the consistency and document the code
1213
> * optimize for readability and simplicity (not over-engineered, best performance is not in scope)
1314
1415
## [Graph](https://github.com/danrusei/algorithms_with_Go/tree/main/graph)
1516

16-
- [x] [Breadth First Search (BFS)](https://github.com/danrusei/algorithms_with_Go/tree/main/graph/traverse_bfs)
17-
- [x] [Depth First Search (DFS)](https://github.com/danrusei/algorithms_with_Go/tree/main/graph/traverse_dfs)
18-
- [x] [Shortest Path from source to all vertices (Dijkstra)](https://github.com/danrusei/algorithms_with_Go/tree/main/graph/dijkstra)
19-
- [] Shortest Path from every vertex to every other vertex (Floyd Warshall)
20-
- [] To detect cycle in a Graph (Union Find)
21-
- [] Minimum Spanning tree (Prim)
22-
- [] Minimum Spanning tree (Kruskal)
23-
- [] Topological Sort
24-
- [x] [Boggle (Find all possible words in a board of characters)](https://github.com/danrusei/algorithms_with_Go/tree/main/graph/boggle)
25-
- [x] [Bridges in a Graph](https://github.com/danrusei/algorithms_with_Go/tree/main/graph/bridges)
17+
* [x] [Breadth First Search (BFS)](https://github.com/danrusei/algorithms_with_Go/tree/main/graph/traverse_bfs)
18+
* [x] [Depth First Search (DFS)](https://github.com/danrusei/algorithms_with_Go/tree/main/graph/traverse_dfs)
19+
* [x] [Shortest Path from source to all vertices (Dijkstra)](https://github.com/danrusei/algorithms_with_Go/tree/main/graph/dijkstra)
20+
* [] Shortest Path from every vertex to every other vertex (Floyd Warshall)
21+
* [] To detect cycle in a Graph (Union Find)
22+
* [] Minimum Spanning tree (Prim)
23+
* [] Minimum Spanning tree (Kruskal)
24+
* [] Topological Sort
25+
* [x] [Boggle (Find all possible words in a board of characters)](https://github.com/danrusei/algorithms_with_Go/tree/main/graph/boggle)
26+
* [x] [Bridges in a Graph](https://github.com/danrusei/algorithms_with_Go/tree/main/graph/bridges)
2627

2728
## [Linked List](https://github.com/danrusei/algorithms_with_Go/tree/main/linkedlist)
2829

29-
- [x] [Insertion of a node in Linked List (On the basis of some constraints)](https://github.com/danrusei/algorithms_with_Go/tree/main/linkedlist/insert_node)
30-
- [] Delete a given node in Linked List (under given constraints)
31-
- [x] [Compare two strings represented as linked lists](https://github.com/danrusei/algorithms_with_Go/tree/main/linkedlist/compare_strings)
32-
- [] Add Two Numbers Represented By Linked Lists
33-
- [] Merge A Linked List Into Another Linked List At Alternate Positions
34-
- [x] [Reverse A List In Groups Of Given Size](https://github.com/danrusei/algorithms_with_Go/tree/main/linkedlist/reverse_by_groups)
35-
- [] Union And Intersection Of 2 Linked Lists
36-
- [x] [Detect And Remove Loop In A Linked List](https://github.com/danrusei/algorithms_with_Go/tree/main/linkedlist/remove_loop)
37-
- [] Merge Sort For Linked Lists
38-
- [x] [Select A Random Node from A Singly Linked List](https://github.com/danrusei/algorithms_with_Go/tree/main/linkedlist/random_node)
30+
* [x] [Insertion of a node in Linked List (On the basis of some constraints)](https://github.com/danrusei/algorithms_with_Go/tree/main/linkedlist/insert_node)
31+
* [] Delete a given node in Linked List (under given constraints)
32+
* [x] [Compare two strings represented as linked lists](https://github.com/danrusei/algorithms_with_Go/tree/main/linkedlist/compare_strings)
33+
* [] Add Two Numbers Represented By Linked Lists
34+
* [] Merge A Linked List Into Another Linked List At Alternate Positions
35+
* [x] [Reverse A List In Groups Of Given Size](https://github.com/danrusei/algorithms_with_Go/tree/main/linkedlist/reverse_by_groups)
36+
* [] Union And Intersection Of 2 Linked Lists
37+
* [x] [Detect And Remove Loop In A Linked List](https://github.com/danrusei/algorithms_with_Go/tree/main/linkedlist/remove_loop)
38+
* [] Merge Sort For Linked Lists
39+
* [x] [Select A Random Node from A Singly Linked List](https://github.com/danrusei/algorithms_with_Go/tree/main/linkedlist/random_node)
3940

4041
## [Tree / Binary Search Tree](https://github.com/danrusei/algorithms_with_Go/tree/main/binary_tree)
4142

42-
- [x] [Find Minimum Depth of a Binary Tree](https://github.com/danrusei/algorithms_with_Go/tree/main/binary_tree/minimum_depth)
43-
- [] Maximum Path Sum in a Binary Tree
44-
- [x] [Check if a given array can represent Preorder Traversal of Binary Search Tree](https://github.com/danrusei/algorithms_with_Go/tree/main/binary_tree/preorder_traversal)
45-
- [] Check whether a binary tree is a full binary tree or not
46-
- [x] [Bottom View Binary Tree](https://github.com/danrusei/algorithms_with_Go/tree/main/binary_tree/bottom_view)
47-
- [] Print Nodes in Top View of Binary Tree
48-
- [x] [Remove nodes on root to leaf paths of length < K](https://github.com/danrusei/algorithms_with_Go/tree/main/binary_tree/remove_nodes)
49-
- [] Lowest Common Ancestor in a Binary Search Tree
50-
- [] Check if a binary tree is subtree of another binary tree
51-
- [x] [Reverse alternate levels of a perfect binary tree](https://github.com/danrusei/algorithms_with_Go/tree/main/binary_tree/reverse_alternate)
43+
* [x] [Find Minimum Depth of a Binary Tree](https://github.com/danrusei/algorithms_with_Go/tree/main/binary_tree/minimum_depth)
44+
* [] Maximum Path Sum in a Binary Tree
45+
* [x] [Check if a given array can represent Preorder Traversal of Binary Search Tree](https://github.com/danrusei/algorithms_with_Go/tree/main/binary_tree/preorder_traversal)
46+
* [] Check whether a binary tree is a full binary tree or not
47+
* [x] [Bottom View Binary Tree](https://github.com/danrusei/algorithms_with_Go/tree/main/binary_tree/bottom_view)
48+
* [] Print Nodes in Top View of Binary Tree
49+
* [x] [Remove nodes on root to leaf paths of length < K](https://github.com/danrusei/algorithms_with_Go/tree/main/binary_tree/remove_nodes)
50+
* [] Lowest Common Ancestor in a Binary Search Tree
51+
* [] Check if a binary tree is subtree of another binary tree
52+
* [x] [Reverse alternate levels of a perfect binary tree](https://github.com/danrusei/algorithms_with_Go/tree/main/binary_tree/reverse_alternate)
5253

5354
## [Other Data Structures](https://github.com/danrusei/algorithms_with_Go/tree/main/other_ds)
5455

55-
- [x] [STACK](https://github.com/danrusei/algorithms_with_Go/tree/main/other_ds/stack)
56-
- [x] [QUEUE](https://github.com/danrusei/algorithms_with_Go/tree/main/other_ds/queue)
57-
- [] HASH-TABLE
56+
* [x] **[Stack](https://github.com/danrusei/algorithms_with_Go/tree/main/other_ds/stack)**
57+
* [x] **[Queue](https://github.com/danrusei/algorithms_with_Go/tree/main/other_ds/queue)**
58+
* [x] **[HashTable](https://github.com/danrusei/algorithms_with_Go/tree/main/other_ds/hashtable)**
5859

5960
## [Sorting And Searching](https://github.com/danrusei/algorithms_with_Go/tree/main/sorting)
6061

61-
- [] Binary Search
62-
- [] Search an element in a sorted and rotated array
63-
- [x] [Bubble Sort](https://github.com/danrusei/algorithms_with_Go/tree/main/sorting/bubble_sort)
64-
- [x] [Insertion Sort](https://github.com/danrusei/algorithms_with_Go/tree/main/sorting/insertion_sort)
65-
- [x] [Merge Sort](https://github.com/danrusei/algorithms_with_Go/tree/main/sorting/merge_sort)
66-
- [x] [Heap Sort (Binary Heap)](https://github.com/danrusei/algorithms_with_Go/tree/main/sorting/heap_sort)
67-
- [x] [Quick Sort](https://github.com/danrusei/algorithms_with_Go/tree/main/sorting/quick_sort)
68-
- [] Interpolation Search
69-
- [] Find Kth Smallest/Largest Element In Unsorted Array
70-
- [] Given a sorted array and a number x, find the pair in array whose sum is closest to x
62+
* [] Binary Search
63+
* [] Search an element in a sorted and rotated array
64+
* [x] [Bubble Sort](https://github.com/danrusei/algorithms_with_Go/tree/main/sorting/bubble_sort)
65+
* [x] [Insertion Sort](https://github.com/danrusei/algorithms_with_Go/tree/main/sorting/insertion_sort)
66+
* [x] [Merge Sort](https://github.com/danrusei/algorithms_with_Go/tree/main/sorting/merge_sort)
67+
* [x] [Heap Sort (Binary Heap)](https://github.com/danrusei/algorithms_with_Go/tree/main/sorting/heap_sort)
68+
* [x] [Quick Sort](https://github.com/danrusei/algorithms_with_Go/tree/main/sorting/quick_sort)
69+
* [] Interpolation Search
70+
* [] Find Kth Smallest/Largest Element In Unsorted Array
71+
* [] Given a sorted array and a number x, find the pair in array whose sum is closest to x
7172

7273
## [Dynamic Programming](https://github.com/danrusei/algorithms_with_Go/tree/main/dynamic)
7374

74-
- [x] [Longest Common Subsequence](https://github.com/danrusei/algorithms_with_Go/tree/main/dynamic/longest_common_subsequence)
75-
- [] Longest Increasing Subsequence
76-
- [] Edit Distance
77-
- [] Minimum Partition
78-
- [x] [Ways to Cover a Distance](https://github.com/danrusei/algorithms_with_Go/tree/main/dynamic/cover_distance)
79-
- [] Longest Path In Matrix
80-
- [x] [Subset Sum Problem](https://github.com/danrusei/algorithms_with_Go/tree/main/dynamic/subset_sum)
81-
- [] Optimal Strategy for a Game
82-
- [] 0-1 Knapsack Problem
83-
- [] Boolean Parenthesization Problem
75+
* [x] [Longest Common Subsequence](https://github.com/danrusei/algorithms_with_Go/tree/main/dynamic/longest_common_subsequence)
76+
* [] Longest Increasing Subsequence
77+
* [] Edit Distance
78+
* [] Minimum Partition
79+
* [x] [Ways to Cover a Distance](https://github.com/danrusei/algorithms_with_Go/tree/main/dynamic/cover_distance)
80+
* [] Longest Path In Matrix
81+
* [x] [Subset Sum Problem](https://github.com/danrusei/algorithms_with_Go/tree/main/dynamic/subset_sum)
82+
* [] Optimal Strategy for a Game
83+
* [] 0-1 Knapsack Problem
84+
* [] Boolean Parenthesization Problem
8485

8586
## [BIT Manipulation](https://github.com/danrusei/algorithms_with_Go/tree/main/bitwise)
8687

87-
- [x] [Maximum Subarray XOR](https://github.com/danrusei/algorithms_with_Go/tree/main/bitwise/max_xor)
88-
- [] Magic Number
89-
- [] Sum of bit differences among all pairs
90-
- [x] [Swap All Odds And Even Bits](https://github.com/danrusei/algorithms_with_Go/tree/main/bitwise/swapp_odd_even)
91-
- [] Find the element that appears once
92-
- [x] [Binary representation of a given number](https://github.com/danrusei/algorithms_with_Go/tree/main/bitwise/decimal_to_binary)
93-
- [] Count total set bits in all numbers from 1 to n
94-
- [x] [Rotate bits of a number](https://github.com/danrusei/algorithms_with_Go/tree/main/bitwise/rotate_bits)
95-
- [] Count number of bits to be flipped to convert A to B
96-
- [x] [Find Next Sparse Number](https://github.com/danrusei/algorithms_with_Go/tree/main/bitwise/next_sparse)
88+
* [x] [Maximum Subarray XOR](https://github.com/danrusei/algorithms_with_Go/tree/main/bitwise/max_xor)
89+
* [] Magic Number
90+
* [] Sum of bit differences among all pairs
91+
* [x] [Swap All Odds And Even Bits](https://github.com/danrusei/algorithms_with_Go/tree/main/bitwise/swapp_odd_even)
92+
* [] Find the element that appears once
93+
* [x] [Binary representation of a given number](https://github.com/danrusei/algorithms_with_Go/tree/main/bitwise/decimal_to_binary)
94+
* [] Count total set bits in all numbers from 1 to n
95+
* [x] [Rotate bits of a number](https://github.com/danrusei/algorithms_with_Go/tree/main/bitwise/rotate_bits)
96+
* [] Count number of bits to be flipped to convert A to B
97+
* [x] [Find Next Sparse Number](https://github.com/danrusei/algorithms_with_Go/tree/main/bitwise/next_sparse)
9798

9899
## [Number Theory](https://github.com/danrusei/algorithms_with_Go/tree/main/numbers)
99100

100-
- [] Modular Exponentiation
101-
- [x] [Modular multiplicative inverse](https://github.com/danrusei/algorithms_with_Go/tree/main/numbers/multiplicative)
102-
- [] Primality Test | Set 2 (Fermat Method)
103-
- [] Euler’s Totient Function
104-
- [x] [Sieve of Eratosthenes](https://github.com/danrusei/algorithms_with_Go/tree/main/numbers/eratosthenes)
105-
- [] Convex Hull
106-
- [] Basic and Extended Euclidean algorithms
107-
- [x] [Segmented Sieve](https://github.com/danrusei/algorithms_with_Go/tree/main/numbers/segmented)
108-
- [] Chinese remainder theorem
109-
- [] Lucas Theorem
101+
* [] Modular Exponentiation
102+
* [x] [Modular multiplicative inverse](https://github.com/danrusei/algorithms_with_Go/tree/main/numbers/multiplicative)
103+
* [] Primality Test | Set 2 (Fermat Method)
104+
* [] Euler’s Totient Function
105+
* [x] [Sieve of Eratosthenes](https://github.com/danrusei/algorithms_with_Go/tree/main/numbers/eratosthenes)
106+
* [] Convex Hull
107+
* [] Basic and Extended Euclidean algorithms
108+
* [x] [Segmented Sieve](https://github.com/danrusei/algorithms_with_Go/tree/main/numbers/segmented)
109+
* [] Chinese remainder theorem
110+
* [] Lucas Theorem
110111

111112
## [String / Array](https://github.com/danrusei/algorithms_with_Go/tree/main/strings)
112113

113-
- [x] [Reverse an array without affecting special characters](https://github.com/danrusei/algorithms_with_Go/tree/main/strings/reverse_alpha)
114-
- [] All Possible Palindromic Partitions
115-
- [] Count triplets with sum smaller than a given value
116-
- [x] [Convert array into Zig-Zag fashion](https://github.com/danrusei/algorithms_with_Go/tree/main/strings/zig_zag)
117-
- [] Generate all possible sorted arrays from alternate elements of two given sorted arrays
118-
- [] Pythagorean Triplet in an array
119-
- [x] [Length of the largest subarray with contiguous elements](https://github.com/danrusei/algorithms_with_Go/tree/main/strings/largest_subarray)
120-
- [] Find the smallest positive integer value that cannot be represented as sum of any subset of a given array
121-
- [x] [Smallest subarray with sum greater than a given value](https://github.com/danrusei/algorithms_with_Go/tree/main/strings/smallest_subarray)
122-
- [] Stock Buy Sell to Maximize Profit
123-
114+
* [x] [Reverse an array without affecting special characters](https://github.com/danrusei/algorithms_with_Go/tree/main/strings/reverse_alpha)
115+
* [] All Possible Palindromic Partitions
116+
* [] Count triplets with sum smaller than a given value
117+
* [x] [Convert array into Zig-Zag fashion](https://github.com/danrusei/algorithms_with_Go/tree/main/strings/zig_zag)
118+
* [] Generate all possible sorted arrays from alternate elements of two given sorted arrays
119+
* [] Pythagorean Triplet in an array
120+
* [x] [Length of the largest subarray with contiguous elements](https://github.com/danrusei/algorithms_with_Go/tree/main/strings/largest_subarray)
121+
* [] Find the smallest positive integer value that cannot be represented as sum of any subset of a given array
122+
* [x] [Smallest subarray with sum greater than a given value](https://github.com/danrusei/algorithms_with_Go/tree/main/strings/smallest_subarray)
123+
* [] Stock Buy Sell to Maximize Profit

other_ds/hashtable/README.md

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# HashTable (HashMap)
2+
3+
Source: [Wikipedia](https://en.wikipedia.org/wiki/Hash_table)
4+
5+
A Hash Table (Hash Map) is a data structure that implements an associative array abstract data type, a structure that can `map keys to values`. A hash table uses a hash function to compute an index into an array of buckets or slots, from which the desired value can be found. During lookup, the key is hashed and the resulting hash indicates where the corresponding value is stored.
6+
7+
Ideally, the hash function will assign each key to a unique bucket, but most hash table designs employ an imperfect hash function, which might cause hash collisions where the hash function generates the same index for more than one key. Such collisions are typically accommodated in some way.
8+
9+
## Collision resolution with linked list chaining
10+
11+
The most common hash table implementation uses chaining with linked lists to resolve collisions. This combines the best properties of arrays and linked lists.
12+
13+
Hash table operations are performed in two steps:
14+
15+
* a key is converted into an integer index by using a hash function.
16+
* the index decides the bucket (which is a linked list) where the key-value pair record belongs.
17+
18+
![hash table](hash_table.png)
19+
20+
As you can see in the above diagram, two keys have the same calculated bucket, `152`, meaning that there is a collision that can be resolved by chaining the records into a linked list.
21+
Check Out the [Linked List material](https://github.com/danrusei/algorithms_with_Go/tree/main/linkedlist) if you are not familiar with this data structure.
22+
23+
## Result
24+
25+
The underlying array used for my HashTable implementation has a length of 16. I created a utility function String() to print out how the KV elements are distributed across the array. The colision was resolved by chaining the elements from the same bucket into a linked list.
26+
27+
```bash
28+
$ go run bin/main.go
29+
Bucket 0 has the following Nodes: {first 1}
30+
Bucket 1 has the following Nodes: {tenth 10} {seventh 7}
31+
Bucket 2 has no Nodes
32+
Bucket 3 has no Nodes
33+
Bucket 4 has the following Nodes: {second 2}
34+
Bucket 5 has the following Nodes: {eleventh 11}
35+
Bucket 6 has the following Nodes: {nineth 9} {sixth 6}
36+
Bucket 7 has the following Nodes: {eigth 8} {fifth 5}
37+
Bucket 8 has no Nodes
38+
Bucket 9 has the following Nodes: {thrid 3}
39+
Bucket 10 has the following Nodes: {fourth 4}
40+
Bucket 11 has no Nodes
41+
Bucket 12 has no Nodes
42+
Bucket 13 has no Nodes
43+
Bucket 14 has no Nodes
44+
Bucket 15 has no Nodes
45+
```

other_ds/hashtable/bin/main.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
import (
4+
"github.com/danrusei/algorithms_with_go/other_ds/hashtable"
5+
)
6+
7+
func main() {
8+
9+
hashmap := hashtable.New()
10+
hashmap.AddKeyValue("first", 1)
11+
hashmap.AddKeyValue("second", 2)
12+
hashmap.AddKeyValue("thrid", 3)
13+
hashmap.AddKeyValue("fourth", 4)
14+
hashmap.AddKeyValue("fifth", 5)
15+
hashmap.AddKeyValue("sixth", 6)
16+
hashmap.AddKeyValue("seventh", 7)
17+
hashmap.AddKeyValue("eigth", 8)
18+
hashmap.AddKeyValue("nineth", 9)
19+
hashmap.AddKeyValue("tenth", 10)
20+
hashmap.AddKeyValue("eleventh", 11)
21+
22+
hashmap.String()
23+
}

other_ds/hashtable/go.mod

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/danrusei/algorithms_with_go/other_ds/hashtable
2+
3+
go 1.15
4+
5+
require github.com/danrusei/algorithms_with_go/linkedlist v0.0.0-20210125085227-cd4bff82e7da

other_ds/hashtable/go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/danrusei/algorithms_with_go/linkedlist v0.0.0-20210125085227-cd4bff82e7da h1:DOPoDxOOM08z89857bKTZ21+8ZfdTcTFYktbqw2Lvas=
2+
github.com/danrusei/algorithms_with_go/linkedlist v0.0.0-20210125085227-cd4bff82e7da/go.mod h1:NvwyzxbWSyZ4nFJnb4zQeKo8BYBRhs/uEAz/epGzG7Y=

other_ds/hashtable/hash_table.png

85.3 KB
Loading

0 commit comments

Comments
 (0)