## C++ Interview Questions

- 0of 0 votes
Design a phone book such that fields are searchable with name , with number. Later enhanced teh question asking searchable with address as well.

- 0of 0 votes
What happens when we pass negative indices to an array? Would the compiler allow it?

- 1of 1 vote
How to design a multi key hash map ( key count can be dynamic. if there are two keys , initiallly which can be used to find the value , keys can be increased to three as well ex: consider school structure. Intially , consider , student id is key , later , should be searchable even with key name , later with grade.

- 1of 1 vote
Design a telephone directory for large ppl (he gave example like design for India). fields will be , first name , last name , number . this should be searchable with first name , last name , number as welll.

later added more complexity like do the same for organisation where even it contains designations. so this should be searchable with designations.

- 0of 0 votes
what container will you choose if you should store numbers and check whether certain number is already stored?

- 0of 0 votes
How and where to initialize const data member in C++, if you did not initialize it in member initializes list?

- 1of 1 vote
Given a string (for example: "a?bc?def?g"), write a program to generate all the possible strings by replacing ? with 0 and 1.

Example:

Input : a?b?c?

Output: a0b0c0, a0b0c1, a0b1c0, a0b1c1, a1b0c0, a1b0c1, a1b1c0, a1b1c1.

- 0of 0 votes
Given a set of 21 tasks = {A, B,....Z} except I, O, U, X and Q. Each task requires 4 hours of processing. Except for tasks E, Y, P, R, W that require 8 hours of processing.

You have 3 machines to process these tasks = T1, T2, T3. T1 and T2 are available everyday for 8 hours. T3 is available only on Mon, Wed and Fri for 8 hours.

You are given 3 lists that indicate the dependency list among the tasks.

L1 = A->R->K->M (eg A can be completed if R is completed, R can be completed only if K is completed etc.)

L2 = N->G->V->E->Z->H

L3 = C->F->Y->D->J->P->T->S->W->B->C (cycle)

Each task needs one machine for its duration to complete.

Tasks cannot be resumed. Which means the 8 hour tasks cannot be executed over 2 days.

Each machine can process only one task at any time.

T1, T2 and T3 can process different tasks in parallel.

You are starting your schedule on a Wednesday.

Machines can be scheduled only during weekdays.

The first Monday in your schedule is a downtime for all the machines.

Given these constraints, write a program that generates a schedule between the tasks and machines such that all the tasks are completed at the

earliest.

- 0of 0 votes
You are given an array of N elements. Each element in the range Min of int to Max of Int. You need to find the length of longest sequence in this array such that difference of largest and smallest element of that sequence is 1. The sequence need not be sequential.

For e.g. array[]={6,10,6,7,8,9,0}

seq {6,10} = diff is 4 len 2

seq { 10,7,8} diff is 3 len 3

seq { 7,8,9} diff 2 len 3

seq {6,6,7} diff is 1 len 3

In this example the program should return 3 .

Complexity N*longN

- 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