STL Algorithms Questions with Answers

18 practice questions on STL Algorithms (C++) with answers and step-by-step solutions. Try 10 samples below, then practise all 18 free in the AptiNest app.

Sample questions

Question 1Easy

What is the output?

#include <iostream>
using namespace std;

#include <vector>
#include <algorithm>

int main() {
    vector<int> v{2, 4, 6};
    int sum = 0;
    for_each(v.begin(), v.end(), [&sum](int x) { sum += x; });
    cout << sum;
    return 0;
}
  1. 0
  2. 6
  3. 12
  4. Compilation error

Answer: C. 12

Explanation

The lambda captures sum by reference, so the additions inside for_each update the local variable.

Step-by-step solution

  1. For_each calls the lambda once for each of 2, 4 and 6.
  2. The lambda captures sum by reference, so each call adds x to the outer sum.
  3. Sum becomes 0 + 2 + 4 + 6 = 12. The output is 12: option C.

Why the other options are wrong

  • A. Sum is changed by the lambda, so it is not left at 0.
  • B. 6 is only the last element; the lambda adds all three values.
  • D. The code compiles, because [&sum] is a valid capture.

Tip: A lambda capture [&x] lets it change an outer variable; [x] captures a copy that cannot change it.

Question 2Easy

What is the output?

#include <iostream>
using namespace std;

#include <vector>
#include <algorithm>

int main() {
    vector<int> v{5, 8, 2, 8};
    auto a = find(v.begin(), v.end(), 8);
    auto b = find(v.begin(), v.end(), 7);
    cout << (a - v.begin()) << " " << (b == v.end());
    return 0;
}
  1. 1 0
  2. 1 1
  3. 3 1
  4. 3 0

Answer: B. 1 1

Explanation

find returns an iterator to the first match (index 1) and returns end() when the value is absent.

Step-by-step solution

  1. Find returns an iterator to the first 8, which is at index 1, so a - v.begin() is 1.
  2. Find for 7 fails and returns v.end(), so b equals v.end().
  3. The comparison is true and prints as 1. The output is 1 1: option B.

Why the other options are wrong

  • A. The second value is 1 for true, not 0, because b equals end.
  • C. The first 8 is at index 1, not index 3; index 3 is the last 8.
  • D. Index 3 is the last 8, not the first, and the second value is 1, not 0.

Tip: Find returns the first match, or end() when the value is missing; compare with end() to test for absence.

Question 3Easy

What is the output?

#include <iostream>
using namespace std;

#include <vector>
#include <algorithm>

int main() {
    vector<int> a{1, 2, 3, 4};
    vector<int> b(4);
    transform(a.begin(), a.end(), b.begin(), [](int x) { return x * x; });
    for (int x : b) cout << x << " ";
    return 0;
}
  1. 1 2 3 4
  2. 1 4 9 16
  3. 2 4 6 8
  4. 0 0 0 0

Answer: B. 1 4 9 16

Explanation

transform applies the lambda to every element of a and writes the results into b.

Step-by-step solution

  1. b has size 4 so writing is safe.
  2. transform squares each element of a.
  3. Results 1 4 9 16 go into b.
  4. Printed with spaces.

Why the other options are wrong

  • A. The lambda squares values.
  • C. That would be doubling, not squaring.
  • D. b is overwritten with the squares.

Tip: Output range must have enough space.

Question 4Easy

What is the output?

#include <iostream>
using namespace std;

#include <string>
#include <algorithm>

int main() {
    string s = "stressed";
    reverse(s.begin(), s.end());
    cout << s;
    return 0;
}
  1. stressed
  2. dessert
  3. desserts
  4. sdessert

Answer: C. desserts

Explanation

reverse reverses the range in place.

Step-by-step solution

  1. Reverse flips the characters of stressed in place.
  2. The letters s t r e s s e d reversed are d e s s e r t s.
  3. The output is desserts: option C.

Why the other options are wrong

  • A. The string is changed by reverse, so it does not print stressed.
  • B. Dessert drops the final s; the reversed word has 8 letters.
  • D. Sdessert moves an s to the front; reverse gives desserts exactly.

Tip: Std::reverse works in place on any range, so the last character becomes the first.

Question 5Medium

What is the output?

#include <iostream>
using namespace std;

#include <string>
#include <algorithm>

int main() {
    string s = "aab";
    int n = 0;
    do {
        cout << s << " ";
        n++;
    } while (next_permutation(s.begin(), s.end()));
    cout << n;
    return 0;
}
  1. aab aba baa 6
  2. aab aba baa aab aba baa 6
  3. aab aba 2
  4. aab aba baa 3

Answer: D. aab aba baa 3

Explanation

next_permutation generates distinct permutations in lexicographic order and returns false after the last one; "aab" has 3!/2! = 3 distinct arrangements.

Step-by-step solution

  1. Next_permutation visits the distinct arrangements in order: aab, then aba, then baa.
  2. Each one is printed with a space, and n goes up by 1 each time.
  3. After baa, next_permutation returns false, so the loop stops with n equal to 3.
  4. The output is aab aba baa 3: option D.

Why the other options are wrong

  • A. The count is 3, not 6, because aab has only 3 distinct arrangements.
  • B. The list is not repeated; the loop stops when next_permutation returns false, so the count is 3.
  • C. It stops too early; baa is also printed before next_permutation returns false.

Tip: Next_permutation on sorted input visits each distinct arrangement once; the count is n! divided by the repeat factorials.

Question 6Medium

What is the output?

#include <iostream>
using namespace std;

#include <vector>
#include <algorithm>

int main() {
    vector<int> v{9, 4, 7, 1, 8};
    nth_element(v.begin(), v.begin() + 2, v.end());
    cout << v[2];
    return 0;
}
  1. 8
  2. 4
  3. 7
  4. 1

Answer: C. 7

Explanation

nth_element places at position 2 the element that would be there if the range were sorted (1, 4, 7, 8, 9), so v[2] is 7. Other positions are only partitioned.

Step-by-step solution

  1. Nth_element puts at index 2 the value that would sit there if the whole range were sorted.
  2. The sorted values are 1 4 7 8 9, so index 2 holds 7.
  3. The output is 7: option C.

Why the other options are wrong

  • A. 8 is at index 3 of the sorted list, not index 2.
  • B. 4 is at index 1 of the sorted list, not index 2.
  • D. 1 is the smallest value, at index 0, not index 2.

Tip: Nth_element guarantees only the value at the chosen position, plus a partition; the rest is not sorted.

Question 7Medium

What is the output?

#include <iostream>
using namespace std;

#include <vector>
#include <algorithm>

int main() {
    vector<int> v{1, 3, 3, 3, 5, 7};
    auto lo = lower_bound(v.begin(), v.end(), 3) - v.begin();
    auto hi = upper_bound(v.begin(), v.end(), 3) - v.begin();
    cout << lo << " " << hi << " " << hi - lo;
    return 0;
}
  1. 2 4 2
  2. 0 4 4
  3. 1 4 3
  4. 1 3 2

Answer: C. 1 4 3

Explanation

lower_bound points to the first 3 (index 1) and upper_bound to the first element greater than 3 (index 4); their difference is the count of 3s.

Step-by-step solution

  1. Lower_bound finds the first 3, at index 1.
  2. Upper_bound finds the first value greater than 3, which is 5 at index 4.
  3. The count of 3s is 4 minus 1, which is 3. The output is 1 4 3: option C.

Why the other options are wrong

  • A. Index 2 is a middle 3, not the first; lower_bound gives index 1.
  • B. Lower_bound is index 1, not 0, because the first 1 is not a 3.
  • D. Upper_bound is index 4, not 3, so the difference would be 2 with the wrong end.

Tip: The count of a value in a sorted range is upper_bound minus lower_bound.

Question 8Medium

What is the output?

#include <iostream>
using namespace std;

#include <vector>
#include <string>
#include <algorithm>

int main() {
    vector<string> w{"pear", "fig", "kiwi", "apple", "yam"};
    stable_sort(w.begin(), w.end(),
                [](const string& a, const string& b) { return a.size() < b.size(); });
    for (const auto& s : w) cout << s[0];
    return 0;
}
  1. fypka
  2. afkpy
  3. yfkpa
  4. fykpa

Answer: A. fypka

Explanation

Words are ordered by length (3, 3, 4, 4, 5). stable_sort keeps equal-length words in their original order: fig before yam, pear before kiwi.

Step-by-step solution

  1. Stable_sort orders by length only. The lengths are pear 4, fig 3, kiwi 4, apple 5 and yam 3.
  2. Words of length 3 keep their original order: fig, then yam. Words of length 4 keep theirs: pear, then kiwi.
  3. Then apple, with length 5, comes last.
  4. The first letters f, y, p, k, a give fypka: option A.

Why the other options are wrong

  • B. Afkpy is sorted alphabetically, not by length.
  • C. Yfkpa puts yam before fig, which breaks the original order of the length 3 words.
  • D. Fykpa puts kiwi before pear, which breaks the original order of the length 4 words.

Tip: Stable_sort keeps equal elements in their original order, so ties are not reshuffled.

Question 9Hard

What is the output?

#include <iostream>
using namespace std;

#include <vector>
#include <algorithm>

int main() {
    vector<int> v{1, 2, 3, 2, 4};
    auto it = remove(v.begin(), v.end(), 2);
    cout << v.size() << " " << (it - v.begin()) << " ";
    v.erase(it, v.end());
    cout << v.size();
    return 0;
}
  1. 5 5 3
  2. 3 5 5
  3. 5 3 3
  4. 3 3 3

Answer: C. 5 3 3

Explanation

std::remove only shifts the kept elements forward and returns the new logical end; the vector size is unchanged until erase removes the tail (the erase-remove idiom).

Step-by-step solution

  1. Remove shifts the kept values 1, 3, 4 to the front and returns an iterator to the new logical end, at index 3.
  2. Remove does not shrink the vector, so v.size() is still 5. The first output is 5, then 3 for the index.
  3. Erase from index 3 to the end removes the two leftover tail values, leaving size 3.
  4. The output is 5 3 3: option C.

Why the other options are wrong

  • A. 5 5 3 uses the wrong logical end; remove returns index 3, not 5.
  • B. 3 5 5 swaps the values; the size is 5 before erase and the logical end is index 3.
  • D. 3 3 3 claims the size changes before erase, but remove never shrinks the vector.

Tip: In the erase-remove idiom, remove shifts values forward and erase actually shortens the vector.

Question 10Hard

What is the output?

#include <iostream>
using namespace std;

#include <vector>
#include <numeric>

int main() {
    vector<double> v{1.5, 2.5, 3.5};
    cout << accumulate(v.begin(), v.end(), 0) << " "
         << accumulate(v.begin(), v.end(), 0.0);
    return 0;
}
  1. 7.5 7.5
  2. 6 7.5
  3. 6 6
  4. 7 7.5

Answer: B. 6 7.5

Explanation

accumulate's result type is the type of the initial value. With int 0 each partial sum is truncated: 1, 3, 6. With 0.0 the sum is 7.5.

Step-by-step solution

  1. Accumulate uses the type of the initial value, which is int here. 0 plus 1.5 becomes 1, then 1 plus 2.5 becomes 3, then 3 plus 3.5 becomes 6.
  2. So the first result is 6, because the running sum is truncated to int each time.
  3. With the initial value 0.0 the sum is a double: 1.5 + 2.5 + 3.5 = 7.5.
  4. The output is 6 7.5: option B.

Why the other options are wrong

  • A. 7.5 for the first sum ignores that the int initial value makes the running sum an int.
  • C. 6 6 wrongly gives the double sum as 6; with 0.0 the sum stays 7.5.
  • D. 7 would need rounding up, but the int sum truncates, so the first result is 6.

Tip: Accumulate's result type comes from the initial value, so use 0.0 when summing doubles.

Practise all 18 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all C++ topics · All topics