## C++ Interview Questions

- 4of 4 votes
Given three arrays A,B,C containing unsorted numbers. Find three numbers a, b, c from each of array A, B, C such that |a-b|, |b-c| and |c-a| are minimum

Please provide as efficient code as you can.

Can you better than this ???

- 0of 0 votes
Print the output of linked list in the reverse order without altering the actual linked list.

First try it without recursion and then with recursion

Original linked list A->B->C->D

Output should be DCBA

- 0of 0 votes
If you run the same program twice, what section would be shared in the memory?

Follow up, is the text portion of memory share? Why not?

- 0of 0 votes
What do you do if your program does a core dump?

(Analyze code dump)

Name all the possible ways a program would do core dump.

- 0of 0 votes
Write a function that accepts an n-dimension array and prints its values--For array of any dimension.

What is the layout of multi-dimensional array in the memory?

- 0of 0 votes
Given a number n, write a function that writes a Fibonacci sequence to number n.

- 0of 0 votes
It was part of a bigger question --a large piece of code.

Implement << operator. What are the differences of implementation as a member function and a non-member function

- 0of 0 votes
What does an iterator in C++ point to in case of a vector vs. list. Where would it point to if the prior links are deleted in the list? In case of a vector if it points to a specific index, where would it point to if the prior indexes are deleted?

- 0of 0 votes
What C++ data structures would you use to implement LRU cache? Show implementation.

- 0of 0 votes
How would you implement this:

`object["String for a security name"]["another string"] = another_object`

- 0of 0 votes
What are the various ways of doing IPC in Unix/Linux? How do you implement it?

- 0of 0 votes
Design a system like friend's functionality in facebook. should have all features of facebook's friends functionality. like for each person , he can have any number of friends , he will get suggestions for new firends , showing common friends if we visits any other profile . algo should be scalable , robust .

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

- 5of 5 votes
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?????