- Predict the time complexity of the following recursive function, given the vector is of size N and the initial call is calSum(v, 0).
int sum = 0;
void calcSum(vector v, int i)
{
if(i == v.size())
return;
sum += v[i];
calcSum(v, i+1);
}
Choose the correct option :
O(N)
O(N^2)
Depends upon the compiler, but O(N) in most cases.
Depends upon the compiler, but O(N^2) in most cases.
I think it should be O(n) but correct ans is O(n^2).
- Given an array of N integers.
We wish to sort these integers but we must remember what their orignal indices were before sorting.
Choose the correct option :
We cannot achieve this with the inbuilt sort function.
We may build a vector> data where data[i].first = AR[i] and data[i].second = i and sort this vector using sort(data.begin(), data.end()).
We may build a vector> data where data[i].first = i and data[i].second = AR[i] and sort this vector using sort(data.begin(), data.end()).
This cannot be achieved without a custom comparator and two arrays.
Correct option is 2 but option 3 is also same so it can also be correct?
- class myStack
{
vector < int > stack;
public:
int top();
void pop();
void push(int x);
}
Imagine the vector as a stack. The element at last position of the vector will be treated as top of the stack and element at stack[0] will be treated as the bottom of the stack.
Choose the correct option :
top() and pop() can be implemented to work in O(1) time complexity.
top() and push() can be implemented to work in O(1) time complexity.
either pop() or push() will work in O(N) and the other will work in O(1).
All functions can be implemented to work in O(1) time complexity.