Set and Dictionary Interview Questions – Problem Statements and
Explanations
Set-Based Questions
1. Add Element to a Set
Problem: Add an item to a given set.
Explanation: Use add() method.
Input: {1, 2, 3}, add 4
Output: {1, 2, 3, 4}
2. Remove Element from Set
Problem: Remove a specific element from a set.
Explanation: Use remove() or discard() to avoid KeyError.
Input: {1, 2, 3}, remove 2
Output: {1, 3}
3. Union of Two Sets
Problem: Find union of two sets.
Explanation: Use | operator or union() method.
Input: {1, 2}, {2, 3}
Output: {1, 2, 3}
4. Intersection of Sets
Problem: Find common elements in two sets.
Explanation: Use & operator or intersection().
Input: {1, 2}, {2, 3}
Output: {2}
5. Difference of Sets
Problem: Elements present in first set but not in second.
Explanation: Use - or difference() method.
Input: {1, 2, 3}, {2, 3}
Output: {1}
6. Check Subset
Problem: Check if one set is a subset of another.
Explanation: Use issubset() method.
Input: {1, 2}, {1, 2, 3}
Output: True
7. Set Length
Problem: Find number of elements in set.
Explanation: Use len().
Input: {1, 2, 3}
Output: 3
8. Clear a Set
Problem: Remove all elements from a set.
Explanation: Use clear().
Input: {1, 2, 3}
Output: set()
9. Symmetric Difference
Problem: Find elements in either set but not in both.
Explanation: Use ^ or symmetric_difference()
Input: {1, 2, 3}, {2, 3, 4}
Output: {1, 4}
10. Convert List to Set
Problem: Remove duplicates using set.
Explanation: Use set() constructor.
Input: [1, 2, 2, 3]
Output: {1, 2, 3}
Dictionary-Based Questions
11. Create a Dictionary from Two Lists
Problem: Combine two lists into a dictionary.
Explanation: Use zip() function.
Input: ["a", "b"], [1, 2]
Output: {"a": 1, "b": 2}
12. Update Dictionary Value
Problem: Change value for a specific key.
Explanation: Use assignment dict[key] = value.
Input: {"a": 1}, update a to 2
Output: {"a": 2}
13. Remove Key from Dictionary
Problem: Delete a key-value pair.
Explanation: Use del or pop() method.
Input: {"a": 1, "b": 2}, remove b
Output: {"a": 1}
14. Check Key Existence
Problem: Verify if a key exists.
Explanation: Use in operator.
Input: {"x": 1}, key = "x"
Output: True
15. Iterate Over Dictionary
Problem: Print all keys and values.
Explanation: Use .items() in loop.
Input: {"a": 10, "b": 20}
Output: a 10, b 20
16. Dictionary Length
Problem: Count total key-value pairs.
Explanation: Use len() function.
Input: {"x": 1, "y": 2}
Output: 2
17. Merge Two Dictionaries
Problem: Combine two dictionaries.
Explanation: Use unpacking or update().
Input: {"a": 1}, {"b": 2}
Output: {"a": 1, "b": 2}
18. Get Value with Default
Problem: Get value or default if key not found.
Explanation: Use get() method.
Input: {"a": 1}, get "b" with default 0
Output: 0
19. Count Frequency of Elements
Problem: Count frequency using dictionary.
Input: [1, 2, 2, 3]
Output: {1: 1, 2: 2, 3: 1}
20. Invert a Dictionary
Problem: Flip keys and values.
Input: {"a": 1, "b": 2}
Output: {1: "a", 2: "b"}
21. Find Key with Maximum Value
Problem: Identify the key with the highest value.
Input: {"a": 10, "b": 20, "c": 15}
Output: "b"
22. Sort Dictionary by Values
Problem: Sort a dictionary based on its values.
Input: {"a": 3, "b": 1, "c": 2}
Output: [('b', 1), ('c', 2), ('a', 3)]
23. Create Dictionary of Squares
Problem: Create dictionary where keys are numbers and values are their
squares.
Input: range(1, 4)
Output: {1: 1, 2: 4, 3: 9}
24. Filter Dictionary by Value Condition
Problem: Retain only items with value greater than a threshold.
Input: {"a": 10, "b": 5, "c": 15}, condition: > 10
Output: {"c": 15}
25. Combine Values of Duplicate Keys
Problem: Given two dictionaries, add values of common keys.
Input: {"a": 1, "b": 2}, {"a": 3, "c": 4}
Output: {"a": 4, "b": 2, "c": 4}
26. Count Word Frequency in Sentence
Problem: Count occurrences of each word in a string.
Input: "apple banana apple"
Output: {"apple": 2, "banana": 1}
27. Remove Duplicate Values from Dictionary
Problem: Remove duplicate values keeping first key only.
Input: {"a": 1, "b": 2, "c": 1}
Output: {"a": 1, "b": 2}
28. Find Common Keys in Two Dictionaries
Problem: Return keys common to both dictionaries.
Input: {"a": 1, "b": 2}, {"b": 3, "c": 4}
Output: ["b"]
29. Swap Keys and Values Safely
Problem: Flip keys and values ensuring all values are unique.
Input: {"x": 1, "y": 2}
Output: {1: "x", 2: "y"}
30. Delete Items by Value
Problem: Remove key-value pairs with specific value.
Input: {"a": 1, "b": 2, "c": 1}, value: 1
Output: {"b": 2}
Would you like to: - 🐍 Add Python code solutions? - 📥 Export this as PDF? - ➕
Add student or employee real-time record questions?