C++ STL Mutating Algorithms

Last Updated :
Discuss
Comments

Question 1

Which of the following steps has the highest time complexity in the program for sorting elements based on frequency?

  • Sorting the frequency vector of pairs.

  • Reconstructing the array from the frequency vector.

  • Inserting elements into the result array.

  • Creating the frequency map using unordered_map.

Question 2

What will be the output of the following program?

C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
    vector<int> v = {1, 2, 3, 4, 5, 6};
    reverse(v.begin(), v.begin() + 2);
    reverse(v.begin() + 2, v.end());
    reverse(v.begin(), v.end());
    for (int i = 0; i < v.size(); i++)
        cout << v[i] << " ";
    return 0;
}


  • 6 5 4 3 2 1

  • 5 6 1 2 3 4

  • 3 4 5 6 1 2

  • 1 2 3 4 5 6

Question 3

What is the time complexity of make_heap function in C++?

  • O(1)

  • O(n log n)

  • O(n^2)

  • O(n)

Question 4

Which of the following is true about the merge() function in C++?

  • The input sequences must be sorted in ascending order for the merge to work correctly.

  • It can merge two unsorted sequences without sorting them first.

  • The merge function returns the merged sequence directly, not an iterator.

  • The merge() function always merges two sequences into the first sequence.

Question 5

What will be the output of the following C++ code?

C++
#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string str = "hello";
    reverse(str.begin() + 1, str.end() - 2);
    cout << str << endl;
    return 0;
}


  • hello

  • hlelo

  • olleh

  • The code will produce a compile-time error.

Question 6

Which of the following describes the internal working of prev_permutation() in C++?

  • It finds the first increasing element, swaps it with the largest smaller element.

  • It finds the first non-increasing element, swaps it with the largest smaller element, and reverses the right subsequence.

  • It sorts in descending order and returns the next smaller permutation.

  • It finds the first decreasing element, swaps it with the smallest greater element, and returns the next permutation.

Question 7

What will be the output of the given program?

C++
#include <bits/stdc++.h>
using namespace std;
struct Point
{
    int x, y;
    bool operator<(Point &oth)
    {
        return this->y > oth.y;
    }
};
int main()
{
    Point arr[] = {{3, 10}, {2, 8}, {5, 4}};
    int n = sizeof(arr) / sizeof(arr[0]);
    sort(arr, arr + n);
    for (auto i : arr)
    {
        cout << i.x << " " << i.y << endl;
    }
    return 0;
}


  • 5 4
    2 8
    3 10

  • 3 10
    2 8
    5 4

  • 2 8
    3 10
    5 4

  • 3 10
    5 4
    2 8

Question 8

Complete the myCmp() function in the program to sort the array arr in descending order based in the y coordinate.

C++
#include <bits/stdc++.h>
using namespace std;
struct Point
{
    int x, y;
};
bool myCmp(Point p1, Point p2)
{
    // Missing code here
}
int main()
{
    Point arr[] = {{3, 10}, {2, 8}, {5, 4}};
    int n = sizeof(arr) / sizeof(arr[0]);
    sort(arr, arr + n, myCmp);
    return 0;
}


  • return (p1.y > p2.y);

  • return (p1.x < p2.x);

  • return (p1.y < p2.y);

  • return (p1.x > p2.x);

Question 9

What will be the output of the following C++ code?

C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
    vector<int> v = {1, 2, 3};
    next_permutation(v.begin(), v.end());
    for (int i : v)
        cout << i << " ";
    return 0;
}


  • 1 2 3

  • 2 1 3

  • 3 2 1

  • 1 3 2

Question 10

Which of the following data structures can be sorted using the sort() function in C++?

  • list<int>

  • set<int>

  • vector<int>

  • unordered_map<int, int>

Tags:

There are 10 questions to complete.

Take a part in the ongoing discussion