## C++ Interview Questions

- 0of 0 votes

AnswersCan we write a collection class in C++ that can store elements of different data types?

- learner123 April 22, 2014 in United States| Report Duplicate | Flag | PURGE

C++ - 1of 1 vote

AnswersGiven a number in an array form, Come up with an algorithm to push all the zeros to the end.

- kiranpm86 February 24, 2014 in India

Expectation : O(n) solution| Report Duplicate | Flag | PURGE

Amazon Quality Assurance Engineer Algorithm Arrays C++ Coding - -1of 1 vote

AnswersGive 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.

- kiranpm86 February 24, 2014 in India

E.g:-

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

b[3] = [1,3,7]| Report Duplicate | Flag | PURGE

Amazon Quality Assurance Engineer Algorithm Arrays C++ Coding - 0of 0 votes

AnswersHow do you implement stack in stl? What is the complexity?

- sasivara January 30, 2014 in United States| Report Duplicate | Flag | PURGE

Bloomberg LP Software Engineer / Developer C++ - 0of 0 votes

AnswersWhat is smart pointer? How do you implement? What happens with the following: p2 = p1;

- sasivara January 30, 2014 in United States

What happens P3(p1) (copy const)?| Report Duplicate | Flag | PURGE

Bloomberg LP Software Engineer / Developer C++ - 0of 0 votes

AnswersI have declared one double pointer character array as given below:

- ermksharma2009 January 28, 2014 in India

char **errorcode;

so how to initialize this array with NULL.

and pass to other function?????| Report Duplicate | Flag | PURGE

Siemens Software Engineer / Developer C++ - 0of 0 votes

AnswersStruct node{

- renjumc January 28, 2014 in India

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| Report Duplicate | Flag | PURGE

NetApp Software Engineer / Developer C C# C++ - -2of 2 votes

AnswersWrite a program to find the GCD of two numbers

- renjumc January 28, 2014 in India| Report Duplicate | Flag | PURGE

NetApp Software Engineer / Developer C C# C++ - 0of 0 votes

Answers

- aryan January 17, 2014 in India`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`

| Report Duplicate | Flag | PURGE

C++ - 0of 0 votes

Answers

- aryan January 17, 2014 in United States`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`

| Report Duplicate | Flag | PURGE

C++ - 0of 0 votes

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

- A.K. January 15, 2014 in United States for Shared file| Report Duplicate | Flag | PURGE

Citrix System Inc Software Engineer / Developer C++ - 2of 2 votes

AnswersHow 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

- chid1989 January 07, 2014 in United States| Report Duplicate | Flag | PURGE

NVIDIA Intern C++ - 0of 0 votes

AnswersThe 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.

- anas.love19 December 27, 2013 in United States

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| Report Duplicate | Flag | PURGE

Student student C++ - -2of 4 votes

AnswersLook at the following pseudo-code, which computes the n-th Fibonacci number:

- Eliana December 24, 2013 in United States for interviews

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| Report Duplicate | Flag | PURGE

Informatica iOS Developer C++ - 0of 0 votes

AnswersLook at the following pseudo-code, which computes the n-th Fibonacci number:

- Eliana December 12, 2013 in United States for games developing

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| Report Duplicate | Flag | PURGE

Akamai Computer Scientist C++ - 0of 0 votes

AnswersIf you look at the following pseudo-code, which computes the n-th Fibonacci number:

- Eliana December 12, 2013 in United States for games developing

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| Report Duplicate | Flag | PURGE

Akamai Computer Scientist C++ - 0of 0 votes

AnswersThe 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.

- Marey December 10, 2013 in United States for artificial intelligence team

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| Report Duplicate | Flag | PURGE

Achieve Internet Analyst C++ - 0of 0 votes

AnswersThe 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.

- Marey December 10, 2013 in United States for artificial intelligence team

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| Report Duplicate | Flag | PURGE

Achieve Internet Analyst C++ - 0of 0 votes

AnswersGiven an integer N, find the smallest integer greater than N which is prime.

- Marey December 10, 2013 in United States for artificial intelligence team

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| Report Duplicate | Flag | PURGE

Achieve Internet Analyst C++ - -1of 1 vote

Answersin 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

- manojvajpai December 09, 2013 in India for HCT-I| Report Duplicate | Flag | PURGE

GE (General Electric) Senior Software Development Engineer C++ - 0of 0 votes

AnswersHow 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

- rasmiranjanbabu November 20, 2013 in United States for Embedded| Report Duplicate | Flag | PURGE

Cognzant Technology Solutions Software Analyst C++ - 0of 0 votes

AnswersHow do you protect your object of your class being deleted by the clients?

- ravi02431 November 13, 2013 in India| Report Duplicate | Flag | PURGE

Tech Lead C++ - 0of 0 votes

AnswersQuestion 3 / 3 (Find first unique character)

- Harjit Singh September 27, 2013 in India for TCS

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:

#| Report Duplicate | Flag | PURGE

Amazon SDE1 Algorithm C++ Data Structures - 2of 2 votes

AnswersQuestion 1 / 3 (Odd even level difference)

- Harjit Singh September 27, 2013 in India for TCS

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 ]| Report Duplicate | Flag | PURGE

Amazon SDE1 Algorithm C++ Trees and Graphs - 0of 0 votes

Answerswrite a program to print a matrix

- ciitaram September 27, 2013 in India

1 2 3

4 5 6

7 8 9

I need to print like following

1 2 3 6 9 8 7 4 5| Report Duplicate | Flag | PURGE

Developer Program Engineer C# C++ - -2of 2 votes

Answerswe 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???

- viki September 15, 2013 in United States| Report Duplicate | Flag | PURGE

C++ - 5of 5 votes

AnswersA link list contains following elements

`struct node{ int data; node* next; node* random; }`

Given head of such a linked list write a function who copies such a linked list and returns the head of the new list. So if in the original list first node points to fourth node in random the copy should have the same relation. The random pointer can point to any node including itself and more than two nodes can have random pointer to the same node.

- vik September 13, 2013 in United States

Required time complexity O(n) and no extra space can be used (apart from the newly allocated memory which you will need to create the new list)| Report Duplicate | Flag | PURGE

Microsoft Software Engineer / Developer C++ Data Structures - -2of 4 votes

AnswersWrite a thread safe data structure such that there could be only one writer at a time but there could be n readers reading the data. You can consider that incrementing or decrementing a variable is an atomic operation. If more than one threads try to write simultaneously then just select one randomly and let others wait

- vik September 13, 2013 in United States| Report Duplicate | Flag | PURGE

Microsoft Software Engineer / Developer C++ Data Structures Operating System - -1of 1 vote

AnswerYou are planning a big programming conference and have received many proposals which have passed the initial screen process but you're having trouble fitting them into the time constraints of the day -- there are so many possibilities! So you write a program to do it for you.

- ruby.dhivi August 29, 2013 in India

· The conference has multiple tracks each of which has a morning and afternoon session. · Each session contains multiple talks. · Morning sessions begin at 9am and must finish by 12 noon, for lunch. · Afternoon sessions begin at 1pm and must finish in time for the networking event. · The networking event can start no earlier than 4:00 and no later than 5:00. · No talk title has numbers in it. · All talk lengths are either in minutes (not hours) or lightning (5 minutes). · Presenters will be very punctual; there needs to be no gap between sessions. Note that depending on how you choose to complete this problem, your solution may give a different ordering or combination of talks into tracks. This is acceptable; you don’t need to exactly duplicate the sample output given here.| Report Duplicate | Flag | PURGE

Software Engineer / Developer Algorithm C++

**CareerCup**is the world's biggest and best source for software engineering interview preparation. See all our resources.

Open Chat in New Window