
Practice 50 Hashmaps Coding Questions
Q1. Write a Rust program to create a hashmap and insert key-value pairs.
Input:
Keys: "a", "b", "c"
Values: 1, 2, 3
Expected Output:
HashMap: {"a": 1, "b": 2, "c": 3}
Q2. Write a Rust program to access a value in a hashmap using a key.
Input:
Key: "b"
Expected Output:
Value for "b": 2
Q3. Write a Rust program to check if a hashmap contains a specific key.
Input:
Key: "d"
Expected Output:
Key "d" not found in the hashmap.
Q4. Write a Rust program to iterate over the keys and values of a hashmap.
Input:
HashMap: {"a": 1, "b": 2, "c": 3}
Expected Output:
a: 1
b: 2
c: 3
Q5. Write a Rust program to remove a key-value pair from a hashmap.
Input:
Key: "b"
Expected Output:
HashMap after removing "b": {"a": 1, "c": 3}
Q6. Write a Rust program to get all keys of a hashmap.
Input:
HashMap: {"a": 1, "b": 2, "c": 3}
Expected Output:
Keys: ["a", "b", "c"]
Q7. Write a Rust program to get all values of a hashmap.
Input:
HashMap: {"a": 1, "b": 2, "c": 3}
Expected Output:
Values: [1, 2, 3]
Q8. Write a Rust program to merge two hashmaps.
Input:
HashMap 1: {"a": 1, "b": 2}
HashMap 2: {"c": 3, "d": 4}
Expected Output:
Merged HashMap: {"a": 1, "b": 2, "c": 3, "d": 4}
Q9. Write a Rust program to find the number of key-value pairs in a hashmap.
Input:
HashMap: {"a": 1, "b": 2, "c": 3}
Expected Output:
Number of key-value pairs: 3
Q10. Write a Rust program to clear all elements from a hashmap.
Input:
HashMap: {"a": 1, "b": 2}
Expected Output:
HashMap after clearing: {}
Q11. Write a Rust program to check if a hashmap is empty.
Input:
HashMap: {}
Expected Output:
The hashmap is empty.
Q12. Write a Rust program to create a hashmap with a default value.
Input:
Keys: "a", "b", "c"
Default value: 0
Expected Output:
HashMap with default values: {"a": 0, "b": 0, "c": 0}
Q13. Write a Rust program to update the value of a key in a hashmap.
Input:
HashMap: {"a": 1, "b": 2}, Key: "b", New value: 5
Expected Output:
HashMap after update: {"a": 1, "b": 5}
Q14. Write a Rust program to count the occurrence of each character in a string using a hashmap.
Input:
String: "hello"
Expected Output:
Character count: {'h': 1, 'e': 1, 'l': 2, 'o': 1}
Q15. Write a Rust program to find the maximum value in a hashmap.
Input:
HashMap: {"a": 5, "b": 2, "c": 3}
Expected Output:
Maximum value: 5
Q16. Write a Rust program to find the key associated with the maximum value in a hashmap.
Input:
HashMap: {"a": 5, "b": 2, "c": 3}
Expected Output:
Key with maximum value: "a"
Q17. Write a Rust program to find the minimum value in a hashmap.
Input:
HashMap: {"a": 5, "b": 2, "c": 3}
Expected Output:
Minimum value: 2
Q18. Write a Rust program to find the key associated with the minimum value in a hashmap.
Input:
HashMap: {"a": 5, "b": 2, "c": 3}
Expected Output:
Key with minimum value: "b"
Q19. Write a Rust program to check if a hashmap contains a specific value.
Input:
HashMap: {"a": 5, "b": 2, "c": 3}, Value: 2
Expected Output:
Value 2 found in the hashmap.
Q20. Write a Rust program to iterate over the keys of a hashmap and print them.
Input:
HashMap: {"a": 1, "b": 2, "c": 3}
Expected Output:
Keys: a b c
Q21. Write a Rust program to iterate over the values of a hashmap and print them.
Input:
HashMap: {"a": 1, "b": 2, "c": 3}
Expected Output:
Values: 1 2 3
Q22. Write a Rust program to create a hashmap from two vectors (one for keys, one for values).
Input:
Keys: ["a", "b", "c"], Values: [1, 2, 3]
Expected Output:
HashMap: {"a": 1, "b": 2, "c": 3}
Q23. Write a Rust program to create a hashmap from a list of tuples.
Input:
Tuples: [("a", 1), ("b", 2), ("c", 3)]
Expected Output:
HashMap: {"a": 1, "b": 2, "c": 3}
Q24. Write a Rust program to check if a hashmap contains a key and return the associated value.
Input:
HashMap: {"a": 1, "b": 2}, Key: "a"
Expected Output:
Value for "a": 1
Q25. Write a Rust program to find the average value of the values in a hashmap.
Input:
HashMap: {"a": 5, "b": 2, "c": 3}
Expected Output:
Average value: 3.3333
Q26. Write a Rust program to convert a hashmap into a vector of tuples.
Input:
HashMap: {"a": 1, "b": 2}
Expected Output:
Vector of tuples: [("a", 1), ("b", 2)]
Q27. Write a Rust program to check if a hashmap has a specific key and insert a default value if not.
Input:
HashMap: {"a": 1, "b": 2}, Key: "c", Default value: 3
Expected Output:
HashMap after inserting default value: {"a": 1, "b": 2, "c": 3}
Q28. Write a Rust program to remove all keys with a specific value from a hashmap.
Input:
HashMap: {"a": 1, "b": 2, "c": 1}, Value to remove: 1
Expected Output:
HashMap after removal: {"b": 2}
Q29. Write a Rust program to create a hashmap with integer keys and string values.
Input:
Keys: 1, 2, 3
Values: "a", "b", "c"
Expected Output:
HashMap: {1: "a", 2: "b", 3: "c"}
Q30. Write a Rust program to combine the values of two hashmaps into a vector.
Input:
HashMap 1: {"a": 1, "b": 2}, HashMap 2: {"c": 3, "d": 4}
Expected Output:
Combined vector: [1, 2, 3, 4]
Q31. Write a Rust program to find all keys that have a value greater than a specific number in a hashmap.
Input:
HashMap: {"a": 5, "b": 2, "c": 3}, Threshold: 3
Expected Output:
Keys with values greater than 3: ["a"]
Q32. Write a Rust program to create a hashmap from a list of words with their frequencies.
Input:
Words: ["apple", "banana", "apple", "orange"]
Expected Output:
Frequency map: {"apple": 2, "banana": 1, "orange": 1}
Q33. Write a Rust program to replace a value for a given key in a hashmap.
Input:
HashMap: {"a": 1, "b": 2}, Key: "b", New value: 5
Expected Output:
HashMap after replacement: {"a": 1, "b": 5}
Q34. Write a Rust program to create a hashmap with keys as characters and values as their ASCII codes.
Input:
Characters: 'a', 'b', 'c'
Expected Output:
HashMap: {'a': 97, 'b': 98, 'c': 99}
Q35. Write a Rust program to concatenate the values of two hashmaps with string values.
Input:
HashMap 1: {"a": "hello", "b": "world"}, HashMap 2: {"c": "rust"}
Expected Output:
Concatenated HashMap: {"a": "hello", "b": "world", "c": "rust"}
Q36. Write a Rust program to find the most frequent value in a hashmap.
Input:
HashMap: {"a": 1, "b": 2, "c": 1}
Expected Output:
Most frequent value: 1
Q37. Write a Rust program to find the total number of unique values in a hashmap
Input:
HashMap: {"a": 1, "b": 2, "c": 1, "d": 3}
Expected Output:
Total number of unique values: 3
Q38. Write a Rust program to create a hashmap from a vector of tuples, where each tuple contains a key-value pair.
Input:
Tuples: [("apple", 2), ("banana", 3), ("orange", 1)]
Expected Output:
HashMap: {"apple": 2, "banana": 3, "orange": 1}
Q39. Write a Rust program to find the sum of all values in a hashmap.
Input:
HashMap: {"a": 5, "b": 3, "c": 2}
Expected Output:
Sum of values: 10
Q40. Write a Rust program to create a hashmap from a string of space-separated words, and count the occurrences of each word.
Input:
String: "rust is fun rust is great"
Expected Output:
Word count: {"rust": 2, "is": 2, "fun": 1, "great": 1}
Q41. Write a Rust program to find the number of elements in a hashmap where the values are greater than a specified threshold.
Input:
HashMap: {"a": 5, "b": 2, "c": 7}, Threshold: 4
Expected Output:
Number of elements greater than 4: 2
Q42. Write a Rust program to filter keys in a hashmap where their corresponding values are even.
Input:
HashMap: {"a": 4, "b": 5, "c": 6}
Expected Output:
Filtered HashMap: {"a": 4, "c": 6}
Q43. Write a Rust program to find the average length of all values (strings) in a hashmap.
Input:
HashMap: {"a": "hello", "b": "rust", "c": "world"}
Expected Output:
Average length of values: 4.33
Q44. Write a Rust program to merge two hashmaps with integer values by summing up the values of common keys.
Input:
HashMap 1: {"a": 5, "b": 2}, HashMap 2: {"b": 3, "c": 1}
Expected Output:
Merged HashMap: {"a": 5, "b": 5, "c": 1}
Q45. Write a Rust program to create a hashmap where the values are vectors of integers and add values to the vectors.
Input:
HashMap: {"a": [1, 2], "b": [3, 4]}, Key: "a", Value to add: 3
Expected Output:
HashMap after adding value: {"a": [1, 2, 3], "b": [3, 4]}
Q46. Write a Rust program to create a hashmap where the values are lists of strings and add a string to the list.
Input:
HashMap: {"a": ["one", "two"], "b": ["three"]}, Key: "b", String to add: "four"
Expected Output:
HashMap after adding string: {"a": ["one", "two"], "b": ["three", "four"]}
Q47. Write a Rust program to convert a hashmap of strings into a hashmap of string lengths.
Input:
HashMap: {"rust": "language", "python": "programming"}
Expected Output:
HashMap of lengths: {"rust": 8, "python": 11}
Q48. Write a Rust program to merge two hashmaps where the values are lists of integers.
Input:
HashMap 1: {"a": [1, 2], "b": [3, 4]}, HashMap 2: {"a": [5, 6], "b": [7, 8]}
Expected Output:
Merged HashMap: {"a": [1, 2, 5, 6], "b": [3, 4, 7, 8]}
Q49. Write a Rust program to check if a hashmap is a subset of another hashmap.
Input:
HashMap 1: {"a": 1, "b": 2}, HashMap 2: {"a": 1, "b": 2, "c": 3}
Expected Output:
HashMap 1 is a subset of HashMap 2.
Q50. Write a Rust program to create a hashmap from a vector of keys and a vector of values.
Input:
Keys: ["a", "b", "c"], Values: [1, 2, 3]
Expected Output:
HashMap: {"a": 1, "b": 2, "c": 3}