Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare the hash map for storing numbers and their indices.
DSA C++
std::unordered_map<int, int> [1];Attempts:
3 left
💡 Hint
Common Mistakes
Using 'nums' which is usually the input vector name.
Using 'result' which is typically the output vector.
✗ Incorrect
We use 'num_map' as the hash map name to store number to index mappings.
2fill in blank
mediumComplete the code to iterate over the input vector 'nums' using index 'i'.
DSA C++
for (int [1] = 0; [1] < nums.size(); ++[1]) {
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'j' or 'k' which are less common for this loop.
Using 'index' which is verbose and uncommon here.
✗ Incorrect
The loop variable 'i' is used to index elements in 'nums'.
3fill in blank
hardFix the error in checking if the complement exists in the hash map.
DSA C++
if (num_map.find([1]) != num_map.end()) {
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for 'nums[i]' itself instead of the complement.
Using addition instead of subtraction.
✗ Incorrect
We check if the complement (target - current number) exists in the map.
4fill in blank
hardComplete the code to insert the current number and its index into the hash map.
DSA C++
num_map.[1](nums[i], i);Attempts:
3 left
💡 Hint
Common Mistakes
Using 'push_back' or 'append' which are not valid for unordered_map.
Using 'insert' which requires explicit pair construction (e.g., insert({nums[i], i})).
✗ Incorrect
'emplace' efficiently inserts the key-value pair; the value is the index 'i'.
5fill in blank
hardFill the blanks to return the indices of the two numbers that add up to target.
DSA C++
return std::vector<int>{num_map[[1]], [2];
Attempts:
3 left
💡 Hint
Common Mistakes
Returning the current number instead of its index.
Using addition instead of subtraction for complement.
✗ Incorrect
We return the index of the complement and the current index 'i'.
