Dictionary
EASY LEVEL (1–10)
Create Dictionary
Problem: Create a dictionary with keys as names and values as ages. Return
the dictionary.
Input:
names= ["Amit","Riya"]
ages= [25,30]
Output:
{"Amit":25,"Riya":30}
Access Value
Problem: Return value of given key from dictionary.
Input:
d= {"a":10,"b":20}
key="b"
Output:
20
Check Key Exists
Problem: Return true if key exists in dictionary.
Input:
d= {"x":1,"y":2}
key="x"
Output:
true
Count Keys
Problem: Return total number of keys in dictionary.
Input:
d= {"a":1,"b":2,"c":3}
Output:
3
Sum of Values
Problem: Return sum of all dictionary values.
Input:
d= {"a":10,"b":20,"c":30}
Output:
60
Update Value
Problem: Update value of a given key.
Input:
d= {"a":10}
key="a"
new_value=50
Output:
{"a":50}
Delete Key
Problem: Remove a key from dictionary.
Input:
d= {"a":10,"b":20}
key="a"
Output:
{"b":20}
Get All Keys
Problem: Return list of all keys.
Input:
d= {"a":1,"b":2}
Output:
["a","b"]
Get All Values
Problem: Return list of all values.
Input:
d= {"a":1,"b":2}
Output:
[1,2]
Merge Two Dictionaries
Problem: Merge two dictionaries into one.
Input:
d1= {"a":1}
d2= {"b":2}
Output:
{"a":1,"b":2}
MEDIUM LEVEL (11–20)
Character Frequency
Problem: Count frequency of characters in string.
Input:
s="banana"
Output:
{"b":1,"a":3,"n":2}
Word Frequency
Problem: Count frequency of words in sentence.
Input:
s="I love Python I love coding"
Output:
{"I":2,"love":2,"Python":1,"coding":1}
Invert Dictionary
Problem: Swap keys and values.
Input:
d= {"a":1,"b":2}
Output:
{1:"a",2:"b"}
Find Key with Maximum Value
Input:
d= {"a":10,"b":25,"c":5}
Output:
"b"
Sort Dictionary by Keys
Input:
d= {"b":2,"a":1,"c":3}
Output:
{"a":1,"b":2,"c":3}
Sort Dictionary by Values
Input:
d= {"a":3,"b":1,"c":2}
Output:
{"b":1,"c":2,"a":3}
Group Elements by First Letter
Input:
words= ["apple","ant","bat","ball"]
Output:
{"a":["apple","ant"],"b":["bat","ball"]}
Count Occurrence of Each Element in List
Input:
nums= [1,2,2,3,1]
Output:
{1:2,2:2,3:1}
Remove Keys with Specific Value
Input:
d= {"a":10,"b":20,"c":10}
value=10
Output:
{"b":20}
Nested Dictionary Access
Input:
d= {"user": {"name":"Amit","age":25}}
Output:
25# access age
HARD LEVEL (21–30)
Two Sum Problem
Problem: Return indices of two numbers that add up to target.
Input:
nums= [2,7,11,15]
target=9
Output:
[0,1]
Group Anagrams
Input:
words= ["eat","tea","tan","ate","nat","bat"]
Output:
[["eat","tea","ate"], ["tan","nat"], ["bat"]]
First Non-Repeating Character
Input:
s="leetcode"
Output:
0
Subarray Sum Equals K
Input:
nums= [1,1,1]
k=2
Output:
2
Merge Two Dictionaries and Sum Common Keys
Input:
d1= {"a":1,"b":2}
d2= {"b":3,"c":4}
Output:
{"a":1,"b":5,"c":4}
Check if Two Dictionaries Are Equal
Input:
d1= {"a":1,"b":2}
d2= {"b":2,"a":1}
Output:
true
Find Duplicate Values
Input:
d= {"a":1,"b":2,"c":1}
Output:
[1]
Flatten Nested Dictionary
Input:
d= {"a": {"b":1}}
Output:
{"a.b":1}
Find Top K Frequent Elements
Input:
nums= [1,1,1,2,2,3]
k=2
Output:
[1,2]
LRU Cache Simulation (Basic Version)
Problem: Simulate simple cache using dictionary with fixed capacity.
Input:
capacity=2
operations= ["put 1 1","put 2 2","get 1"]
Output:
1