0
0
DSA C++programming~10 mins

Why Sorting Matters and How It Unlocks Other Algorithms in DSA C++ - Test Your Knowledge

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Adata.begin(), end()
Bbegin(), data.end()
Cbegin(), end()
Ddata.begin(), data.end()
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.
2fill in blank
medium

Complete 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'
Anum
Bi
Cval
Dx
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.
3fill in blank
hard

Fix 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'
Atarget.begin()
Bdata.begin()
Ctarget
Ddata
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the whole container instead of the target value.
Passing iterators instead of the value.
4fill in blank
hard

Fill 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'
Alength
Bsize
Clength()
Dsize()
Attempts:
3 left
💡 Hint
Common Mistakes
Using length or size without parentheses.
Using incorrect member function names.
5fill in blank
hard

Fill 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'
Asize()
Bbegin()
Cend()
Dlength()
Attempts:
3 left
💡 Hint
Common Mistakes
Using length() instead of size() inconsistently.
Forgetting parentheses on member functions.
Using incorrect iterators for std::transform.