Skip to content
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
79 changes: 79 additions & 0 deletions Design-hashmap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
class MyHashMap {
class Node{
int key;
int val;
Node next;
Node(int key, int val){
this.key = key;
this.val = val;
}
}
public Node[] storage; // we are keeping array of nodes

private int hash(int key){
return key% 10000;
}

public MyHashMap() {
this.storage = new Node[10000]; //we do it 10,000 instead of 10,000
}

private Node find(Node head, int key){
Node prev = null;
Node curr = head;
while(curr != null && curr.key != key){ //for iteration of linkedlist
prev = curr;
curr = curr.next;
}
return prev;
}

public void put(int key, int value) {
int idx = hash(key);
if(storage[idx] == null){ // there is no element, initialize a dummy node
storage[idx] = new Node(-1,-1);
}
Node prev = find(storage[idx], key);
//if curr key already exists in list
if(prev.next != null)
prev.next.val = value;
//else create a fresh node
else
prev.next = new Node(key, value);

}

public int get(int key) {
int idx = hash(key);
if(storage[idx] == null){ //if it is not initialized only then nothing is there
return -1;
}
Node prev = find(storage[idx], key);
if(prev.next!=null){ //curr key exists
return prev.next.val;
}
return -1;
}

public void remove(int key) {
int idx = hash(key);
if(storage[idx] == null){ // there is no element simply return
return;
}
Node prev = find(storage[idx], key);
if(prev.next==null) return; //curr doest not exist then simple return
//to delete in middle
Node curr = prev.next;
prev.next = prev.next.next;
curr.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);
*/
44 changes: 44 additions & 0 deletions Implement-queue-using-stacks.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class MyQueue {
private Stack<Integer> in;
private Stack<Integer> out;
public MyQueue() {
this.in = new Stack<>();
this.out = new Stack<>();
}

public void push(int x) {
in.push(x); //simple
}
//if out stack is empty pop element from instack and push is in outstack
public int pop() {

if(out.isEmpty()){
while(!in.isEmpty()){
out.push(in.pop());
}
}
return out.pop();
}

public int peek() {
if(out.isEmpty()){
while(!in.isEmpty()){
out.push(in.pop());
}
}
return out.peek();
}

public boolean empty() {
return in.isEmpty() && out.isEmpty();
}
}

/**
* 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();
* boolean param_4 = obj.empty();
*/
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ Explain your approach in **three sentences only** at top of your code


## Problem 1: (https://leetcode.com/problems/implement-queue-using-stacks/)

Use 2 stacks instack & outstack
For peep() and pop() - if outstack is empty, then util the instack is empty try pushing values from instack to outstack then pop element from outstack.

## Problem 2:
Design Hashmap (https://leetcode.com/problems/design-hashmap/)

we are using an storage array of Node w 10,000 and Linkedlist of 100 so traveling linkedlist gets faster.
create a find function with 2 pointers prev,curr that iterates over the linkedlist to search for the key and return the prev Node.
put() has 2 scenarios - if key already exists or create fresh Node, delete() - if key does not exist or if it is in middle.