## C++ Interview Questions

- 0of 0 votes
Do STL containers always create copy of objects when containers are populated (e.g. if you have a vector<A> or a map<int, A>, when we insert elements into vector/map, whether copies of object of class A would be stored inside the vector/map?

- 0of 0 votes
How can we achieve something similar to polymorphism in C language? Polymorphism is an OOPs feature.

- 0of 0 votes
Can we write a collection class in C++ that can store elements of different data types?

- 0of 0 votes
Write the following functions to serialize a vector of strings to a string and read it back(possibly on another machine):

string encode(vector<string> v);

vector<string> decode(vector<string> v);

- 0of 0 votes
Given a number in an array form, Come up with an algorithm to push all the zeros to the end.

Expectation : O(n) solution

- 0of 0 votes
Give 2 arrays of size 7 and 3 which are sorted such that the last 3 blocks in first array are empty, merge the arrays in a sorted manner in the most efficient way.

E.g:-

a[7] = [4, 10, 11, 20__, __, __]

b[3] = [1,3,7]

- 0of 0 votes
How do you implement stack in stl? What is the complexity?

- 0of 0 votes
What is smart pointer? How do you implement? What happens with the following: p2 = p1;

What happens P3(p1) (copy const)?

- 0of 0 votes
I have declared one double pointer character array as given below:

char **errorcode;

so how to initialize this array with NULL.

and pass to other function?????

- 0of 0 votes
Struct node{

node *pNext;

node *pRandom;

};

You have a link list of the above node structure. pRandom has connection to any random nodes.

Write a program to clone this list.

note:You should not add any new items to node

- -1of 1 vote
Write a program to find the GCD of two numbers

- 0of 0 votes
`A chessboard was given to us. Where in there was a Knight and King was placed oncertain positions. Our aim is to reach the king from the knight in minimum no of counts.As we know, knight can either move 2 steps vertical/horizontal and 1 stephorizontal/vertical. same goes here as well. Proper image of the chess board was given inthe question paper, and all the positions(max 8) were given that knight can take in thefirst step. Sol : Most of us implemented using recursive func`

- 0of 0 votes
`Seat Reservation prog for the theatre. Write a function for seat allocation for the movietickets. Total no of seats available are 200. 20 in each row. Each row is referred by theCharacter, "A" for the first row and ,J, for the last. And each seat in a row is represented by the no. 1-20. So seat in diffrent rows would be represented asA1,A2....;B1,B2.....;........J1,J2... Each cell in the table represent either 0 or 1. 0 rep wouldseat is available , 1 would represent seat is reserved.Booking should start from the last row (J) to the first row(A). At the max 20 seats can be booked at a time. if seats are available, then print all the seat nos like "B2" i.e (2 row, 3col) otherwise Print "Seats are not available." and we must book consecutive seats only`

- 0of 0 votes
What is generics? How do you call a generic method in C++/C#? What are the disadvantages of generics?

- 0of 0 votes
How will you implement run-time polymorphism in C? There are two structs. There is a common function receiving only one argument(only one). The function should accept both base struct and derived struct objects and do corresponding actions. i.e if base struct object is passed, do base struct's task and vice versa

- 0of 0 votes
The way a Knight Given a chessboard, consisting of nÃ—n cells, several of them are cut. Find the path of minimum length for a Knight from one cell to another. The Knight canâ€™t go through cut cells.

Specifications

Input

The first row is set to the number n (2 â‰¤ n â‰¤ 50). Each of the next n lines contains n symbols. The symbol # denotes the cut cell, the point - not cut cell, the symbol @ denotes the initial and final cell of the Knight's path (the chessboard contains two such characters).

Output If the path can not be constructed, print "Impossible". Otherwise display the same map as the input, but check all Knight intermediate positions with symbol @. Example

Example input

5

.....

.@@..

.....

.....

.....

5

@..@.

..##.

.....

.....

.....

5

@....

..#..

.#...

.....

....@

Example output

Sample 1

...@.

.@@..

....@

.....

.....

Sample 2

@..@.

..##.

.@..@

..@..

@....

Sample 3

- -2of 4 votes
Look at the following pseudo-code, which computes the n-th Fibonacci number:

int fibonacci(int n)

{

if (n == 0)

{

print(0)

return 0

}

if (n == 1)

{

print(1)

return 1

}

return fibonacci(n - 1) + fibonacci(n - 2)

}

If one calls fibonacci(3), then the following will happen:

* fibonacci(3) calls fibonacci(2) and fibonacci(1) (the first call).

* fibonacci(2) calls fibonacci(1) (the second call) and fibonacci(0).

* The second call of fibonacci(1) prints 1 and returns 1.

* fibonacci(0) prints 0 and returns 0.

* fibonacci(2) gets the results of fibonacci(1) and fibonacci(0) and returns 1.

* The first call of fibonacci(1) prints 1 and returns 1.

* fibonacci(3) gets the results of fibonacci(2) and fibonacci(1) and returns 2.

In total, 1 will be printed twice and 0 will be printed once.

We want to know how many times 0 and 1 will be printed for a given integer N.

INPUT

The first line contains an integer T, denoting the number of test cases.

The next T lines contain an integer N.

OUTPUT

For each test case, print one line of output which contains 2 integers separated by a space. The first integer is the number of times 0 is printed. The second integer is the number of times 1 is printed.

CONSTRAINTS

1 <= T <= 50

0 <= N <= 40

SAMPLE INPUT

2

0

3

SMAPLEOUTPUT

1 0

1 2

- 0of 0 votes
Look at the following pseudo-code, which computes the n-th Fibonacci number:

int fibonacci(int n)

{

if (n == 0)

{

print(0)

return 0

}

if (n == 1)

{

print(1)

return 1

}

return fibonacci(n - 1) + fibonacci(n - 2)

}

If one calls fibonacci(3), then the following will happen:

* fibonacci(3) calls fibonacci(2) and fibonacci(1) (the first call).

* fibonacci(2) calls fibonacci(1) (the second call) and fibonacci(0).

* The second call of fibonacci(1) prints 1 and returns 1.

* fibonacci(0) prints 0 and returns 0.

* fibonacci(2) gets the results of fibonacci(1) and fibonacci(0) and returns 1.

* The first call of fibonacci(1) prints 1 and returns 1.

* fibonacci(3) gets the results of fibonacci(2) and fibonacci(1) and returns 2.

In total, 1 will be printed twice and 0 will be printed once.

We want to know how many times 0 and 1 will be printed for a given integer N.

INPUT

The first line contains an integer T, denoting the number of test cases.

The next T lines contain an integer N.

OUTPUT

For each test case, print one line of output which contains 2 integers separated by a space. The first integer is the number of times 0 is printed. The second integer is the number of times 1 is printed.

CONSTRAINTS

1 <= T <= 50

0 <= N <= 40

SAMPLE INPUT

2

0

3

SMAPLEOUTPUT

1 0

1 2

- 0of 0 votes
If you look at the following pseudo-code, which computes the n-th Fibonacci number:

int fibonacci(int n)

{

if (n == 0)

{

print(0)

return 0

}

if (n == 1)

{

print(1)

return 1

}

return fibonacci(n - 1) + fibonacci(n - 2)

}

If one calls fibonacci(3), then the following will happen:

* fibonacci(3) calls fibonacci(2) and fibonacci(1) (the first call).

* fibonacci(2) calls fibonacci(1) (the second call) and fibonacci(0).

* The second call of fibonacci(1) prints 1 and returns 1.

* fibonacci(0) prints 0 and returns 0.

* fibonacci(2) gets the results of fibonacci(1) and fibonacci(0) and returns 1.

* The first call of fibonacci(1) prints 1 and returns 1.

* fibonacci(3) gets the results of fibonacci(2) and fibonacci(1) and returns 2.

In total, 1 will be printed twice and 0 will be printed once.

We want to know how many times 0 and 1 will be printed for a given integer N.

INPUT

The first line contains an integer T, denoting the number of test cases.

The next T lines contain an integer N.

OUTPUT

For each test case, print one line of output which contains 2 integers separated by a space. The first integer is the number of times 0 is printed. The second integer is the number of times 1 is printed.

CONSTRAINTS

1 <= T <= 50

0 <= N <= 40

SAMPLE INPUT

2

0

3

SMAPLEOUTPUT

1 0

1 2

- 0of 0 votes
The strength of a pair integer sequences is defined by the number of integers that they have in common. You are required to find the strength of several pairs of integer sequences.

INPUT

The first line of input contains T, the number of test cases. T test cases follow. Each test case contains 3 lines. The first line contains two integers N and M, which are the lengths of the two sequences. The next two lines contain the sequences.

OUTPUT

This should contain T lines, each containing an integer representing the strength of the pair of sequences for the corresponding test case.

CONSTRAINTS

The length of each sequence will be between 1 and 20 inclusive

A sequence can contain an integer between 1 and 100 inclusive

Sequences will not contain duplicate integers

SAMPLE INPUT

3

4 4

1 2 3 4

3 4 5 6

4 5

1 2 3 4

1 2 3 5 6

3 4

1 2 3

5 6 7 4

SAMPLE OUTPUT

2

3

0

- 0of 0 votes
The binary weight of a positive integer is the number of 1's in its binary representation. For example, the decimal number 1 has a binary weight of 1, and the decimal number 7 (which is 111 in binary) has a binary weight of 3.

Given a positive integer N, find the smallest integer greater than N that has the same binary weight as N.

INPUT

The first line of input contains a number T the number of test cases. The next T lines contain a number N.

OUTPUT

For each test case output a line containing the smallest number greater than N which has the same binary weight as N.

CONSTRAINTS

1 <= N <= 10000

SAMPLE INPUT

2

3

7

SAMPLE OUTPUT

5

11

- 0of 0 votes
Given an integer N, find the smallest integer greater than N which is prime.

INPUT

The first line of input contains T, the number of test cases. T test cases follow. Each test case contains a single integer N.

OUTPUT

This should contain T lines, each containing the smallest prime integer greater than N.

CONSTRAINTS

1 <= T <= 5

1 <= N <= 100

SAMPLE INPUT

2

6

11

SAMPLE OUTPUT

7

13

- 0of 0 votes
in C++, Suppose we are creating 2 objects like: "sample *s1 = new sample(); and sample *s2 = new sample(); then in this case what will happen if we do like: *s1 = *s2;" Please provide me the answer in detail

- 0of 0 votes
How to find the number of static objects and dynamic objects created for a class?

Let say,`class MyClass { public: }; int main() { MyClass cls;//Static Object MyClass *obj = new MyClass();//Dynamic Object ... ... //So on } void NewFun() { MyClass my; MyClass *Obj1; }`

It should work for all the cases, like big or small projects

- 0of 0 votes
How do you protect your object of your class being deleted by the clients?

- -3of 3 votes
Question on function hiding using inheritance.

- 0of 0 votes
Question 3 / 3 (Find first unique character)

Find the first unique character in a Stream. Please note that you are being provided a stream as a source for these characters.

The stream is guaranteed to eventually terminate (i.e. return false from a call to the hasNext() method), though it could be very long. You will access this stream through the provided interface methods.

A call to hasNext() will return whether the stream contains any more characters to process.

A call to getNext() will return the next character to be processed in the stream.

It is not possible to restart the stream.

If there is no unique character, then return the character '#'. # won't be any character in the character stream.

You just have to complete the function getUniqueCharacter() using the functions hasNext() and getNext() which are already defined.

Example:

Input:

aAbBABac

Output:

b

Input:

aBBa

Output:

#

- 2of 2 votes
Question 1 / 3 (Odd even level difference)

You are given a function calcDifference which takes in the root pointer of a binary tree as it's input. Complete the function to return the sum of values of nodes at odd height - sum of values of node at even height. Consider the root node is at height 1

Sample Input:

Sample Output

-74

Explanation:

[ (1 + 4 + 5 + 6 + 7 ) ? (2 + 3 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + 15) = -74 ]

- 0of 0 votes
write a program to print a matrix

1 2 3

4 5 6

7 8 9

I need to print like following

1 2 3 6 9 8 7 4 5

- -2of 2 votes
we create 2 class A, B and we declare 2 variable in Class B . whatever we do change in Class B i want to reflect in Class A ?? How We can Do???