`{"id":"9ea1e09e-3bb0-469e-a91f-0fa104d85b83","name":"Write Hashmap","description":"1. You are required to complete the code of our Hashmap class. Please watch the question video carefully. The theoretical details of required functionality is explained in detail there. Implement the functions to achieve what is explained in the theoretical discussion in question video.\r\n2. Input and Output is managed for you.","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"None","sampleCode":{"cpp":{"code":""},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static class HashMap<K, V> {\r\n private class HMNode {\r\n K key;\r\n V value;\r\n\r\n HMNode(K key, V value) {\r\n this.key = key;\r\n this.value = value;\r\n }\r\n }\r\n\r\n private int size; // n\r\n private LinkedList<HMNode>[] buckets; // N = buckets.length\r\n\r\n public HashMap() {\r\n initbuckets(4);\r\n size = 0;\r\n }\r\n\r\n private void initbuckets(int N) {\r\n buckets = new LinkedList[N];\r\n for (int bi = 0; bi < buckets.length; bi++) {\r\n buckets[bi] = new LinkedList<>();\r\n }\r\n }\r\n\r\n public void put(K key, V value) throws Exception {\r\n // write your code here\r\n }\r\n\r\n public V get(K key) throws Exception {\r\n // write your code here\r\n }\r\n\r\n public boolean containsKey(K key) {\r\n // write your code here\r\n }\r\n\r\n public V remove(K key) throws Exception {\r\n // write your code here\r\n }\r\n\r\n public ArrayList<K> keyset() throws Exception {\r\n // write your code here\r\n }\r\n\r\n public int size() {\r\n // write your code here\r\n }\r\n\r\n public void display() {\r\n System.out.println(\"Display Begins\");\r\n for (int bi = 0; bi < buckets.length; bi++) {\r\n System.out.print(\"Bucket\" + bi + \" \");\r\n for (HMNode node : buckets[bi]) {\r\n System.out.print( node.key + \"@\" + node.value + \" \");\r\n }\r\n System.out.println(\".\");\r\n }\r\n System.out.println(\"Display Ends\");\r\n }\r\n}\r\n\r\n public static void main(String[] args) throws Exception {\r\n BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\r\n HashMap<String, Integer> map = new HashMap();\r\n\r\n String str = br.readLine();\r\n while (str.equals(\"quit\") == false) {\r\n if (str.startsWith(\"put\")) {\r\n String[] parts = str.split(\" \");\r\n String key = parts[1];\r\n Integer val = Integer.parseInt(parts[2]);\r\n map.put(key, val);\r\n } else if (str.startsWith(\"get\")) {\r\n String[] parts = str.split(\" \");\r\n String key = parts[1];\r\n System.out.println(map.get(key));\r\n } else if (str.startsWith(\"containsKey\")) {\r\n String[] parts = str.split(\" \");\r\n String key = parts[1];\r\n System.out.println(map.containsKey(key));\r\n } else if (str.startsWith(\"remove\")) {\r\n String[] parts = str.split(\" \");\r\n String key = parts[1];\r\n System.out.println(map.remove(key));\r\n } else if (str.startsWith(\"size\")) {\r\n System.out.println(map.size());\r\n } else if (str.startsWith(\"keyset\")) {\r\n System.out.println(map.keyset());\r\n } else if (str.startsWith(\"display\")) {\r\n map.display();\r\n }\r\n str = br.readLine();\r\n }\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"put India 135\r\nput Aus 5\r\nput Canada 3\r\ndisplay\r\nget China\r\nremove Aus\r\nget Aus\r\ncontainsKey US\r\nput US 10\r\nput UK 20\r\nremove US\r\ncontainsKey US\r\nput Pak 80\r\nput China 200\r\ndisplay\r\nput Utopia 0\r\ndisplay\r\nput Nigeria 3\r\ndisplay\r\nput India 138\r\ndisplay\r\nput Sweden 1\r\ndisplay\r\nput finland 20\r\ndisplay\r\nquit","sampleOutput":"Display Begins\r\nBucket0 .\r\nBucket1 .\r\nBucket2 Canada@3 .\r\nBucket3 India@135 Aus@5 .\r\nDisplay Ends\r\nnull\r\n5\r\nnull\r\nfalse\r\n10\r\nfalse\r\nDisplay Begins\r\nBucket0 .\r\nBucket1 .\r\nBucket2 Canada@3 UK@20 Pak@80 .\r\nBucket3 India@135 China@200 .\r\nDisplay Ends\r\nDisplay Begins\r\nBucket0 Utopia@0 .\r\nBucket1 .\r\nBucket2 Canada@3 UK@20 Pak@80 .\r\nBucket3 India@135 China@200 .\r\nDisplay Ends\r\nDisplay Begins\r\nBucket0 Utopia@0 .\r\nBucket1 .\r\nBucket2 Canada@3 UK@20 Pak@80 .\r\nBucket3 India@135 China@200 Nigeria@3 .\r\nDisplay Ends\r\nDisplay Begins\r\nBucket0 Utopia@0 .\r\nBucket1 .\r\nBucket2 Canada@3 UK@20 Pak@80 .\r\nBucket3 India@138 China@200 Nigeria@3 .\r\nDisplay Ends\r\nDisplay Begins\r\nBucket0 Utopia@0 Sweden@1 .\r\nBucket1 .\r\nBucket2 Canada@3 UK@20 Pak@80 .\r\nBucket3 India@138 China@200 Nigeria@3 .\r\nDisplay Ends\r\nDisplay Begins\r\nBucket0 Utopia@0 .\r\nBucket1 .\r\nBucket2 Pak@80 finland@20 .\r\nBucket3 .\r\nBucket4 Sweden@1 .\r\nBucket5 .\r\nBucket6 Canada@3 UK@20 .\r\nBucket7 India@138 China@200 Nigeria@3 .\r\nDisplay Ends\r\n","questionVideo":"https://www.youtube.com/embed/2ijH5vcKIxQ?end=754","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"1254d21e-2209-40bc-9e24-00d135ace68d","name":"Hashmap And Heap For Beginners","slug":"hashmap-and-heap-for-beginners","type":0},{"id":"527a245e-65c9-4d88-990d-06cc687eb2b1","name":"Write Hashmap","slug":"write-hashmap","type":1}],"next":{"id":"9e630ffd-4a57-4956-b5e1-a79c88813a68","name":"Write Hashmap","type":3,"slug":"write-hashmap"},"prev":{"id":"42424e1a-0d5d-442a-ac4b-2d45e22950b1","name":"Write Priority Queue Using Heap","type":3,"slug":"write-priority-queue-using-heap"}}}`

# Write Hashmap

1. You are required to complete the code of our Hashmap class. Please watch the question video carefully. The theoretical details of required functionality is explained in detail there. Implement the functions to achieve what is explained in the theoretical discussion in question video. 2. Input and Output is managed for you.

`{"id":"9ea1e09e-3bb0-469e-a91f-0fa104d85b83","name":"Write Hashmap","description":"1. You are required to complete the code of our Hashmap class. Please watch the question video carefully. The theoretical details of required functionality is explained in detail there. Implement the functions to achieve what is explained in the theoretical discussion in question video.\r\n2. Input and Output is managed for you.","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"None","sampleCode":{"cpp":{"code":""},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static class HashMap<K, V> {\r\n private class HMNode {\r\n K key;\r\n V value;\r\n\r\n HMNode(K key, V value) {\r\n this.key = key;\r\n this.value = value;\r\n }\r\n }\r\n\r\n private int size; // n\r\n private LinkedList<HMNode>[] buckets; // N = buckets.length\r\n\r\n public HashMap() {\r\n initbuckets(4);\r\n size = 0;\r\n }\r\n\r\n private void initbuckets(int N) {\r\n buckets = new LinkedList[N];\r\n for (int bi = 0; bi < buckets.length; bi++) {\r\n buckets[bi] = new LinkedList<>();\r\n }\r\n }\r\n\r\n public void put(K key, V value) throws Exception {\r\n // write your code here\r\n }\r\n\r\n public V get(K key) throws Exception {\r\n // write your code here\r\n }\r\n\r\n public boolean containsKey(K key) {\r\n // write your code here\r\n }\r\n\r\n public V remove(K key) throws Exception {\r\n // write your code here\r\n }\r\n\r\n public ArrayList<K> keyset() throws Exception {\r\n // write your code here\r\n }\r\n\r\n public int size() {\r\n // write your code here\r\n }\r\n\r\n public void display() {\r\n System.out.println(\"Display Begins\");\r\n for (int bi = 0; bi < buckets.length; bi++) {\r\n System.out.print(\"Bucket\" + bi + \" \");\r\n for (HMNode node : buckets[bi]) {\r\n System.out.print( node.key + \"@\" + node.value + \" \");\r\n }\r\n System.out.println(\".\");\r\n }\r\n System.out.println(\"Display Ends\");\r\n }\r\n}\r\n\r\n public static void main(String[] args) throws Exception {\r\n BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\r\n HashMap<String, Integer> map = new HashMap();\r\n\r\n String str = br.readLine();\r\n while (str.equals(\"quit\") == false) {\r\n if (str.startsWith(\"put\")) {\r\n String[] parts = str.split(\" \");\r\n String key = parts[1];\r\n Integer val = Integer.parseInt(parts[2]);\r\n map.put(key, val);\r\n } else if (str.startsWith(\"get\")) {\r\n String[] parts = str.split(\" \");\r\n String key = parts[1];\r\n System.out.println(map.get(key));\r\n } else if (str.startsWith(\"containsKey\")) {\r\n String[] parts = str.split(\" \");\r\n String key = parts[1];\r\n System.out.println(map.containsKey(key));\r\n } else if (str.startsWith(\"remove\")) {\r\n String[] parts = str.split(\" \");\r\n String key = parts[1];\r\n System.out.println(map.remove(key));\r\n } else if (str.startsWith(\"size\")) {\r\n System.out.println(map.size());\r\n } else if (str.startsWith(\"keyset\")) {\r\n System.out.println(map.keyset());\r\n } else if (str.startsWith(\"display\")) {\r\n map.display();\r\n }\r\n str = br.readLine();\r\n }\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"put India 135\r\nput Aus 5\r\nput Canada 3\r\ndisplay\r\nget China\r\nremove Aus\r\nget Aus\r\ncontainsKey US\r\nput US 10\r\nput UK 20\r\nremove US\r\ncontainsKey US\r\nput Pak 80\r\nput China 200\r\ndisplay\r\nput Utopia 0\r\ndisplay\r\nput Nigeria 3\r\ndisplay\r\nput India 138\r\ndisplay\r\nput Sweden 1\r\ndisplay\r\nput finland 20\r\ndisplay\r\nquit","sampleOutput":"Display Begins\r\nBucket0 .\r\nBucket1 .\r\nBucket2 Canada@3 .\r\nBucket3 India@135 Aus@5 .\r\nDisplay Ends\r\nnull\r\n5\r\nnull\r\nfalse\r\n10\r\nfalse\r\nDisplay Begins\r\nBucket0 .\r\nBucket1 .\r\nBucket2 Canada@3 UK@20 Pak@80 .\r\nBucket3 India@135 China@200 .\r\nDisplay Ends\r\nDisplay Begins\r\nBucket0 Utopia@0 .\r\nBucket1 .\r\nBucket2 Canada@3 UK@20 Pak@80 .\r\nBucket3 India@135 China@200 .\r\nDisplay Ends\r\nDisplay Begins\r\nBucket0 Utopia@0 .\r\nBucket1 .\r\nBucket2 Canada@3 UK@20 Pak@80 .\r\nBucket3 India@135 China@200 Nigeria@3 .\r\nDisplay Ends\r\nDisplay Begins\r\nBucket0 Utopia@0 .\r\nBucket1 .\r\nBucket2 Canada@3 UK@20 Pak@80 .\r\nBucket3 India@138 China@200 Nigeria@3 .\r\nDisplay Ends\r\nDisplay Begins\r\nBucket0 Utopia@0 Sweden@1 .\r\nBucket1 .\r\nBucket2 Canada@3 UK@20 Pak@80 .\r\nBucket3 India@138 China@200 Nigeria@3 .\r\nDisplay Ends\r\nDisplay Begins\r\nBucket0 Utopia@0 .\r\nBucket1 .\r\nBucket2 Pak@80 finland@20 .\r\nBucket3 .\r\nBucket4 Sweden@1 .\r\nBucket5 .\r\nBucket6 Canada@3 UK@20 .\r\nBucket7 India@138 China@200 Nigeria@3 .\r\nDisplay Ends\r\n","questionVideo":"https://www.youtube.com/embed/2ijH5vcKIxQ?end=754","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"1254d21e-2209-40bc-9e24-00d135ace68d","name":"Hashmap And Heap For Beginners","slug":"hashmap-and-heap-for-beginners","type":0},{"id":"527a245e-65c9-4d88-990d-06cc687eb2b1","name":"Write Hashmap","slug":"write-hashmap","type":1}],"next":{"id":"9e630ffd-4a57-4956-b5e1-a79c88813a68","name":"Write Hashmap","type":3,"slug":"write-hashmap"},"prev":{"id":"42424e1a-0d5d-442a-ac4b-2d45e22950b1","name":"Write Priority Queue Using Heap","type":3,"slug":"write-priority-queue-using-heap"}}}`

Editor

# Write Hashmap

easy

1. You are required to complete the code of our Hashmap class. Please watch the question video carefully. The theoretical details of required functionality is explained in detail there. Implement the functions to achieve what is explained in the theoretical discussion in question video. 2. Input and Output is managed for you.

None

## Format

### Input

Input is managed for you

### Output

Output is managed for you

## Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}put India 135 put Aus 5 put Canada 3 display get China remove Aus get Aus containsKey US put US 10 put UK 20 remove US containsKey US put Pak 80 put China 200 display put Utopia 0 display put Nigeria 3 display put India 138 display put Sweden 1 display put finland 20 display quit```

### Sample Output

```.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}Display Begins Bucket0 . Bucket1 . Bucket2 Canada@3 . Bucket3 India@135 Aus@5 . Display Ends null 5 null false 10 false Display Begins Bucket0 . Bucket1 . Bucket2 Canada@3 UK@20 Pak@80 . Bucket3 India@135 China@200 . Display Ends Display Begins Bucket0 Utopia@0 . Bucket1 . Bucket2 Canada@3 UK@20 Pak@80 . Bucket3 India@135 China@200 . Display Ends Display Begins Bucket0 Utopia@0 . Bucket1 . Bucket2 Canada@3 UK@20 Pak@80 . Bucket3 India@135 China@200 Nigeria@3 . Display Ends Display Begins Bucket0 Utopia@0 . Bucket1 . Bucket2 Canada@3 UK@20 Pak@80 . Bucket3 India@138 China@200 Nigeria@3 . Display Ends Display Begins Bucket0 Utopia@0 Sweden@1 . Bucket1 . Bucket2 Canada@3 UK@20 Pak@80 . Bucket3 India@138 China@200 Nigeria@3 . Display Ends Display Begins Bucket0 Utopia@0 . Bucket1 . Bucket2 Pak@80 finland@20 . Bucket3 . Bucket4 Sweden@1 . Bucket5 . Bucket6 Canada@3 UK@20 . Bucket7 India@138 China@200 Nigeria@3 . Display Ends ```

Question Video

Discussions

Show Discussion

Related Resources