Embercode
✕
SortingQuick quiz

Sort then search

7 questions. Covers Sort then search.

Questions 1 to 5 of 7

  1. 1. What does this program print? ``cpp #include <algorithm> #include <iostream> #include <vector> int main() { std::vector<int> v = {3, 7, 7, 12, 18}; auto it = std::ranges::lower_bound(v, 7); std::cout << (it - v.begin()) << '\n'; auto it2 = std::ranges::lower_bound(v, 10); std::cout << *it2 << '\n'; } ``
  2. 2. What does this program print? ``cpp #include <algorithm> #include <iostream> #include <vector> // Print every pair of values that adds up to target. Works on sorted input. void print_pairs(const std::vector<int>& sorted, int target) { std::size_t lo = 0; std::size_t hi = sorted.size() - 1; while (lo < hi) { int sum = sorted[lo] + sorted[hi]; if (sum == target) { std::cout << sorted[lo] << " + " << sorted[hi] << " = " << target << '\n'; ++lo; --hi; } else if (sum < target) { ++lo; } else { --hi; } } } int main() { std::vector<int> v = {2, 9, 4, 7, 1, 8, 5}; std::ranges::sort(v); print_pairs(v, 10); } ``
  3. 3. Complete the sentence. When no two neighbouring values in a list are equal, std::ranges::adjacent_find returns the ____ of the list.

    Type your answer.

  4. 4. On an empty std::vector, sorted.size() - 1 wraps around to a huge number, because size() returns an ____ type. Fill in the blank.

    Type your answer.

  5. 5. In a sorted list, why does checking only neighbouring pairs find the closest pair of values?

5 question(s) left on this page.

Typed answers ignore extra spaces and, for most questions, capital letters.