Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a vector of integers named 'arr'.
DSA C++
std::vector<int> [1]; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'array' or 'list' instead of 'arr'.
✗ Incorrect
The vector is named 'arr' as per the instruction.
2fill in blank
mediumComplete the code to sort the vector 'arr' using a comparison based sorting algorithm.
DSA C++
std::sort(arr.[1](), arr.end()); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'start()' or 'first()' which are not valid vector methods.
✗ Incorrect
The correct method to get the iterator to the beginning of the vector is 'begin()'.
3fill in blank
hardFix the error in the code to perform counting sort on the vector 'arr'.
DSA C++
std::vector<int> count(max_val + 1, 0); for (int num : arr) { count[[1]]++; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'arr' or 'i' which are not the current element in the loop.
✗ Incorrect
We increment the count at index 'num' for each element in 'arr'.
4fill in blank
hardFill both blanks to complete the code that reconstructs the sorted array from the count array.
DSA C++
int index = 0; for (int i = 0; i <= max_val; i++) { while (count[[1]] > 0) { arr[[2]] = i; count[i]--; index++; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'count' or 'num' instead of 'i' for the first blank.
✗ Incorrect
We use 'i' to index the count array and 'index' to assign values in 'arr'.
5fill in blank
hardFill all three blanks to complete the code that creates a map of element frequencies using a non-comparison based approach.
DSA C++
std::unordered_map<int, int> freq; for (int [1] : arr) { freq[[2]] = freq[[3]] + 1; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names for loop and map keys.
✗ Incorrect
The loop variable is 'num', and we use it to update the frequency map keys.