Tag Archives: interview question

Career Cups easily the best site for Java, C++ interview questions from places like Bloomberg, Goldman Sachs, Microsoft, Amazon, etc

Career Cups easily the best site for Java, C++ interview questions from places like Bloomberg, Goldman Sachs, Microsoft, Amazon, etc
There are so many of these great questions from the Career Cup site. This is easily the best and constantly has the freshest lot. Keep your eyes peeled on this site as your interview draws closer. There are so many more of the below examples. Just ensure you know the questions and the CORRECT answers as you will find some answers are incorrect. Below are just some examples:

From career cup:
thefourthmusketeer’s Interview with Bloomberg LP
Bloomberg LP Interview Question for Software Engineer / Developers about Algorithm
thefourthmusketeer on May 27, 2010

Find out the first non duplicate character in a string.
eg: “nasa” output: ‘n’
I have given a solution using hashmap.
Its o[n]+ o[n] performance. ( constructing hashmap + traversing the string )
He wanted solution with just o[n] which I was not able to write there.
14
[Full Interview Report]

Read Question »
Question #2346664 (Report Dup) | Edit
Bloomberg LP Interview Question for Software Engineer / Developers about C++
thefourthmusketeer on May 27, 2010

Write a function which takes x as a parameter and prints f(x)given that
f(0)=1;
f(1)=0;
f(x+2)=F(x)-f(x-1);
I wrote both the iterative and recursive versions.
the interviewer then modified the question
In the recursive version, print all the numbers in the series till the input parameter
6
[Full Interview Report]

Read Question »
Question #2379664 (Report Dup) | Edit
Bloomberg LP Interview Question for Software Engineer / Developers
thefourthmusketeer on May 27, 2010

I was asked a question to get all the numbers from a file with a number per line.
I used streams and stored them in a vector.
However he told me that using a vector wastes space. How do you optimise . I told him I would use a linked list.
But he said he wanted to use a vector construct.
5
[Full Interview Report]

Read Question »

NOTE I now post my TRADING ALERTS into my personal FACEBOOK ACCOUNT and TWITTER. Don't worry as I don't post stupid cat videos or what I eat!

Job Interview question: How to stream output, open file, and read file in C++ and C

Job Interview question: How to stream output, open file, and read file in C++ and C

To stream to clog, set:
#include
#include

void main ( )
{
using namespace std;

// open the file “file_name.txt”
// for reading
ifstream in(“file_name.txt”);

// output the all file to stdout
if ( in )
cout << in.rdbuf(); else // if the ifstream object is in a bad state // output an error message to stderr clog << "Error while opening the file" << endl; } How to open and read a file: #include

void readone();

int main(int argn, char **argv)
{
int i;
for (i=0; i<=2; i++){ readone(); } } void readone() { static int ctr = 0; FILE *f = fopen("test.txt", "r"); char str[100]; int i; for (i=0; i<=ctr; i++){ fscanf(f, "%[^\n]\n", str); } puts(str); fclose(f); ctr++; } Beaware of ftell and fseek: /* ftell example : getting size of a file */ #include

int main ()
{
FILE * pFile;
long size;

pFile = fopen (“myfile.txt”,”rb”);
if (pFile==NULL) perror (“Error opening file”);
else
{
fseek (pFile, 0, SEEK_END);
size=ftell (pFile);
fclose (pFile);
printf (“Size of myfile.txt: %ld bytes.\n”,size);
}
return 0;
}

NOTE I now post my TRADING ALERTS into my personal FACEBOOK ACCOUNT and TWITTER. Don't worry as I don't post stupid cat videos or what I eat!

C++ Job interview question: Inheritance and object orientation source code example to create class object on heap vs stack

C++ Job interview question: Inheritance and object orientation source code example to create class object on heap vs stack

This may be a stupid example but does show the difference between stack and heap object creation using class object:

#include
using namespace std;

class MyClass {
public:
MyClass(int x) {}
MyClass() {}
};

int main() {

MyClass *myclass = new MyClass[10]; // creates class object on heap
int i;

int j[] = {1,2,3,4,5,6,7,8,9,10};

MyClass mc2[10] = {1,2,3,4,5,6,7,8,9,10}; //creates class object on stack

MyClass* mc[10];
for (int i=0; i < 10; i++) mc[i] = new MyClass(i); //it will call user-defined constructor here. return 0; }

NOTE I now post my TRADING ALERTS into my personal FACEBOOK ACCOUNT and TWITTER. Don't worry as I don't post stupid cat videos or what I eat!

C++ Job interview question: Old school bsearch and qsort source code example from the world of C

C++ Job interview question: Old school bsearch and qsort source code example from the world of C.

This may give you a better understanding of trees are you move into C++.

Here are some traditional binary and quick sort source code example

/* bsearch example */
#include
#include

int compareints (const void * a, const void * b)
{
return ( *(int*)a – *(int*)b );
}

int values[] = { 10, 20, 25, 40, 90, 100 };

int main ()
{
int * pItem;
int key = 40;
pItem = (int*) bsearch (&key, values, 6, sizeof (int), compareints);
if (pItem!=NULL)
printf (“%d is in the array.\n”,*pItem);
else
printf (“%d is not in the array.\n”,key);
return 0;
}

/* qsort example */
#include
#include

int values[] = { 40, 10, 100, 90, 20, 25 };

int compare (const void * a, const void * b)
{
return ( *(int*)a – *(int*)b );
}

int main ()
{
int n;
qsort (values, 6, sizeof(int), compare);
for (n=0; n<6; n++) printf ("%d ",values[n]); return 0; }

NOTE I now post my TRADING ALERTS into my personal FACEBOOK ACCOUNT and TWITTER. Don't worry as I don't post stupid cat videos or what I eat!

Is this the toughest C++ interview question? Palindrome with strings

Is this the toughest C++ interview question? Palindrome with strings

This could easily one of the hardest questions you could be asked in an advanced senior level C++ interview. This program has been described as self containing which can find the center of a palindrome and will do it o(n). The algorithm comes from Haskell authored by Johan Jeuring.

#include
#include
#include

// Simple linked list functions
typedef struct Node Node;
struct Node {
int len;
Node* next;
};

Node* insertNode(Node* head, Node* node) {
node->next = head->next;
head->next = node;
return node;
}

Node* createNode(int len) {
Node* newNode = (Node*) malloc(sizeof(Node));
newNode->len = len;
return newNode;
}

Node* insertNum(Node* head, int len) {
return insertNode(head, createNode(len));
}

void printReverse(Node* node) {
if (node != NULL) {
printReverse(node->next);
printf(“%d “, node->len);
}
}

void printReverseList(Node* head) {
printReverse(head->next);
printf(“\n”);
}

void cleanList(Node* head) {
Node* curr = head;
while (curr != NULL) {
Node* next = curr->next;
free(curr);
curr = next;
}
}

int main(int argc, char* argv[]) {
if (argc < 2) { printf("USAGE: ./pal string\n"); return 0; } char* a = argv[1]; int n = 0; int len = strlen(a); int currTail = 0; // length of the longest tail palindrome Node* centers = createNode(0); centers->next = NULL;
while (n < len) { if (n - currTail != 0 && a[n - currTail - 1] == a[n]) { n++; currTail += 2; continue; } Node* center = centers->next;
insertNum(centers, currTail);
int centerDist = currTail;
while (centerDist != 0 && center != NULL && centerDist – 1 != center->len) {
centerDist–;
insertNum(centers, center->len > centerDist ? centerDist : center->len);
center = center->next;
}
if (centerDist == 0) {
n++;
currTail = 1;
} else {
currTail = center->len;
}
}
Node* center = centers->next;
insertNum(centers, currTail);
while (currTail != 0) {
currTail–;
insertNum(centers, center->len > currTail ? currTail : center->len);
center = center->next;
}
printReverseList(centers);
cleanList(centers);
return 0;
}

NOTE I now post my TRADING ALERTS into my personal FACEBOOK ACCOUNT and TWITTER. Don't worry as I don't post stupid cat videos or what I eat!

Here is Source code of a commonly asked C++ job interview question: Fibonacci series

Here is Source code of a commonly asked C++ job interview question: Fibonacci series

#include

int main()
{
//define first two fibonacci series numbers.
int fib1 = 0;
int fib2 = 1;

//declare the variable to store the next number of fibonacci series
int fib3;

//declare the variable to store how many numbers to be printed. Default is 2.
int numbers = 2;

//the counter to keep track how many numbers are printed.
int counter = 2;

//Ask user how many numbers of the fibonacci series need to be printed.
std::cout << "How many Fibonacci number you need ? : " ; //Store the number. std::cin >> numbers;

//If number entered is less than 3, exit the program.
if (numbers < 3) return 0; //Print the first two element. std::cout << fib1 << "\t" << fib2; //do-while loop to calculate the new element of the series and printing the same. do { counter++; fib3 = fib1 + fib2; std::cout << "\t" << fib3; fib1 = fib2; fib2 = fib3; } while (counter <= numbers); std::cout << std::endl; system("pause"); return 0; }

NOTE I now post my TRADING ALERTS into my personal FACEBOOK ACCOUNT and TWITTER. Don't worry as I don't post stupid cat videos or what I eat!

C++ interview question: How to merge one array into another also known as merge sort

C++ interview question: How to merge one array into another also known as merge sort

How to merge one array into another. A good example would involve a merge sort like:
void MergeSort(apvector &arrayA, apvector &arrayB, apvector &arrayC)
{
int indexA = 0; // initialize variables for the subscripts
int indexB = 0;
int indexC = 0;

while((indexA < arrayA.length( )) && (indexB < arrayB.length( )) { if (arrayA[indexA] < arrayB[indexB]) { arrayC[indexC] = arrayA[indexA]; indexA++; //increase the subscript } else { arrayC[indexC] = arrayB[indexB]; indexB++; //increase the subscript } indexC++; //move to the next position in the new array } // Move remaining elements to end of new array when one merging array is empty while (indexA < arrayA.length( )) { arrayC[indexC] = arrayA[indexA]; indexA++; indexC++; } while (indexB < arrayB.length( )) { arrayC[indexC] = arrayB[indexB]; indexB++; indexC++; } return; } The one powerful concept of arrays is their use to traverse the array as you can access each element using the same expression. All you need to is have an interated statement in which the variable i can be used as a counter incrementing from zero to the length minus one.

NOTE I now post my TRADING ALERTS into my personal FACEBOOK ACCOUNT and TWITTER. Don't worry as I don't post stupid cat videos or what I eat!

C++ template job interview questions you can expect include errors and macro differences

C++ template job interview questions you can expect include errors and macro differences

This is a typical template function:
template
T sum(T* arr, int num){
T sum=0;
for(int i=0;i
T min (T i, T j)
{
return i < j ? i : j; } Below will generate errors: template
int Product (T1 a, T2 b, T3 c)
{
return a * b * c;
}

The class parameter arguments need to be separated by commas versus semi colons. Also, the function Product needs to return the class not int. To correct, do:

Tutorial Example:
template
T GetMin (T a, U b)
{
return (a NOTE I now post my TRADING ALERTS into my personal FACEBOOK ACCOUNT and TWITTER. Don't worry as I don't post stupid cat videos or what I eat!

c++ Memory related job interview questions involving memory related

c++ Memory related job interview questions involving memory related
For the resize function for containers like vector. This will return a bad_alloc there is no available memory left to resize for the container. Resize will copy the original vector and reclaims the space. It will also add the elements on the back. The memory will be allocated in another spot (if available), and the contents of the vector are copied into this space. This memory allocation is based on two strategies of best fit or first fit.
Stack vesus heap memory allocation:
For stack, local variables through functions are declared here unless they are static or register type. Function parameters are copied here as well. Note that local variables are not automatically initialized so garbage values will be stored here as well. All local variables are automatically destroyed as the function goes out of scope. They are then recreated for each time the function is called.
Declared variables (??) are stored on the heap before the execution of the program takes place. These are initialized to zero while they are not automatically destroyed when the function end (or goes out of scope) but you will not have access to them. Global, static, and register variables are created on the heap as well. Memory allocated by new, malloc, and calloc are created on the heap too.
There are other types of memory including unitialized including static variables before the execution of the program. They are also initialized as zero as well. This is also known as Block Started Symbol (BSS).
Some trickier questions to be asked:
Can class size and object size be the same?

If the class hsa static members, the static members are not stored in the object themselves. This is not the case if the class has not been instantiated
Why should malloc never be directly used to dynamically allocate instances of a class?
Malloc never allocates memory for objectsize * and it does not initialize the class (i.e. invoke the constructor or destructor) where new allocates the memory of objectsize * and implicitly invokes the constructor (the default would be considered if the class constructor does not exist). This is to initialize the class.

Difference between malloc/free and new/delete?
Malloc will allocate memory for the object on the heap, but it will not the object’s constructor to initialize the object. new allocates memory and also invokes constructor to initialize the object.
malloc() and free() do not support object semantics
Does not construct and destruct objects
string * ptr = (string *)(malloc (sizeof(string)))
Are not safe
Does not calculate the size of the objects that it construct
Returns a pointer to void
int *p = (int *) (malloc(sizeof(int)));
int *p = new int;
Are not extensible
new and delete can be overloaded in a class
“delete” first calls the object’s termination routine (i.e. its destructor) and then releases the space the object occupied on the heap memory. If an array of objects was created using new, then delete must be told that it is dealing with an array by preceding the name with an empty []:-
Int_t *my_ints = new Int_t[10];

NOTE I now post my TRADING ALERTS into my personal FACEBOOK ACCOUNT and TWITTER. Don't worry as I don't post stupid cat videos or what I eat!

Interview question tip: Good c++ Destructors practices

Good c++ Destructors practices
It is good practice to have virtual destructors if the base class has any virtual functions. Members allocated on the heap must be properly freed within the destructors. This usually includes members, network resources like sockets, mutexes, and graphical resources.

NOTE I now post my TRADING ALERTS into my personal FACEBOOK ACCOUNT and TWITTER. Don't worry as I don't post stupid cat videos or what I eat!