Hash Map — Insert, Get and Remove
Practice this coding question on Coding Gurukul.
Problem statement
Maintain a hash map that starts empty. - PUT key x: store x for key, replacing any previous value. - GET key: print the stored value, or MISSING if key does not exist. - REMOVE key: delete the entry if present. - SIZE: print the number of distinct keys. Only GET and SIZE produce output. A value of 0 is a stored value, not a missing key. Use expected O(1) time per command. A language hash-map container is allowed. Worked example PUT bolt 5 followed by PUT bolt 0 leaves one key with value 0. GET bolt prints 0 and SIZE prints 1. After removal, GET bolt prints MISSING and SIZE prints 0.
Input format
First line: q. Next q lines: PUT key x, GET key, REMOVE key, or SIZE.
Output format
One line for each GET or SIZE.
Constraints
1 <= q <= 100000; keys contain 1–20 lowercase English letters; -10^9 <= x <= 10^9. At least one output command.
Sample input
7 PUT bolt 5 PUT bolt 0 GET bolt SIZE REMOVE bolt GET bolt SIZE
Sample output
0 1 MISSING 0
Sign in to run or submit a solution in C++, Java or Python.