Challenge - 5 Problems
Hash Table Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Java HashMap after insertions
What is the printed state of the HashMap after inserting these key-value pairs in order: (1, "One"), (2, "Two"), (3, "Three")?
DSA Java
import java.util.HashMap; public class Main { public static void main(String[] args) { HashMap<Integer, String> map = new HashMap<>(); map.put(1, "One"); map.put(2, "Two"); map.put(3, "Three"); System.out.println(map); } }
Attempts:
2 left
💡 Hint
HashMap does not guarantee order, but small maps often print in insertion order in Java 8+.
✗ Incorrect
The HashMap prints entries in the order they were inserted for small maps in Java 8 and later. So the output shows keys 1, 2, 3 in that order with their values.
🧠 Conceptual
intermediateUnderstanding hash function output range
If a hash function for a hash table returns values from 0 to 15, what is the maximum number of buckets in the hash table?
Attempts:
2 left
💡 Hint
Think about how many distinct values are possible from 0 to 15 inclusive.
✗ Incorrect
The range 0 to 15 includes 16 distinct values, so the hash table can have 16 buckets indexed from 0 to 15.
🔧 Debug
advancedIdentify the error in this hash function implementation
What error will this Java hash function code cause when used in a hash table?
DSA Java
public int hash(int key) { return key % 0; }
Attempts:
2 left
💡 Hint
Modulo by zero is not allowed in Java.
✗ Incorrect
The code attempts to compute key % 0, which causes a runtime ArithmeticException for division by zero.
❓ Predict Output
advancedOutput of hash function with negative key
What is the output of this Java code snippet?
DSA Java
public class Main { public static int hash(int key, int size) { return (key % size + size) % size; } public static void main(String[] args) { System.out.println(hash(-3, 10)); } }
Attempts:
2 left
💡 Hint
Modulo in Java can be negative; this formula ensures a positive result.
✗ Incorrect
The expression (key % size + size) % size converts negative modulo results to positive indices within the table size.
🧠 Conceptual
expertCollision handling in hash tables
Which collision handling technique uses linked lists to store multiple values at the same bucket index?
Attempts:
2 left
💡 Hint
This technique stores collided elements in a list at the bucket.
✗ Incorrect
Separate chaining stores collided elements in linked lists at each bucket, allowing multiple entries per bucket.
