Bouns Questions

1. Write code to iterate from the first element to the last element when the vector is given

void iter(vector<int> v)
{
    vector<int>::iterator it1,it2;
    
    for(it1=v.begin(),it2=v.end(),it2;it1!=it2;it1++)
    {
        cout<<*it1<<" ";
    }   
    
    cout<<endl;
}

 2. Write code to sort and reverse the vector.

vector<int> sortVector(vector<int> v)
{
    sort(v.begin(), v.end());
    return v;
}

vector<int> reverseVector(vector<int> v)
{
    for(int i = 0, j = v.size() - 1; i < j; i++, j--)
        swap(v[i],v[j]);

    return v;
}

4. Declare, Insert and Print a pair of strings paired with another pair of int paired with int.

//Declaration of Pair
pair<string,pair<int,int>> x;

//Inserting element
x= { "Geeks" , { 1, 2} };

//Printing element
cout<<x.first<<" "<<x.second.first<<" "<<x.second.second;

5. Find the index of a given element in a vector, such that the next element to it is 2, and if there exists more than one such index returns the larger one if there is no such index then return -1.

int find_index(vector<int> &V,int a)
{
    stack<int> temp;
    
    for(int i=0;i<V.size();i++)
    {
        if(V[i]==a){
            temp.push(i);
        }
        
        if(i==temp.top()+1){
            if(V[i]==2)
                continue;
            else if(V[i]==a){
                temp.pop();
                temp.push(i);
            }
            else{
                temp.pop();
            }
        }
    }
    
    if(temp.top()==V.size()-1)
        temp.pop();

    if(temp.size()==0)
        return -1;
        
    return temp.top();
}

6. Write a Class stack to implement a stack using a queue.

class Stack {
    queue<int> q1, q2;
 
public:
    void push(int x)
    {
        q2.push(x);
        
        while (!q1.empty()) {
            q2.push(q1.front());
            q1.pop();
        }
 
        queue<int> q = q1;
        q1 = q2;
        q2 = q;
    }
 
    void pop()
    {
        if (q1.empty())
            return;
        q1.pop();
    }
 
    int top()
    {
        if (q1.empty())
            return -1;
        return q1.front();
    }
 
    int size()
    {
        return q1.size();
    }
};

7. Write a code to perform push from the back, pop from the front, get the size, get back and get front operations.

void push(queue<int> &q,int x)
{
    q.push_back(x);
}

int pop(queue<int> &q)
{
    int x=getFront(q);
    q.pop_front();
    
    return x;
}


int getSize(queue<int> &q)
{
    return q.size();
}

int getBack(queue<int> &q)
{
    return q.back();
}

int getFront(queue<int> &q)
{
    return q.front();
}

8. Write a code to check duplicate values in the vector and print them with the best possible time complexity.

void dupicate(vector<int> &V)
{
    unordered_set<int> store;
    
    for(auto it:V)
    {
        if(store.find(it)!=store.end())
        {
            cout<<it<<" ";
        }
        else
        {
            store.insert(it);
        }
    }
}


Top C++ STL Interview Questions and Answers

The Standard Template Library (STL) is a set of C++ template classes that are used to implement widely popular algorithms and data structures such as vectors, lists, stacks, and queues. It is part of the C++ Language ISO standard. STL is a popular topic among interviewers, so it is useful for both freshers and experienced to learn the commonly asked interview question on STL in C++.

In this article, we will see the top 50 most important and most frequently asked interview questions on C++ STL.

Similar Reads

STL Interview Questions and Answers

1. What is STL?...

Bouns Questions

1. Write code to iterate from the first element to the last element when the vector is given...

Contact Us