Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complete Design-2 #2050

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 122 additions & 0 deletions DesignHashMap.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// to design the hashmap we basically need to understand the hashing :- so hashing means maping the
// key with hash function to memory registarion with hash code
// now the primary data structure used for the hash map can be the array as it helps us access each
// variables with that index
// if we perform 1:1 mapping on the given data strcuture , the required data structure length would be
// the maximum length of the expected output in this case 10^6
// so we can use a different hash function
// primart hash function - key%length
// but it will introduce the problem of collition
// we can address this which several techniques
// 1. linear probing
// 2. quadratic probing
// 3. BST chaining
// 4. Linear chaining
// 5. double hashing

// let us implement the second hash function with the linear chaining
// we will create the first data strcutre of the size 1000 as we want to balance the size and the
// time it will require to itrate through each value pairs .

class MyHashMap {
public:

struct Node
{
int val;
int key;
Node *next;
};

Node* storage[10000];
int buckets;
MyHashMap() {
this->buckets = 10000;
for(int i = 0 ; i < buckets; i++)
{
storage[i]= nullptr;
}
}

int primaryFunction(int key){
return key%buckets;
}
Node *getPrevNode(int primaryIndex, int key){
Node *curr = storage[primaryIndex];
Node *prev = NULL;
while(curr != NULL && curr->key !=key)
{
prev = curr;
curr = curr->next;
}
return prev;
}
void put(int key, int value) {
// now to add the value to the we will need to check the hash function
int primaryIndex = primaryFunction(key);
if(storage[primaryIndex] == NULL)
{
Node *dummy = new Node;
dummy->val = -1;
dummy->key = -1;
dummy->next = NULL;
storage[primaryIndex] = dummy;

// now add a new node to that dummy node
Node *new_node = new Node;
new_node->key = key;
new_node->val = value;
new_node -> next = NULL;
storage[primaryIndex]->next = new_node;
}
Node *prev = getPrevNode(primaryIndex,key);

if(prev->next == NULL)
{
Node *new_node = new Node;
new_node->key = key;
new_node->val = value;
new_node -> next = NULL;
prev->next = new_node;
}
else
{
prev->next->val = value;
}

}

int get(int key) {
// to get the value based on the key we will need to see if it is intialized
int primaryIndex = primaryFunction(key);
if(storage[primaryIndex] == NULL)
return -1;
Node *prev = getPrevNode(primaryIndex,key);
if(prev->next == NULL)
return -1;
return prev->next->val;
}

void remove(int key) {
// to remove we will need to first find the element
int primaryIndex = primaryFunction(key);
if(storage[primaryIndex] == NULL)
return;
Node *prev = getPrevNode(primaryIndex,key);
if(prev->next == NULL)
return;
//found it
Node *temp = prev->next;
prev->next = temp->next;
temp->next = NULL;

}
};

/**
* Your MyHashMap object will be instantiated and called as such:
* MyHashMap* obj = new MyHashMap();
* obj->put(key,value);
* int param_2 = obj->get(key);
* obj->remove(key);
*/
57 changes: 57 additions & 0 deletions ImplementQueue.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// now before starting with the implementation queues we need to undetstand the key difference between them
// stack and queue so the queue works on the property of the first in and first out
// and stack, has property is the last in first out
// so we can use this property to store the values of the queue
class MyQueue {
public:
stack<int>inStack;
stack<int>outStack;
MyQueue() {

}

void push(int x) {
inStack.push(x);
}

int pop() {
if(outStack.empty())
{
while(!inStack.empty())
{
int x = inStack.top();
inStack.pop();
outStack.push(x);
}
}
int x = outStack.top();
outStack.pop();
return x;
}

int peek() {
if(outStack.empty())
{
while(!inStack.empty())
{
int x = inStack.top();
inStack.pop();
outStack.push(x);
}
}
return outStack.top();
}

bool empty() {
return inStack.empty() && outStack.empty();
}
};

/**
* Your MyQueue object will be instantiated and called as such:
* MyQueue* obj = new MyQueue();
* obj->push(x);
* int param_2 = obj->pop();
* int param_3 = obj->peek();
* bool param_4 = obj->empty();
*/