Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to sort the array using std::sort.
DSA C++
#include <algorithm> #include <vector> int main() { std::vector<int> data = {5, 3, 8, 1, 2}; std::sort(data.[1]); return 0; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using begin() and end() without specifying the container causes errors.
Using data.begin() but forgetting data.end() or vice versa.
✗ Incorrect
std::sort requires iterators to the beginning and end of the container. data.begin() and data.end() provide these iterators for the vector.
2fill in blank
mediumComplete the code to print the sorted vector elements.
DSA C++
#include <iostream> #include <vector> int main() { std::vector<int> data = {1, 2, 3, 5, 8}; for (int [1] : data) { std::cout << [1] << " "; } return 0; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in the loop header and inside the loop body.
Using reserved keywords or invalid identifiers.
✗ Incorrect
The variable name in the range-based for loop can be any valid identifier. Here, 'num' is used consistently to represent each element.
3fill in blank
hardFix the error in the binary search code by completing the condition.
DSA C++
#include <vector> #include <algorithm> bool contains(const std::vector<int>& data, int target) { return std::binary_search(data.begin(), data.end(), [1]); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the whole container instead of the target value.
Passing iterators instead of the value.
✗ Incorrect
std::binary_search needs the target value to search for, not the container or iterators.
4fill in blank
hardFill both blanks to create a map of word lengths for words longer than 3 characters.
DSA C++
std::map<std::string, int> wordLengths; for (const auto& word : words) { if (word.[1] > 3) { wordLengths[word] = word.[2]; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using length or size without parentheses.
Using incorrect member function names.
✗ Incorrect
In C++, std::string has member functions length() and size() that return the length of the string. Both are valid, but here length() is used in the condition and size() to assign the length.
5fill in blank
hardFill all three blanks to create a filtered map of uppercase words and their lengths for words longer than 4.
DSA C++
#include <map> #include <string> #include <algorithm> std::map<std::string, int> filteredWords; for (const auto& word : words) { if (word.[1] > 4) { std::string upperWord = word; std::transform(upperWord.[2], upperWord.[3], upperWord.begin(), ::toupper); filteredWords[upperWord] = word.size(); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using length() instead of size() inconsistently.
Forgetting parentheses on member functions.
Using incorrect iterators for std::transform.
✗ Incorrect
To filter words longer than 4, use size(). std::transform needs iterators to the start and end of the string, so begin() and end() are used.