Category Archives: Quant Development

Quant Development

Everything you need to know about binary trees and circular linked lists for C++ job interview questions

Everything you need to know about binary trees and circular linked lists for C++ job interview questions

In order to properly build a binary tree, you need to do the following steps:

1) Inorder traversal of a Binary search Tree prints the data in the increasing order.
2) While doing in order traversal, at each step, build the linked list adding the each element at the end of linked list.
3) At each step, also make the end of the linked list to point to the first element of the linked list. This will make the linked list circular.

Here is a code sample of a binary sorted tree that can be converted to a linked list (this also ensures the list is circular):

#include
using namespace std;
#include
#include
#include
#include
#include
#include
#include
#define MAX 1000
#define max(a,b) (a)>=(b)?(a):(b)
#define REP(i,n) for(i=0;i
/*** structure of the tree node ****/
struct node
{
int data;
node *small;
node *large;
};
/* fun to insert element in the binary search tree */
void insert(node **root,int val)
{
node *tmp;
if(*root==NULL)
{
tmp=(node*)malloc(sizeof(node));
tmp->data=val;
tmp->small=NULL;
tmp->large=NULL;
*root=tmp;
}
else
{
if(val<(*root)->data)
insert(&((*root)->small),val);
else
insert(&((*root)->large),val);
}
}
/* this function joins two given nodes */
void join(node **a,node **b)
{
(*a)->large=(*b);
(*b)->small=(*a);
}

/* this function appends second list behind first list */
node *append(node *head1,node *head2)
{
if(head1==NULL)
return head2;
if(head2==NULL)
return head1;

node *alast,*blast;
alast=head1->small;
blast=head2->small;

join(&alast,&head2);
join(&blast,&head1);
return head1;
}

/**** most important function ,uses recursion to convert bst to circular ll ***/

node* treetolist(node *root)
{
if(root==NULL)
return NULL;

node *alist,*blist;
alist=treetolist(root->small);
blist=treetolist(root->large);

root->small=root;
root->large=root;

alist=append(alist,root);
alist=append(alist,blist);

return alist;
}

/* funtion to display bst */
void display(node *root)
{
if(root!=NULL)
{
display(root->small);
cout<data<<"\t"; display(root->large);

}
}

/* function to display ll */
void displayl(node *head)
{
for(int i=1;i<=5;i++) { cout<data<<"\t"; head=head->large;
}
}
int main()
{
node *root=NULL;
node *head;

/* test values */
insert(&root,10);insert(&root,4);insert(&root,8);insert(&root,15);insert(&root,9);
display(root);
cout< 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 the world famous how many characters in this C++ string job interview question?

Here is the world famous how many characters in this C++ string job interview question?

Also, here is a way to find the number of words in a string (same as count number of specific characters) This always reports 1 more than so maybe display count – 1?

#include
#include
using namespace std;

int main()
{
int i, numspaces;
char nextChar;
string msg;

numspaces=1;

cout << "Type in a string\n"; getline(cin, msg); // checks each character in the string for (i=0; i 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 using O[n^2]

Is this the toughest C++ interview question? Palindrome with strings using O[n^2]

This could easily one of the hardest questions you could be asked in an advanced senior level C++ interview. Here are two attempts o[n^2]

#include
using namespace std;

void longestPalindrome(char *str, int n)

{
int i=0;
int j=0;
int start = 0;
int end = 0;
int range = 0;
int maxstart = 0;
int maxend = 0;

while(i < n && j < n) { start = i; end = j; while((str[start] == str[end]) && (start >=0) && (end <= n)) { if((maxend - maxstart) < (end-start)) { maxend = end; maxstart = start; } end++; start--; } start++; end++; } for(int i = maxstart; i <= maxend; i++) printf("%c", str[i]); } int main() { char * str = "RACECAR"; cout << strlen(str);; longestPalindrome(str,sizeof(str)); //also: based on O(n^2) char a[]="abcdaeeadabb"; char *p,*q,*r,*found; int i=0; int max=0; for(p=a;*p!='\0';p++) { q=p; r=a+strlen(a); while(qmax) {max=i; found=p;}
}
if(q==r) max=max+1;
cout<<"max is: " << max<<*found; 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!

All the potential string related questions asked in a C++ job interview

All the potential string related questions asked in a C++ job interview
The differences between C string and the string class in C++ is that C strings are handled by standard function and are more efficient since they are arrays of characters. C++ string class has more encapsulated functionality.
You may get asked about substring replacement but there are usually three options you can choose. You could use start index of a substring, use KMP in O[n]) algorithm, or brute force using O(n*m).
Here is a difficult brute force way:

#include
#include
using namespace std;

int main(int argc, char *argv[]) {

int idx = 0;
char target[100];
char *pTag = target;
char sub[10];
char replace[100];
char *pRpr = replace;
if(argc != 4)
{
cout << "Exit... Bye Bye\n"; exit(1); } strcpy(target, argv[1]); strcpy(sub,argv[2]); strcpy(replace,argv[3]); //Loop until target 0 char * pTg, * pSb, *pRp; pRp = replace; while(pTag != '\0') { pTg = pTag; pSb = sub; while(*pSb != '\0' && *pTg != '\0' && *pTg++ == *pSb++); if(*pSb == '\0' && *(pTg -1) == *(pSb -1)) { while(*pRp != '\0')pRp++; while((*pRp++ = *pTg++) != '\0'); while((*pTag++ = *pRpr++) != '\0'); cout << target << "\n"; return 0; } pTag++; } return 0; } Here is a much easier sub str str way: #include
using namespace std;

void replace(char *target, char *substring, char *replace);

int main()
{
char target[100] = “MyNameIsGajanan”;
char *substring = “Gajan”, *rep = “AAA”;
//cout << target <FACEBOOK ACCOUNT and TWITTER. Don't worry as I don't post stupid cat videos or what I eat!

Why your quant trade systems gives edge so don't sell it for millions

Here is a conversation on reddit.com which pretty well inspired me tostart thinking about blending my knowledge of technology and financial investment. This was pretty inspiring where an indie software engineer launched his own business. He is now clearing in the millions per year. Source was:

IAMA 100% automated independent retail trader. I trade around 800k to 1.5 million shares a day and make 2cents/trade on average. AMAA from IAmA

whatswith_this 1 point2 points3 points 6 months ago[+] (3 children)

Top of Form

Top of Form

Top of Form

sell your program for thousands?

Bottom of Form

mejalx [S] 11 points12 points13 points 6 months ago[+] (8 children)

mejalx [S] 11 points12 points13 points 6 months ago[-]

Top of Form

Why? Edges are short lived, I’d rather squeeze out every penny I can while I can. I’d sell a model after it quit working, but I’m not a douche bag.

Bottom of Form

Felix_D 1 point2 points3 points 6 months ago[+] (7 children)

Felix_D 1 point2 points3 points 6 months ago[-]

Top of Form

I’m not sure what you mean by “edge”? Could you please clarify?

Bottom of Form

AnonysaurusRex 2 points3 points4 points 6 months ago[+] (6 children)

AnonysaurusRex 2 points3 points4 points 6 months ago[-]

Top of Form

Edge = advantage.

At the moment his system gives him an advantage over other traders, this advantage is known as an “Edge”. If he sold his system, he would be giving his “Edge” to other traders and subsequently, loosing his advantage.

Bottom of Form

cookiecaper 0 points1 point2 points 6 months ago[+] (5 children)

cookiecaper 0 points1 point2 points 6 months ago[-]

Top of Form

And so the question becomes: will guy make more money selling the “edge” while it’s good or just riding it out and keeping it to himself? If he sold the program for $500 and then just charged $250/strategy and staggered the release a little bit and/or charged more for better strategies maybe he’d make a ton of money. It’d be a good way to help people who need the cash and maybe, I don’t know, but maybe it’d make more than just keep that strategy to himself and waiting for it to die out.

Bottom of Form

omargard 0 points1 point2 points 6 months ago[+] (4 children)

omargard 0 points1 point2 points 6 months ago[-]

Top of Form

Sell a program that makes millions for $500? That’s funny.

mejalx believes his strategies could work with ten times the amount of money(volume). If he sells his edge to more than 10 people with the same amounts of money, or one person with 10 times the amount of money to “invest”, they have to share the profits.

Probably a big fund with $1B available capital and faster connections than mejalx (and people like you) would buy this program for $500 – lol – and exploit the edge to the maximum, leaving nothing for you.

Bottom of Form

komphwasf3 0 points1 point2 points 15 days ago[+] (0 children)

komphwasf3 0 points1 point2 points 15 days ago[-]

Top of Form

Well, maybe that number is off, but I’d think of it like selling a car, when the machinery used to make it is worth $ten million.

So cookiecaper’s idea is sound, but omargard is right in that if it were sold, it’d probably be sold for closer to $10k or $100,000k, since it earns millions

Although if it breaks down any edges, then it’s not a good idea to sell it

Bottom of Form

cookiecaper 0 points1 point2 points 6 months ago* [+] (2 children)

cookiecaper 0 points1 point2 points 6 months ago* [-]

Top of Form

Uhhhhhuh. I’m sure he could find plenty of corporate buyers. Really, I just made up numbers for fun.

And it doesn’t make millions all the time, especially not if multiple people have it. He can sell the base for something reasonable, give starter algorithms, and so on, and sell them for more as more goes on. Also, I don’t suggest that this become a mass-market endeavor; just something to keep between friends.

ANYWAY, doesn’t matter.

Bottom of Form

load more comments (2 replies)

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!

How to convert string to double dollars using C++ string stream, a STL container, and locale

How to convert string to double dollars using C++ string stream, STL containers, and locale

#include
#include
#include
#include
#include

using namespace std;

void to_dollar(int a);

int my_number_format(int num)
{
int idx;
char buffer[64];

idx = sizeof(buffer)-1;
buffer[idx–] = 0; // end zero

int count = 0;
while (num > 0) {
if (0 == idx)
return -1;
if (count > 2) {
buffer[idx–] = ‘,’;
if (0 == idx)
return -1;
count = 0;
}
count++;
buffer[idx–] = ‘0’+(num % 10);
num /= 10;
}

buffer[idx] = ‘$’; // currency sign
return printf(“%s\n”, &buffer[idx]);
}

int main()
{
int userInput;

cout << "Please enter an integer-:> ” << endl; cin >> userInput;
int result;
result=my_number_format(userInput);
return (0);
}

void to_dollar(int a)
{
stringstream inString;
string strTemp;
long double intTemp;

inString << a; // convert integer to string for easier handling inString.str(inString.str()+"00"); // concatenate two zeos to denote currency fractional digits inString >> intTemp; // convert back to integer

locale loc(“english_USA”); // set the locale
cout.imbue(loc);

cout.setf(cout.flags() | ios::showbase); // turn on currency sign printing
const money_put& m_put = use_facet >(loc); // get the money_put facet
m_put.put(cout, false, cout, ‘ ‘, intTemp); // show me the money. Note to use fractionally adjusted value.
cout << endl; }

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!

Covering C++ interviews questions on const, initialization list, global variables, and static

Covering C++ interviews questions on const, initialization list, global variables, and static
Note that non static const data members and reference data members cannot assign values. You would need to have these initialized in the initialization list.

Initialization lists can be listed as private after the constructor definition.
class Baz
{
public:
Baz() : _foo( “initialize foo first” ), _bar( “then bar” ) { }

private:
std::string _foo;
std::string _bar;
};

In order to access non static members of a class from a static method, you need to declare a class instance inside the static instance. A static method can only use in class’s role is to use static members only. Static methods can be accessed by non static and static data members. Static methods cannot be volatile, constant, or virtual. Also for example, for app.cpp or app.c:
static void print_some_data()
{
}
is limited in abc.cpp/c only, no other compilation unit in an application can use it.

A common use for static to keep track the number of instances of a class.
Static member functions are used to access private static data member and they do not have a this pointer. They can only access static data members as they are not attached to instances of objects.
To access static member of a class, use ::
In short:
1. Within a function, there is one copy of the variable that retains its value even when the function returns.
2. Within a class, all objects share the static variable and the static variable is accessed using the class name.
3. In the global scope a static variable is local to the current file.

2.

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!