[14] python (realistic) IV #misc

CV claims — “used py for sys automation”

Q: optimize – how did you optimize perf?
A: I didn’t need to for my scripts.

Q: try/catch + ….?
Q: immutable – what data types are mutable/immutable?
Q: threading – global interpreter lock?
A: never really needed threading
Q: xml – what xml parser did you use? ess477
Q: debugger? A: I didn’t need it

Q: read a config file?

Q: logging?
Q: DB access?
Q: command line arguments stored where? A: sys.argv?
Q78: exit normally? A: sys.exit()
Q78b: normally, “raise Exception” would print to stderr stream, but how do you direct that to a log file instead?
A: set sys.stderr stream. dive205 i.e. [[dive into python]]

Q: how do you handle newline diff between OS? ess114
Q: truth table? e68 (i.e. [[essentialRef]])
Q: how do you edit on windows and deply to linux?
A: samba, ftp. See also the post in pearl
— sys admin
Q: how do you pass input files to your py script? cook539
A: fileinput module is convenient.

Q: how is PYTHONPATH used with sys.path?

Q: another py — how do you execute another py script? Ess89 (i.e. [[essentialRef]])
Q: what command line options do you use often?

Q5: how do you launch an external program?
Q5b: how do you capture its output? c546 [[cookbook]] has a slightly advanced solution
Q5c: how do you call a unix shell command?
A: shutil module meets some of your needs

Q: exit with an error msg? cook540
A: raise SystemExit (‘msg’)

— data structures
Q: diff between listcomp and genexp? How would you choose?
Q: convert between list and tuple? cvc
Q: convert a list of objects to string and concat? see post in recrec
Q: split a string on multiple delimiters? cook37 explains re.split()
Q: iterating in reverse? cook119
==coding IV
Q: how do you print to a file rather than stdout?
A: e115 — print >> anOpenFile, ‘whatever’ # using the print KEYWORD
A: c144 — print (‘whatever’, file=anOpenFile) # using the print() function
Q: concat 2 lists?
Q: initialize a dict
Q: initialize a 2D array? m52 (i.e. [[perl to python migration]])
Q: walk a directory
Q: use a dict as a “seen”
Q: iterate a dict? mig87
Q: iterate a file
Q: interpolate into a string? ess115. c61 i.e. [[cookbook]]
Q: date/time usage (datetime module is OO;  time module is procedural?)
Q: trim both ends of a string? strip()


##9dataStruct(+..)for TIMED c++cod`IV

  1. linked node manipulation in a graph context
  2. vector (more useful than array), std::string (more useful than cStr). Many string operations are still unfamiliar
    1. Array-based data structures are required in 80% of my coding tests.
    2. More than 50% of all my coding tests require nothing but arrays.
    3. Most of my toughest coding tests are presented in arrays but may need maps as helpers
  3. string in std::string or c-str
  4. iterator manipulation like erase, lower_bound, find,
  5. sorting, operator<(), upper_bound, binary search … on containers
  6. sorted data structure like std::map
  7. [w] stringstream — ECT to improve

Very few Wall St interviewers would test you on graph or DP. Here are other less important constructs:

  1. [w] binary tree is common and simple, but they can ask very tough questions on it!
  2. [w] double pointer is trickier and my advantage
  3. [w] Node class in a linked data structure.
  4. [w] stack, queue.
  5. [w] grid or matrix
  6. file I/O? only for IDE tests, not pure algo or take-home tests
  7. [w] basic syntax for pointer arithmetic.
  8. [w] dtor, copier, op=? only for design questions, not algo questions.
  9. [w] shared_ptr? Only for design questions, never needed in algo questions!
  10. [w] ref variable only as function I/O.
  11. stl algo? Only Citadel array-shrink
  12. never exception
  13. never template
  14. no (very, very seldom) threading in coding Q
  15. adv: pointer to function
  16. adv: circular buffer
  17. [w = no weakness]


(Revisit) senior manager IV: what they watch out for

Common, non-trivial, perhaps hidden issues in a candidate, ranked:

  • twist and turn and not /candid/ about his past
  • not listening
    • jumping to conclusions
    • assuming too much
    • not waiting for a long-winded interviewer to finish, perhaps interrupting
    • jumping to the defensive, when deliberately provoked
  • desperate for the job and losing his perspective
  • opinionated
  • choosy on projects
  • conceited beyond “cool confidence”
  • —–secondary
  • impulsive answers ^ elusive, guarded answers
    • elusive answers? common
  • bad-mouth past managers
  • lack of humor? common
  • tensed up and not cool and calm, perhaps prone to workplace stress
  • exaggeration about his past? common
  • not articulate enough? common
  • poor eye contact? common

%% Quesiton to interviewer

As I get older, the soft interview is becoming more important more tricky. Part of the soft interview is my list of questions to interviewer.

Tip: If possible, ask a “salesy” question so you can highlight your memorable strengths. More importantly, ask smart, serious questions.

How about “truthful” question that show your real concern? I feel there’s very limited value in truthfulness here.

Some people (like me) don’t like [e]asy questions, but interviewer may find it too sensitive and blunt, though they can always dodge it.

  • [e] Q: between quality and speed, what’s more important in everyday situations?
  • Q: bottlenecks and what was/is done about each
  • [e] Q: what are your challenges?
    • resource?
    • people not communicating enough?
    • requirement changing too fast?
  • [e] Q: what design trade-offs have you made?
  • [e] Q: what user groups, and what challenges do each present?
  • [e] Q: what types of team player are needed here?
  • [e] Q: how is performance *measured*?
  • [e] Q: your management style?
  • [e] Q: median age among the developers?
  • Q: Are expectations any different on an older developer (say, my age)
  • Q: who is the most important (not necessarily biggest) competitor?
  • Q (only for the hiring manager): Based on what you have learned about me in this interview, do you have any questions or concerns about my ability (not suitability) to do this job?
    • Note This is a pretty aggressive question, but if there is a good rapport built during the interview, it can be useful. More assertive hiring managers like this as it shows the candidate is not shy about seeking feedback. This can also clear the air of any lingering concerns or possible miscommunications/misconceptions that are a subtext to the interview.


MSVS linker option -L vs -l #small L

Linker option –l (small ell) for the lib file name; -L for to specify the search path


Finally, you must tell the linker where to find the library files. The ST-Developer Windows installation contains programming libraries built for use with Visual C++ 2005 (VC8) and 2008 (VC9) with the /MD flag, as well as versions that are link compatible with other compilers. These are kept in different subdirectories under the ST-Developer “lib” directory. The library directories available for Visual Studio 2005 and 2008 are as follows:

  • $(ROSE)/lib/i86_win32_vc8_md (2005, 32bit platform, /MD)
  • $(ROSE)/lib/x64_win64_vc8_md (2005, 64bit platform, /MD)
  • $(ROSE)/lib/i86_win32_vc9_md (2008, 32bit platform, /MD)
  • $(ROSE)/lib/x64_win64_vc9_md (2008, 64bit platform, /MD)

Select the Linker category of properties and, pick the General options under that. In the Additional Library Directories property, add the appropriate ST-Developer library directory.


I know more c++than c#(spent X years full time)

Action: spend enough (30?) hours revisiting my 1000+ cpp (including socket, stream..) blog posts and explore just a little beyond them.

(I blogged about exactly the same observation before …) Here’s a paradox — i spent 2 years in a full time c# job, arguably longer than my full time c++ experience. However, sometimes I feel I understand dotnet less than C++.

Reason? Many aspects of the languages are never used in-depth on work projects, so length of experience != depth of experience. We need spare time self-exploration to learn those aspects like:

– template meta programming
– custom operator new/delete
– memory tuning
– memory leak detection
– threading
– async communications
– pure virtual
– ++i vs i++

(… not a complete list by any measure.) Interviewers are notorious for testing these obscure or advanced, theoretical topics.

file/console input output ] python

Output typically uses “print”.

print >> myFileObject , arguments
print >> sys.stderr , arguments…

Quiz: so, in that case, how do you print to stdout?

Input from file is very common, so that's another quiz

Input from std input —

aLine = sys.stdin.readline()
all_the_Lines = myFileObject.readlines()

Some prefer the object-oriented way to output


##which common UDP/TCP functions are blocking

A non-blocking send fails when it can’t send a single byte, usually because destination send buffer (for TCP) is full. For UDP, see [4]

Note read() and write() has similar behavior. See send()recv() ^ write()read() @sockets and http://www.mathcs.emory.edu/~cheung/Courses/455/Syllabus/7-transport/flow-control.html

[1] meaning of non-blocking connect() on TCP is special. See P51[[tcp/ip sokets in C]] and https://www.scottklement.com/rpg/socktut/nonblocking.html
[2] non-blocking accept() is obscure knowledge — See https://www.scottklement.com/rpg/socktut/nonblocking.html
[3] select() on a non-blocking socket is obscure knowledge —  See https://www.scottklement.com/rpg/socktut/nonblocking.html
[4] UDP has no send buffer but some factors can still cause blocking

default flags arg to func fcntl on entire socket touching TCP buffers?
select blocking not supported? still blocking! [3]  no
epoll blocking not supported?  no
recv blocking can change to NB can change to NB  yes
send/write TCP blocking frequently can change to NB can change to NB  yes
recvfrom blocking can change to NB can change to NB  yes
sendto UDP blocking sometimes [4] can change to NB can change to NB  yes
accept blocking not supported? can change to NB [2]  yes
connect()TCP blocking not supported? can change to NB [1]  no
connect()UDP NB. Saves the destination
for future transfers
Not Applicable

my quicksort in python/c++

import random
# partition the given section by fixing the anchor
def partitionUsingFarRight(A, le, ri):
    pivotValue = A[ri] # immutable value
    pivotPos = i = ri
    while True:
        if (i <= le): return pivotPos
        i -= 1
        if A[i] > pivotValue:  
          swap(A, pivotPos-1, i)
          swap(A, pivotPos-1, pivotPos) 
          pivotPos -= 1
def partitionUsingFarLeft(A, le, ri): 
    # optional: swap a random object to the far left
    swap(A, random.randint(le, ri), le)
    ret = i = le
    while True:
        if i == ri: return ret
        i +=1 #move pointer
        if A[i] < benchmark: # 3-step adjustment
            swap(A, ret+1, i)
            swap(A, ret+1, ret)
            ret +=1
def partition(A, le, ri):
    return partitionUsingFarLeft(A, le,ri)
def recurse(A, le, ri): 
    if le>=ri: return
    print 'entering partition()...',
    print(A[le:ri+1]), ' pivotVal =', A[ri]
    anchor = partition(A, le, ri)
    print '...after partition()   ',
    recurse(A, le, anchor-1)
    recurse(A, anchor+1, ri)
def swap(A, x,y):
    tmp = A[x]
    A[x] = A[y]
    A[y] = tmp
def qsort(A):
    recurse(A, 0, len(A)-1)

Above is py, below is c++

#include <iostream>
#include <vector>

std::vector<int> A{77, 11, 66,22,33,99,44,88, 77, 55, 0};
int const size = A.size();

void dump(int l=0, int r=size-1) {
	for (int i = l; i <= r; ++i)
		std::cout << A[i] << ' ';
	std::cout <<std::endl;

template <typename T>
void swap(int pos1, int pos2) {
	if (A[pos1] == A[pos2]) return;
	T tmp = A[pos1];
	A[pos1] = A[pos2];
	A[pos2] = tmp;

/*partition the region [l,r] such that all elements smaller than
pivotValue are on the left of pivotPosition
template <typename T>
int partitionUsing1st(int l, int r) {
	T const pivotVal = A[l];
	int pivotPos = l;
	for (int i = l+ 1; i <= r; ++i) { 
		if (A[i] >= pivotVal) continue;
		swap<int>(pivotPos + 1, i);
		swap<int>(pivotPos + 1, pivotPos);
	return pivotPos;
template <typename T>
int partitionUsingLast(int l, int r) {
	T const pivotVal = A[r];
	int pivotPos = r;
	for (int i = r - 1; i >= l; --i) {
		if (A[i] <= pivotVal) continue;
		swap<int>(pivotPos - 1, i);
		swap<int>(pivotPos - 1, pivotPos);
	return pivotPos;
/*based on [[Algorithms unlocked]], should but doesn't minimize swaps!
Lime zone -- items smaller than pivot value
Red zone -- items bigger than pivot value
Ugly zone -- items yet to be checked
template <typename T>
int partitionMinimalSwap(int le, int ri) {
	T const pivotVal = A[ri];
	// two boundaries exist between zones
	int redStart = le;
	// we start with redStart == uglyStart == l, which means item at le is Unknown
	for (int uglyStart = le; uglyStart < ri; ++uglyStart) {
		if (A[uglyStart] < pivotVal) {
			swap<int>(uglyStart, redStart);
	swap<int>(ri, redStart);
	return redStart;

template <typename T>
void recurse(int l, int r) {
	if (l >= r) return; // recursion exit condition
	int const anchor = partitionMinimalSwap<T>(l, r);
	recurse<T>(l, anchor-1);
	recurse<T>(anchor+1, r);

int main() {
	recurse<int>(0, size-1);
	return 0;

threading – I/O^CPU intensive

See also — [[linux sys programming]] has an one-pager on this topic.
This is a common topic in IV and in the literature. HSBC market data + algo trading IV 2017 also touched on this.
IO-intensive – may scale up to hundreds of threads, even with just 4 cores. Each thread handles some I/O channel or connection.
eg(?): network server
eg: GUI – multiple threads could be waiting for disk or user input
CPU-intensive on multi-core machines – don’t need too many threads, but single-thread is non-optimal. Each core is effectively dedicated to a single thread.

java local var( !! fields)need explicit initialization

http://stackoverflow.com/questions/268814/uninitialized-variables-and-members-in-java briefly mentions the reason.

Instance variables (i.e. fields) of object type default to being initialized to null. Local variables of object type are not initialized by default and it’s a compile time error to access an undefined variable.

For primitives, story is similar

West Coast^Wall St techies #per HenryWu

Henry Wu felt

* west coast techies have clearly higher calibre.

* Wall St is more busy. I guess he means higher workload, faster pace, more quick-n-dirty and lower quality.

* Very few older guys on the west coast. There’s probably implicit age discrimination that’s hard to prove.

* Perm roles pay higher on the West coast. However, his sample may be different from David Leak’s

c++ compiler to print __cplusplus

Based on http://stackoverflow.com/questions/1562074/how-do-i-show-the-value-of-a-define-at-compile-time:

#define VALUE(x) #x
#define VAR_NAME_VALUE(var) #var “=” VALUE(var)
#pragma message(VAR_NAME_VALUE(__cplusplus))
—-save above in dummy.cpp—-

g++ -std=c++14 dummy.cpp # shows:
dummy.cpp:7:44: note: #pragma message: __cplusplus=201300L

search in q[less]

  • In a q(less) screen, You can highlight not only the same AAA occurrences, but AAA or BBB. I used


  • [2/3] /ctrl-R — search without metacharacters
  • -i, then /needle — case insensitive

[3/4] means vi/less receives 3 keystrokes; we hit 4 keys including shift or ctrl …

–search can fail with super-long lines. See also http://www.greenwoodsoftware.com/less/bugs.html

  • Symptom — if you navigate to the correct region of the big text file, then the search does hit the target word.
  • Solution — use grep to confirm


Wall St cod`IV don’t care about efficiency

Most challenging algo interviews (onsite) are really about “solve it by hook or by crook” regardless of efficiency. (Usually there isn't, but if there's an obvious brute-force solution it gives you no credit anyway.)

In the toughest algo questions, usually there's no memory capacity issue to worry about.
Occasionally, interviewer stipulates O(N logN) or O(1)
Once we solve it we can improve efficiency. It's a second phase. Can be easier than first phase.
In that case threading, sorting … are unimportant. Instead we need judicious use of trees, recursions, pattern recognition… and very clear thinking.
See [[EPI300]] and the green book by Zhou Xinfeng

## c++topics seldom quizzed

(master -> pearl)
some low-level details I thought would be popular but seldom asked:
* L-value
* iterator types and implementations
* static variables outside classes
* implicit acts of magic by compiler
* array and cStr – syntax, memory, … the gory details beyond the basics
* template specialization
* ref/pointer typedef inside  templates
* non-dummy-type args in template
* MI
* enum
* exception spec
* C integration
* pimp
* fwd declaration
* namespace
* linker
* extern
* double pointers
* hiding rule
* swap – all the important usages and no-fail
* overloading and  method resolution
* casting and conversion
*** OOC and  conversion ctor
— “mid-level”
* boost Any vs Variant
* i/o stream
* regex
* file access (including random)
* serialization
* memory leak detection
* details of boost thread
* boost smart pointer beyond the shared_ptr
* std::string details

struct packing^memory alignment %%experiment

Sound byte — packing is for structs; alignment and padding is Not only for structs

Longer version — alignment and padding apply to any object placement, whether that object is a field of a struct or an independent object on stack. In contrast, the packing technique is specific to a struct having multiple fields.

https://github.com/tiger40490/repo1/blob/cpp1/cpp1/memAlign.cpp has my test code to reveal the rules:

  1. For a (8-byte) long long object on stack (or in a struct), the address is 8-byte aligned. So padding is added by compiler, unless you say “packed” on a struct.
  2. For a (4-byte) int object on stack (or in a struct), the address is 4-byte aligned.
  3. For a (2-byte) short object on stack (or in a struct), the address is 2-byte aligned.
  4. For a char object on stack (or in a struct), the address is 1-byte aligned. All memory address are 1-byte aligned, so compiler adds no padding.

http://stackoverflow.com/questions/11108328/double-alignment Wug’s answer echoes Ashish’s comment that tiny fields like char should be grouped together, due to Rule #4. This applies to stack layout as well [1]. However, compiler optimization can be subversive:

Not only can the compiler reorder the stack layout of the local variables, it can assign them to registers, assign them to live sometimes in registers and sometimes on the stack, it can assign two locals to the same slot in memory (if their live ranges do not overlap) and it can even completely eliminate variables.

[1] See https://stackoverflow.com/questions/1061818/stack-allocation-padding-and-alignment

2 simple yet concurrent singleton implementations ] c++

Both designs are thread-safe.

  1. First design is the static factory method + a static data member initialization.
  2. Second design uses a local static object, based on [[eff C++]] P 222.
template<class T>
class StaticFieldSingleton {
 static StaticFieldSingleton<T> instance_; //declaration of static field
 StaticFieldSingleton() {
  cout << "StaticFieldSingleton() ctor\n";
 StaticFieldSingleton(StaticFieldSingleton<T> const &);
 StaticFieldSingleton<T>& operator=(StaticFieldSingleton<T> const &);
 static StaticFieldSingleton<T>& getInstance() {
  return instance_;
//separate definition required on any static field
template<class T> StaticFieldSingleton<t> StaticFieldSingleton<t>
::instance_; //<---def of static field: required complexity

///////////// 2nd design uses a static local object
class SimpleSingleton {
 SimpleSingleton() {
  cout << "SimpleSingleton()\n";
 SimpleSingleton(SimpleSingleton const &);
 SimpleSingleton& operator=(SimpleSingleton const &);
 static SimpleSingleton& get_instance() {
  static SimpleSingleton instance;//<----- cleaner syntax
  return instance;
int main() {

 SimpleSingleton& ins1 = SimpleSingleton::get_instance();
 SimpleSingleton& ins2 = SimpleSingleton::get_instance();
 cout << &ins1 << endl;
 cout << &ins2 << endl;


python dict bad lookup key: solutions

A common scenario. myDict[‘baaadKey’] throws exception

Solution: defaultdict class

Solution: mydict.get(myKey, myDefault).
** C# supports the same.
** Java lookup method won’t throw. See
http://stackoverflow.com/questions/3626752/key-existence-check-in-hashmap ** STL map (red-black-tree) can silently insert a pair in this context

Solution: mydict.setdefault(myKey, myDefault) of the regular dict class. Note this solution is similar to the get() solution and it does NOT set a single default for the entire dict.

## X years C++xp +! using virtual

  • It’s possible to code C for years without using pointers (except string functions), or malloc
  • It’s possible to code C++ for years without using virtual, new/delete, STL, or even class.
  • It’s possible to code java/c# for years without any threading, or reflection
  • It’s possible to code perl for years without using interesting regex or hard/soft references.
  • It’s possible to code SQL for years without writing outer joins. I guess I wrote lots of mysql queries without any join.
  • It’s possible to code python for years without creating python classes.
  • eg quartz — the python experience didn’t make a strong python developer. The other tech knowledge gained has even lower market value.

RESTful – phrase book

agile – …

coupling – less coupling between client and server, so server side changes are easier. I think SOAP requires client rebuild.

b2b – still dominated by SOAP

resource-oriented services – …

object – each URL logically represents an object, which you can Get (query), POST (create), PUT (update its content) or DELETE

Q: is REST simpler than traditional SOA or SOAP web services?

Q: the concept of “resource” — does it sometimes mean a database record?

CAPM beta – phrasebook


  • regression — beta is named in the context of a regression against the market factor
  • cov/var — beta is defined mathematically as this ratio
  • excess return — in the regression, both the explanatory variable and the dependent variable are excess returns.
  • portfolio — (due to regression) a portfolio beta can be computed from weighted average

above 1 — means the regression slope is steeper than the “market”
equals 1 — is the market itself or any “normal” security
below 1 — means the regression slope is more gentle than the “market”

[12]back-scan any container,print`every Other item #MS

Have I overspent my time on this once-asked question?

The umbrella question — write a utility function to iterate any container and print out every Other element backwards?

Good coding practice! I think this is all about iterator syntax knowledge (my weakness) not algorithm (my strength)!

Note this is really about knowledge not  coding abilities. QQ not ZZ.

Iterator declaration is a can of worm 😦 I might need to give up on this.

#include <iostream>
#include <vector>
#include 	<list>
#include <set>
using namespace std;

template<class _InIt>  void printAlternateItem2itr(_InIt _First, _InIt _Last){
	bool flag = true;
	// if the iterator is from rbegin, then ++ would reverse it!
	for (_InIt it = _First; it != _Last; ++it, flag=!flag) {
		if (flag) cout << *it << ' ';
	cout << endl;
template <typename CONT> void printAlternateItemBackward(CONT const & cont) {
	printAlternateItem2itr(cont.rbegin(), cont.rend());
int main() {
	//vector<int> cont = { 11,2,3,4,5,6,7,18 };
	//list<int> cont = { 11,2,3,4,5,6,7,18 };
	string cont = "0123456789a";
	set<int> cont2 = { 11,2,33,44,55,66,77,88,99 };
	int arr[] = { 11,2,3,4,5,6,7,18,9 };
	int size = sizeof(arr) / sizeof(arr[0]);
	printAlternateItem2itr(arr, arr + size); //forward only

Q: is comparison defined on all iterators?
A: now I think linked list doesn’t. Now I think only random access itr does.

%%Q: what’s the signature of STL find()? I will use those declarations of iterators in my function. (Actually the map and set containers have member functions find() outperforming std::find)

%%Q: from a const container, can u get a non-const iterator?

Q: why don’t you take a container as input? Why must you take iterators?
%%A: it’s more common to take iterator, but in this case container will do. All containers provide rbegin() or begin() including string. Raw array doesn’t but the iterator increment won’t work for raw arrays anyway.

Separate question
Q: OO design — how would you represent Order state transition graph in an OMS?

fwd contract often has negative value, briefly

An option “paper” is a right but not an obligation, so its holder has no obligation, so this paper is always worth a non-negative value.

if the option holder forgets it, she could get automatically exercised or receive the cash-settlement income. No one would go after her.

In contrast, an obligation requires you to fulfill your duty.

A fwd contract to buy some asset (say oil) is an obligation, so the pre-maturity value can be negative or positive. Example – a contract to “buy oil at $3333” but now the price is below $50. Who wants this obligation? This paper is a liability not an asset, so its value is negative.

find every 3-subsets adding up to 9 #Ashish

Q: You have 9 poker cards, numbered 1 to 9. Given two integers SUM (for example 11) and COUNT (for example, 3), construct every combination of 3 cards, who add up to the target 11.

Within each combination, if we need to generate each permutation, it’s as simple as calling next_permutation() within an array (which is a combination).

You can only choose each card 0 or 1 time, i.e. no redraw.

I used to feel this was dynamic programming. Now I feel we have no choice but iterate over all combinations. We have an algo to generate ascending combinations. We can stored them in mini arrays, each one is ascending. We could use binary search in each mini-array.

https://github.com/tiger40490/repo1/blob/cpp1/cpp/array/sizeN-subset_TargetSum_Ashish.cpp is a very short recursive solution by my friend CSY.

//There are N distinct poker cards numbered 1 to N. Find all combinations
//of C cards such that each combo adds up to the same given Target
//Based on https://bintanvictor.wordpress.com/2017/11/08/generate-next_combo3-boys-out5-recursive/
//Note some range of the generated sequence is ascending, permitting
//binary search like lower_bound, but it's not easy to tweak the algo
//to skip ahead. There's no random access iterator here!
#include <iostream>
#include <sstream>
#include <deque>
#include <iomanip> //setw
#include <algorithm>  //sort
#include <assert.h>
//#define DEBUG
using namespace std;
size_t calls=0, combos=0;
size_t const C=3; //how many in each combination
size_t const Target=12;
deque<int> pool{1,3,4,5,8};
deque<int> prefix;

template<typename T> void dumpDeque(deque<T> const & p, string const & headline){
  cout<<"-- "<<headline<<" -- size = "<<p.size()<<endl;
  for(int i=0; i<p.size(); ++i) cout<<setw(5)<<p[i];
template<typename T> int showCombo(deque<T> const * p){
  ++ combos;
  size_t sum = 0;
  stringstream ss;
  for(int i=0; i<p->size(); ++i){
        sum+= (*p)[i];
  static string last;
  string combo=ss.str();
  cout<<"combo: "<<combo;
  if (sum == Target) cout<<" <- Hit!";
  assert(last <= combo && "should be ascending");
  last = combo;

template<typename T> int recurs(){
#ifdef DEBUG
  cout<<"-------------\nentering "; dumpDeque(prefix, "prefix"); dumpDeque(pool, "pool");
  if (prefix.size() == C) return showCombo(&prefix); //prefix alone is the combo
  if (pool.empty()) return 0;
  T poolhead = pool.front(); pool.pop_front();

  prefix.push_back(poolhead); //add poolhead to prefix

  //this 1st recursive function call starts a rather deep call stack and prints
  //all combinations that start with the given (new longer) prefix
  recurs<T>();//use the longer prefix and the shorter pool

  prefix.pop_back();//restore prefix
  pool.push_front(poolhead); //restore pool, needed by the 2nd call in the parent stack
#ifdef DEBUG
  cout<<"^^^^^^ restored before returning "; dumpDeque(prefix, "prefix"); dumpDeque(pool, "pool");

int main() {
  assert(C <= pool.size());
  sort(pool.begin(), pool.end());
  cout<<calls<<"  calls to the recursive function to generate "<<combos<<endl;

Microsoft COM – a few keywords

“COM library” vs “type library”???

I suspect most code (whether people mention COM or not) written before dotnet are probably technically COM code???

— dotnet without the dotnet context —

Client/server — A COM client is whatever code or object that gets a pointer to a COM server and uses its services. A COM server is any object that provides services to clients. In-process servers are implemented in a dynamic linked library (DLL), and out-of-process servers are implemented in an executable file (EXE). Out-of-process servers can reside either on the local computer or on a remote computer.

Registry — COM types are usually listed by GUIDs in the registry, though some COM types are RegFree

DLL — COM components are usually implemented in DLL files, and registration allows only a single version of a DLL. Dotnet classes also exist in DLL or EXE files.

MS-Office – For example COM allows Word documents to dynamically link to data in Excel spreadsheets
Bindings — COM interfaces have bindings in several languages, such as C, C++, Visual Basic

ActiveX – is part of COM

— Excel Addin —
All COM Add-ins must implement each of the five methods of this interface: OnConnection, OnStartupComplete, OnAddinsUpdate, OnBeginShutDown, and OnDisconnection.

joint prob – jargon, …

Relevance –
– I feel conditional prob is based on joint prob
– conditional expn is based on joint prob

I feel one *extensible* example would be poker cards with 2 colors, 4 shapes, 13 values (based on how many “points”).

Important – variance of X+Y. http://www.math.cornell.edu/~back/m171/var_sum.pdf is a simple proof in discrete case.

Somewhat important – X*Y

IKM c++ misc points

Q: is increment operator synthesized by the compiler?
Portable file paths? Relative path OK. For absolute paths, use boost::filesystem
default args must follow non-default args.
(obscure syntax) int *(*p)[5] = new int *[5][5];
(obscure syntax) redefinition error:
const int Mon = 1, Tue =2;
Pointer to private field? legal
Pointer to ctor? Illegal
Templatize operator<< then specialize it? legal
Causing ambiguity error —
class base{};
class der: public base{};
void SomeFunc(base& b){ };
void SomeFunc(base b){ };
void SomeFunc(der& b){ };
void SomeFunc(der b){ };

matlab cheat sheet

–to insert a column into a matrix…. create a new matrix with the new column and the existing matrix.
–format long g % to display more digits without “e”

multi-line comment

nan(N) % better error detection than
–Calling another .m script (not a function) — http://stackoverflow.com/questions/5226840/call-a-matlab-script-in-a-script

— print variable with tag
disp([‘x is equal to ‘,num2str(x),’.’])
fprintf(‘TS: row # %in’, foundInHeet);


    error(‘Every time stamp must match between EWJ/JPP time series’)

— execute a multi-line selection of code
select -> right-click -> evaluate selection
–locate nan’s in a large vector
–code folding
preferences -> editor/debugger

##c++(GTD+)learning-aids to upgrade 6→7

Which yardstick? First must be IV. 2nd would be common (not the obscure) GTD skills
For IV, need to analyze how I was beaten in past interviews. For GTD zbs, a few major home projects has significantly increased my proficiency.
  • see recoll -> c++Q&A.txt
  • [ZZ] try out debug tools in the gdb book? LG
  • [ZZ] experiment with sample code in [[fin instrument pricing using c++]]
  • [ZZ] proj: valgrind (linux) – get hands-on experience. Might take too much time to get it working
  • problem – test if a linked list has cycles
  • problem: all permutations of 0 or more of abcde
  • problem: write skeleton c++ code for ref counted string or auto_ptr
  • problem: test if a given container is in ascending order
  • [ZZ means … see https://bintanvictor.wordpress.com/2017/02/14/low-leverage-ctopics/]

python script to descend n edit files in-place

import re
import os
import sys
from os import walk
import xml.dom.minidom as md

pretty_print = lambda f: ‘n’.join([line for line in md.parse(open(f)).toprettyxml(indent=’ ‘*2).split(‘n’) if line.strip()])

dirName, baseName = os.path.split(sys.argv[1])
print sys.argv
print dirName
print baseName

for (path, dirs, files) in walk(sys.argv[1]) :
        for oFileName in files :
                print oFileName; #raw_input(“…”)
                if not re.search(“vol.*.xml$”,oFileName): continue
                fullpath = path+”\”+oFileName;
                except Exception as e:
                        print e; raw_input(“…”)                      

                totalsubs = 0
                for line in open (fullpath): #path+”\tmp.txt”) :
                        # $2 not supported!
                        # \b same as in perl
                        newStr, subsMade = re.subn(‘\b(Tenor=”.*?”)s+(Date=”.*?”)’ , “\2 \1”, line)
                        if (subsMade > 0):
                                print newStr, # comma to suppress n
                                totalsubs += subsMade
                print str(totalsubs) + ” total substitutions made — ” + fullpath

                tmpFile=open(path+”\tmp.txt”, ‘w’)
                tmpFile.write(pretty_print (fullpath))

[[automate the boring stuff with python]]

This book teaches just enough python features for the purpose. All

non-essentials are left out.

–sub chapter on selenium

the easiest way to use selenium and drive a browser, IMO.

–Chapter on Excel

text file spreadsheet converter


setting font in a cell

–Chapter on PDF:

combine select pages from many files


Chapter on CSV + json

Chapter on task scheduler

Chapter on keyboard/mouse control — powerful

quant developer requirements

Many quant developers (in our department) program in c# (for excel

plugin) or build infrastructure code modules around quant lib, but

they don't touch c++ quant business logic classes. C++ quant lib

(model) programming is reserved for the mathematicians, typically


Many of these non-C++ quant developers have good product knowledge and

can sometimes move into business side of trading.

I was told these quant developers don't need advanced math knowledge.

—-quant interviews

Mostly C++ questions. Most candidates are filtered out here.

2nd group – probability, (different from statistics)

Some finance intuitions (eg — each item in the BS formula)

Some brain teasers

— some typical C++ questions (everything can be found from the Scott

Meyers books)

exceptions during ctor/dtor

virtual ctor

Given a codebase, how do you detect memory leak

multiple inheritance (fairly common in practice)



[[heard on the street]] and [[A Practical Guide To Quantitative

Finance Interviews]]

Another book by Shreve.

##some benefits@learning c++, even if no salary increase

  1. After learning c++, i am fairly confident I could if I must pick up c# in a few (4?) months and start passing interviews. C++ is inherently tougher than java and C#. Java and C# both have large libraries, but the core languages are significantly simpler/cleaner than c++.
  2. After learning C++, i have found python and perl easier to understand and master since both are written in C/C++. I now believe some people who claim they could pick up a new language in a few months. Those languages have their roots in C/C++.
    • The basic challenges of scope+namespace, object lifetime, heap/stack, pointers, memory allocation, object construction, pass-by-ref/value, arrays, function pointer, exceptions, nested struct+array+pointer… are faced by every language designer. Many of these challenges depend on basic library, which is invariably C.
    • The common OO challenges of inheritance, virtual, static/non-static, HAS-A/IS-A, constructor, downcast, … are faced by every OO language designer. Many of them borrow from java, which borrows from C++ and smalltalk
  3. threading — java remains the gold standard but c++ currency support is more complex, harder to understand and offers some low-level insight
  4. memory management — c++ offers insight into JVM and CLR
  5. c++ gave me other insight into java, esp. GC, JVM, overriding, references, heap/stack, sizeof, …

a MSVS SOLUTION imt a folder of MSVS projects

label: IDE, book

I agree the project is a more important development concept, but the solution also has unique, important functionalities. I found many of these functionalities from [[MSVS 2010 unleashed]].

* project dependency is described in the sln file.

* you can build an entire solution, according to the predefined build order

* a solution can contain so-called non-project items such as documentation. All such files are by default put into the “Solution Items” virtual folder.

pointer in C +other lang-(early) learning notes

Every variable in any lang is a 3-some — name + addr + content (but see the post on mutability, initialize etc). http://aelinik.free.fr/c/ch11.htm has a good explanation:

int x;

x = 7;

the variable x now has two values:

Left value: 1000

Right value: 7

Here the left value, 1000, is the address of the memory location reserved for x. The right value, 7, is the content stored in the memory location.


Now a few simple rules on & and *

rule — &var1 returns address of var1.

rule — I don't think &var1 can be on the left side of =

rule — var1 must be a L-value expression. See http://bigblog.tanbin.com/2011/11/c-func-call-as-l-value-key-points.html

Now suppose we assign ptr1 = &var1

rule — *ptr1 returns the content of var1. If you print *ptr1, and print var1, you get same.

rule — *ptr1 can be on the left side of =

I believe *ptr1 == *(&var1) == var1

to a novice programmer — the importance of data structures in financial IT systems

Data structure is essential and non-trivial to every programming language on wall street — java (collections), c++ (STL), python/perl (high-level data types)… A lot of algorithms (in the traditional, strict sense of the word) are created on or using classic data structures. There are a lot of jargons you need to grasp. Everyday programming requires you to be familiar with common operations on these data structures, but these are relatively straightforward. However, here are some of the non-trivial aspects of data structures and


* sort python dict or tuple

* internals of hash table and ConcurrentHashMap

* STL container holding pointers

* STL pair

* STL stream iterators

* customize a sorted set/map for MyClass in C++, which is harder than java

* sort a map by value (real interview question) in java. Perl and python is easy.

* whiteboard-implement a blocking stack/queue (UBS)

* whiteboard-implement a basic hash table (MS)

* find top 99 in a large array (Barc) but no need to return them sorted

* choose array vs circular array vs linked list, in terms of memory efficiency (JPM).

* given 2 nodes in a binary tree, with no pointers except pointer-to-parent, find lowest common ancestor (Google)

* iterative tree walk (Barc)

* filtering iterator (MS)

* STL transform(), erase(), partition() etc

* STL allocators

* STL functors, binders

* STL iterator adaptors (back_inserter etc)

Posted By familyman to learning finance,c++,py… <http://bigblog.tanbin.com/2011/05/to-novice-programmer-importance-of-data.html> at 5/03/2011 07:34:00 PM

weblogic server910_generic.jar

Q: where to find this jar?

A: according to google: go to bea download site, select “solaris 10 x86” and u will get server910_generic.jar

Q: Working on solaris on sparc hardware?

A: yes. Tested many times with T2000 servers.

Q: The jar can work on win32?

A: jdk150_08 will break weblogic910. Confirmed: jdk150_04 supports weblogic insalled from weblogic910_generic.jar

Posted By familyman to learning finance,c++,py… <http://bigblog.tanbin.com/2006/09/weblogic-server910genericjar.html> at 9/23/2006 12:53:00 AM

hard edit vs soft edit when submitting order to mainframe

In one order entry system (A big European megabank), new orders are sent to mainframe. Upon validation, mainframe can return a message to the order entry system.

If the message is a hard edit, the order is rejected by mainframe validation module.

If  the message is a soft edit, the order is accepted by mainframe validation module. The soft edit is purely informational. Not necessarily a warning. No action is required on the user of the order entry system. I guess the soft edit is just “FYI”.

Posted By familyman to learning finance,c++,py… at 3/15/2011 11:52:00 PM

cobol copybook = input format spec

a cobol-copybook is “a file describing an input data format”.

“cobol copybook” is the standard term (“cobol-layout” is less common) for files like that mentioned in https://ssl.kundenserver.de/shop.softproject.de/downloads/CobolCopybookToolkitForJava.pdf


— based on http://edocs.bea.com/JAM/v51/program/egenapp.html
A COBOL CICS or IMS mainframe application typically uses a copybook source file to define its data layout. This file is specified in a COPY directive within the LINKAGE SECTION of the source program for a CICS application, or in the WORKING-STORAGE SECTION of an IMS program. If the CICS or IMS application does not use a copybook file, you will have to create one from the data definition contained in the program source.

A copybook is conceptually (not technically) part of a cobol program. Usually this copybook is a standalone file, included from its parent-program.

Posted By familyman to learning finance,c++,py… at 1/20/2008 08:53:00 PM

Gaussian HJM, briefly

… is a subset of HJM models.

An HJM model is Gaussian HJM if vol term is deterministic. Note “vol” term means the coefficient of the dW term. Every Brownian motion must always refer to an implicit measure. In this case, the RN measure.

How about the drift term i.e. the “dt” coefficient? It too has to be deterministic to give us a Gaussian HJM.

Well, Under the RN measure, the drift process is determined completely by the vol process. Both evolve with time, but are considered slow-moving [1] relative to the extremely fast-moving Brownian Motion of “dW”. Extremely because there’s no time-derivative of a BM

[1] I would say “quasi constant”

Language is not yet precise so not ready to publish on recrec…

fwd contract arbitrage concept – less useful

label – fwd deal

The basic relationship (between spot price, fwd contract price, T-maturity bond price..) is intuitive, low-math, quite accessible to the layman, so I decided to really understand it, but failed repeatedly. Now I feel it’s not worth any further effort. It’s not quitting. It’s saving effort.

– interviewers won’t ask this
– real projects won’t deal with it, because the (arbitrage-enforced) precision mathematics simply doesn’t manifest in the real data, perhaps due to bid/ask spread
– Only financial math literature makes extensive use of it

I think this is like the trigonometry or the integration techniques — you seldom need them outside academics.

arb-free IR model

… must model the (evolution of) entire YC, rather than some points on it, like (the evolution of) one Libor rate. This is a main theme of the lectures on Black’s model, forward measure, HJM etc.


For more details, See the post on HJM

c++string tasks: IV+GTD

(Let’s be imprecise here… Don’t sweat the small stuff.)

We should be able to perform all of these using c-string, std::string (limited adoption since c++98), the standard string in java , c#, perl, python, php. This is a master list. Tolerate multiple names on Each task.

See basic tasks on https://bintanvictor.wordpress.com/2018/01/26/22tasksarraystrdictq-algoiv/

use string iterator with STL algorithms

–the easy

convert to vector and apply vector tricks
convert to std::string and apply tricks
count how many times a substr occurs
sort content

3c++London bbg algo IV #all about array

Q: given a string of characters, find the longest “run” of any repeating character.

Q: given an array of integers, and a target, determine if there’s any pair that add up to the target. Return true/false.

Q: maximum profit problem. Given a time series of historical spot FX rates, find the maximum possible trading profit by exactly one buy one sell.
A: I have seen this many times. First write pseudo-code.


Q: a vector needs to allocate N instances of Account but Account has no default ctor, but it just works. How does the compiler achieve it? (Actually it won’t compile if compiler can’t synthesize the no-arg ctor)
A: indeed the call to array new would prevent the vector concrete class from compiling due to SFINAE rule or something like that. (Java and c# would use type constraints instead.) The compiler must be using 2 separate lines – one to allocate raw memory, the other to initialize the object.

However, when is vector internal array allocated by array new? I discussed with Ashish but found no answer.

Q: why did you say you needed to write your own smart ptr?
A: super simple one, just to deal with some issue of the raw ptr… probably dangle pointer, by overriding the deref operator

Q: why would anyone use unique ptr when shared ptr is general purpose
A: threading…
A: some pointee objects are designed with sole-ownership

Q: is the lambda functionality doable in c++98? what is the c++98 equivalent?
A: some functor with a challenging syntax I can’t remember.

Q: OK you don’t use c++11 at work, but do you hack around at home?

Q: why is  the bid/ask spread much wider in options than the underlier?
A: must be the risk to the writer. Competition didn’t drive down the bid/ask spread like it did in FX and cash equities.
AA: delta hedge adjustment can’t be done every second. Before the next adjust, the risk to the writer (market maker and quoter) would be quite high.

[[21st century c]] – unusually practical update on C

a sub-chapter on string processing in the new world
a sub-chapter on robust Macros in the new world
a sub-chapter on function to report errors in the new world
a full chapter on pointer in the new world
a full chapter on C api to be consumed by other languages like python
a full chapter on struct syntax improvement to support returning multiple values + status code
a sub-chapter on pthreads
a sub-chapter on [[numerical recipes in C]] and the implementation – the GNU scientific library
a sub-chapter on SQLite
briefly on valgrind
function returning 2 values + status code
many innovative macro tricks
innovative and concise explanation of auto(i.e. stack) vs static vs malloc memory

Note a sub-chapter is very short, in a concise book. A remarkably practical update on C, somewhat similar to [[safe c++]]. Content isn’t theoretical, and not so relevant to interviews, but relevant to real projects and GTD

vector initializer: disallowed in field declaration until c++11

in a field declaration, you face restrictions:

vector<int> vec; //acceptable
vector<int> vec(9); // field initialization, disallowed until C++11. See https://stackoverflow.com/questions/24149924/has-the-new-c11-member-initialization-feature-at-declaration-made-initializati

You can write this kinda code in a *local* variable definition though.

swaps illustration diagrams — how to read

This write-up covers IRS, x-ccy swap…

These block diagrams are popular and partially useful, but beginners often don't realize:

* initial context — typically a corporation has a periodic liability, or an investor has a periodic income.

** We had better ignore all the other arrows first.

* the motivation — typically to convert the initial single arrow to other arrows. The swap contract adds 2 arrows, one of them cancelling out the pre-existing arrow.

** we had better focus on the 3 arrows and ignore other parts of the diagram.

yield curve , according to Jeff

Jeff’s lecture notes (in 0xpdf) has detailed explanations on

1) EUR OIS YC bootstrapping using specific OIS instruments
2) Libor YC under OIS discounting — so OIS curve + libor curve needed.
3) Libor curve for a non-default tenor, such as 6M or 2M

lots of “root-finding”… but not too hard.

A YC (or a term structure) can be represented as a series of
* spot disc factors
* fwd disc factors
* spot interest rates
* fwd interest rates

Rebanato – good author on fixed income models

recommended by Sian Hwee.

Ronnie said Black model is popular (largely due to simplicity, and historical reason), and many option products are quoted in terms of vols implied from the Black model. 
TA seems to agree that the advanced models (beyond the Black model) are still needed but indeed harder than the earlier lectures before the Black model.

buying (i.e. long) a given interest rate

Tony (FX lecturer) pointed out “buying” any variable means executing at the current “level” and hope the “level” moves up. (Note a mathematician would point out an interest rate is not directly tradeable, but never mind.)

Therefore, buying an interest rate means borrowing (not lending) at a rock bottom rate.
Wrong intuition — “locking in the interest income stream”.
Eg: Say gov bond interest is super low, we would borrow now, and hope for a rise.
Eg: Say swap rate is super low, we would lock it in — pay fixed and lock in the floating income stream, and hope for the swap rate and floating stream both to rise.

enough c# mileage accummulated@@

Your mileage will show in IV and in projects, but IV is way, way more important than real project performance — You only need to be barely competent to get your job done, solving common problems at your pace (though at Baml my own pace was slower than other team members). No need to be competent to solve all tricky problems, some are too tough for everyone. But you better ace the IV.

– – I still feel less familiar with some everyday tasks than my c# veteran colleagues.
– – i did rather few complete projects like Guardian and many enhancement projects. In contrast, veteran c# guys did more big projects. The number of years isn’t as important as complexity and variety of challenges. VARIETY — for eg a web dev expert (or a DB-centric c# coder) isn’t really a complete c# expert.

– – wpf is a specialized skillset, like swing, or web dev or sockets. I didn’t get a lot of wpf hands-on mileage yet.
– – wcf is another specialized skillset. I lack mileage.
– – assembly, appdomain, GAC

= = people agree that I can get my job done and meet most technical challenges, even if non-trivial. (“tactical”)
= = explaining, justifying my design choices – not always convincing. Not only c#, but java too.

= = excel integration is one of the most valuable among the specialized c# skills. I have some experience.

+ + When presented a new c# challenge, when pushed to the frontier of my c# know-how, I am competent to construct a basic “infrastructure” and then search on-line for the missing pieces to complete the loop meeting the challenge

+ + competent with most everyday troubleshooting.  Some troubleshooting is beyond my current skill but I feel many are hard for my veteran colleagues too. If my colleagues can solve 50% of the tech problems, then my level is close to that. This competence comes from mileage i.e. real world projects. The more projects one takes on, the more competent.

+ + like Venkat, I did practice with some hot c# features like threading, closure, remote debugging …

+ + much more confident with MSVS than before. Perhaps comparable to my Eclipse confidence.
+ + on some of the hot but purely theoretical IV topics (fake mileage), I have read up quite a bit and appreciate their nuances. I can follow most discussions on these topics –
*GC, dtor, finalizer
*value/reference types
*dynamic type

Morris in-order walk]O(N) #O(1)space

I feel this is too hard and unlikely to come up in coding interviews. Also, most trees have up-links, so the no-uplink constraint is artificial and unrealistic. In reality, Morris complexity is usually unnecessary.

–threaded binary tree is the basis of Morris


The Morris algo need to construct the threaded BST, walk and remove the extra links, all without recursion.

  • Tip: For my ascending walk, I only add right-up thread link to bring me to my ancestors. I don’t need any leftward thread link.
  • Tip: All the thread links point upward
  • How many right-up links? Let’s define two clubs Honey and White.
    • every node HH having a left child will get a incoming right-up link pointing to it! After printing HH’s left subtree, this link is used to revisit HH.
    • Except the very last node, every node WW without a right child needs to add a right-up link, so we don’t get stuck at WW.
    • If the Honey club has H members, and White club has W members, then I think we create H right-up links. W +1 = H
    • A node can be both Honey and White i.e. it has a right-up link and is also target of a right-up link.
  • Tip: the sequence of Honey nodes to try has to be top-down. We must create the uplink to every higher Honey node first, as insurance that we can always come back.  While a higher Honey node is locked down and we search for its predecessor in its left subtree, we ignore any lower Honey node
    • First time we hit a Honey node, I’m sure it is not uplinked. After we get it uplinked, we must descend Left.
    • 2nd time we hit a Honey node, I believe its left subtree is completely fixed, so if we descend left again, we will go to a dead end. We must move right, either down or up.
  • Tip: should really use a few variables instead of “cur”. This “cur” is like a “temp1” variable used in first pass, then temp2 variable in 2nd pass etc. These variables are not related at all.

https://github.com/tiger40490/repo1/blob/cpp1/cpp1/binTree/MorrisInOrder.cpp is my c++ implementation with a concise/cryptic implementation and an “unfolded” elaborate implementation

c++IV Art@Click #Jens

Q: buffer overflow?
%%A: avoid arrays, use vector
Q: XOR usage?
AA: usages? easy to google
Q: why bitwise shift?
%%A: mostly an optimization as far as I know, but the compiler probably translates integer multiply/divide already.
AA: usages? easy to google
Q: what’s wrong with pointers?
Q: dangling pointer?
Q3: what are the common exceptions in c++?
%%A: c++ has a few standard exceptions and a lot of UB; java has lots of standard exceptions and no UB. q(new) and dynamic_cast…
Q3b: undefined behavior?
%%A: much worse than exceptions or error codes
%%A: perhaps fairly consistent on one platform, but I know writing beyond an array’s limit is indeterminate. See [[c++ debugging)]
Q: exceptions – why do you not want to use it in your API?
%%A: can of worm. If I throw I can’t control how clients use this API. What if it’s thrown in a dtor? What if they don’t catch by reference? What if they catch a sliced one or a copy rather than the original exception object I want them to get? What if they catch by pointer and try to delete or forget to delete? Java cleaned it up.
%%A: I don’t see a lot of well-regarded API’s exposing exceptions
%%A: there’s performance cost
A: the best practice has always frowned on exception specifications. C++11 favors “noexcept”
A: now I think we should be consistent throughout – either throw exceptions consistently or never.
Q: memory leak – what is it and how do you deal with it?
%%A: valgrind replaces malloc with …?
%%A: provide class-specific op-new (and delete), which is safer (see effC++) than a customized global op-new. Add your own house keeping code therein…
Q: how is semaphore different from a mutex
%%A: I think a mutex is more basic and usually provided by the kernel (For a userland thread the thread library not the kernel must provide the mutex). I guess the counting semaphore is implemented using mutex + condition variables, since the semephore may need to inform the waiting threads.
Q: preprocessors?
%%A: 3 usages in my projects – #includes, macros and conditional compile. Now I think template meta programming also uses a lot of macros.
Q: stack trace?
%%A: very useful, that’s why java, c#, python, perl provide it, and GDB too.
A: [[safe c++]] shows simple and robust technique to build a stack trace upon assertion failure
I said many times “I’m philosophical about that” – meaning “it’s controversial IMO and I have my views which may look naive or extreme or eccentric”

[[java performance]] by Scott Oaks

–[[java performance]] by Scott Oaks


best of breed..see chapter details on

[jvm] heap memory

[jvm] threading

[jvm] instrumentation



lambda, stream  (java 8 interviews!)


The Introduction chapter outlines 3 broad aspects

* JVM – like memory tuning

* java language – like threading, collections

* Java API — like xml parser, JDBC, serialization, Json


JVM tuning is done by “system engineers” who may not be developers.


C++bbg Standard IV Questions { Zack

1. How is keyword virtual used in C++?

2. Does better O() algorithm always works faster in real cases?

3. Between pre-increment (++i) and post-increment (i++) operator, which one is more efficient in both built-in and overloading case? …….. [[more eff c++]] item 6

4. Please compare array and linked-list in terms of insertion and access

5. Please name some sorting algorithms and their time complexity

6. Can you tell me the difference between stack and heap?

7. What Linux command do I use the find a file in a directory recursively?

8. what is the virtual functions and destructors

9. what is the constructor destructor orders of a class then an inherited class

10. what is the most known sort algorithms , and what is the complexity of each

11. Smart Pointer, how does it work.

12. Templates, when use instead of inheritance…….. [[eff c++]] Item 41

13. Polymorphism, when use multi inheritance, problems that can happen, ……. [[eff c++]]

14. Sockets, monitoring sockets.

15. Multithreading, give a small example of Producer, Consumer.

16. Debugging issues, using GDB

19. TCP/IP and Multicast

20. STL and Boost libraries

par swap rate drop means …@@

For a given tenor (say 1Y)


I think treasury yield rise (or drop) has a simpler interpretation….


I think Libor ED deposit rate drop (or rise) has another simple interpretation …. and has a credit element.


Libor par swap rate drop has a non-trivial interpretation….


OIS swap rate is even more complicated…

dtor is simple thing.. really@@

update: [[safe c++]] has a concise argument that if ctor is allowed to throw exception, then it’s possible to have dtor skipped accidentally. Only an empty dtor can be safely skipped.

This is typical of c++ — destructor (dtor) is one of the most important features but quite tricky beneath the surface

* exception
* dtor sequence – DCBC
* virtual dtor
* synthesized dtor is usually no good if there’s any pointer field
* lots of undefined behaviors
* there are guidelines for dtor in a base class vs leaf class — never mindless
* objects put into containers need a reasonable dtor
* when the best practice is to leave the dtor to the compiler, you could look stupid by writing one, esp. in an interview.

* smart pointer classes is all about dtor

* RAII is all about dtor

* interplay with delete

*** placement new, array-delete vs delete

*** override operator delete

*** double-delete

*** ownership !

socket stats monitoring tools – on-line resources

This is a rare interview question, perhaps asked 1 or 2 times. I don’t want to overspend.

In ICE RTS, we use built-in statistics modules written in C++ to collect the throughput statistics.

If you don’t have source code to modify, I guess you need to rely on standard tools.

option math : thin -> thick -> thin

Many topics I may have to give up for lack of bandwidth. For the rest of the topics, let’s try to grow the “book” from thin to thick then to thin.



+ delta hedging

+ graphs of greeks

+ arbitrage constraints on prices of European calls, puts etc. Intuition to be developed

+ basic strategies like straddle                     


LG American options


LG binary options but ..

+ Roger’s summary on N(d1) and N(d2)

LG div

LG most of the stoch calc math but ..

LG vol surface models

+ some of the IV questions on martingale and BM


## some of my c# dotnet achievements

1) home-grown web server to download any file in the local file system – essentially my invention, as no one in the team know how to do it

GUI – full ownership

stability improvement in the microagent and Guardian server

Bloomberg piece – no one dared to touch it given the complexity

Excel add-in

Created many windows services

binary tree in-order walk #famed bbg Q

Is there a way to design an iterator over a binary search tree with the following properties?

1. Elements are visited in ascending order (i.e. an in-order traversal)
2. next() and hasNext() queries run in O(1) time.
3. Memory usage is O(1)

I feel O(1) memory is impossible since this is recursive (though all recursions can convert to iteration with a queue bigger than O(1)), with a stack data structure of size H := height of tree.

Similarly, hasNext() need to work through the stack of size H, so O(1) run time impossible

Morris may meet the requirement.

academic route as a long term career option#Sam UChicago

A letter never sent out…

Hi Sam

(This is more like a personal blog, to record my thoughts and conversations.)

I never considered those options you posed today

Q: 2nd master's degree?

A: I liked the part time study experience so far. Will take a 2nd Masters if someone pays for me.

Q: I did think about teaching at polytechnic level, but teach what subject?

A: Either IT or financial math, or perhaps data science — after I spoke to Bernie.

c++11 QnA IV 3arrow

Q: In a move constructor, is the parameter “x” an rvalue reference? is there another rvalue reference in the call?
%%A: x is a rvr, but as a variable is an l-value expression since it is named and has a Location.

Q: What’s an rvalue reference actually, like a std::string && rvr1
A: I feel it’s similar to a regular reference variable and often treated as a pointer. Since “pointer” has multiple meanings, I would not say that. I speculate that compiler treats rvr1 as an special alias to the original object. A special name plate on the memory location. Compiler knows to treat the object-behind as suitable-for-stealing.

Q: what’s lockfree? How did you make it work in your projects?
A: see my blog about atomic{int}

Q: What part of the boost thread library did you use?

Q: for-loop in c++11?
AA: work for C-style arrays, initializer lists, and any type that has begin() and end() functions defined for it that return iterators.

Q: Why did you implement your own smart pointer and wrapper over int?
A: to avoid uninitialized variables. See post on uninitialized ..

Q: Can ctor throw exception? Why do you say it’s not best practice?
A: now I think it’s not necessarily best practice. Exception is the only way constructors can signal failure.

Q: What kind of algo is qsort? Average and worst runtime complexity?
A: average nLog(n), worst case n^2

Q: Recursive vs iterative, which is faster?
A: comparable, but space complexity lower for iterative?

Q: How did you use parallel processing in GS?
A: data parallellism, threading, and other techniques. Coarse-grained is ideal.
A: i guess pipelining parallellism is also relevant, using task queues

Q: (rarely quizzed) Translation lookaside buffer

Q: mutable keyword’s usage? How about in c++11?
AA: closure – captured variables can be modified if “mutable”.

Q(seldom quizzed): noexcept
AA: both an operator and a function specifier…


c++advanced IV questions – contributed by real interviewers


These authors are real interviewers. They really do care about the topics I mentioned to you Monday night. They are seeking c++ specialists who understand the low level details. They don't care about the high level, architecture, design, picking the right framework or library etc.

Fwd: difficult phone screen with Credit Suisse

Hi Bin,
I just finished a frustrating interview with Credit Suisse.
It's just a phone screen, but many questions I cannot answer. When you have time, please add some comments to below questions.
And because I work from home for the interview, so I can record the whole call. It's shared to you in another email. Please give me some comments about how I behaved, just assume if you were the interviewer, what's your feeling about this candidate?
It's not that urgent, so just do it only when you can find time. I know you have a lot of work to do, so don't waste too much of you time.
1. describe one of your best coding challenges in 2 to 3 years, that can show off your coding abilities and your object oriented skills.
Rong: I mentioned NIO server I had hands on experience.
Interrupted by him, he said: the key thing I'm asking for is the specific coding that you personally did.
Rong: I should not mention I lead a team, because maybe that's the reason he suspect I describe other developers' work as mine. I feel for developer role, management experience is a downside factor.
2. Describe how did the single threaded application avoid blocking IO.
Rong: I cannot articulate how NIO works. I actually indeed created a NIO server. That's true, but I don't know how to explain well in phone screen, and I cannot recall some details such as the class, methods.
3. He continued to asked some details about NIO, and I cannot recall details.
4. Now he start normal quiz procedure. I only list difficult ones here, but I attached the full list FYI.
Would you want to implement a maximum queue depth?(I didn't know what's maximum queue depth mean)
What happens if your queue backsup?(I don't known what's that mean)
Would you want a maximum length? Why?
What happens if your producer hit the maximum length?
Do you know what's the completion services?
There is an alternative that you can implement producer/consumer without a queue?
What are the 3 different ways of creating a thread?(implement Runnable, extend thread, what's the third?)
Different between Runnable and Callable? (Callable return result, Callable throw Exception)
Any other differences or advantages for one over the other?
Let's say we have a process that receives request to do work. And each request has no dependency on each other.
And the request queue backs up. How would you solve this? 
(I don't know what's backs up mean. So I assume it means the queue is full. So requesters are blocked and processor will process one by one.)
When you want to use a timer of some kind, what are the different way you can do it in your code?
(I said there is a scheduler class?? not sure if there is)
How would you shut down the multi-threaded system? let's assume you write your own multi-threaded system.
(use countdownlatch?)
What if the thread is blocked?
(call thread.interrupt())
That's it?
What's the exception chaining? (I don't know)
Apart from try-catch block, how can you ensure all exceptions in thread pool are caught by our code?
What's meant by safe publication and the context thread safty shared object retrieval?
fail-fast vs fail-safe?
How would you use the future task? Which method would you use? How would you retrieve the result later?
Do you know the method, what's that called?
Difference between semophore and mutex?

Fwd: java threading questions {xr

Since vector is synchronized, is below code thread safe? If 2 threads are running below code, what will happen?
if(!vector.contains(element)) {
Rong answer: not thread safe, because another thread could add element in between vector.contains(element) and vector.add(element);
(my own question, not from interview, but just my own thought: What does “Vector is synchronized” mean?
my own answer: It means all methods that can update data are synchronized. So calling each single method is thread safe, 
but a transaction involving 2 synchronized methods is not thread safe. Only if a transaction is atomic, we can say it’s thread safe.
synchronize(obj) {
if 2 threads are running above code the same time, what will happen?
Rong answer: one thread acquire lock, then release lock, another thread acquire lock and then release lock, both wait forever.
follow up question: if a third thread call obj.notifyAll(), what will happen?
Rong answer: both waiting threads will be woken up, then one thread acquire lock and finish, then another thread acquire lock and finish.
synchronize(obj) {
what will happen if 2 threads are running above code?
Rong answer: both print “A” then wait; if notified by a third thread, then will sleep for a long time.

Fwd: vague question { xr – production troubleshoot

This is another vague question I couldn’t handle well.
He said that if the production application is hung, how do you find out what caused the problem?
Rong: I check CPU, if CPU is busy… (He interrupted: “suppose CPU is not busy.”)
Rong: I will check memory usage, if …(He interrupted: “suppose not memory used up.”)
Rong: I will check if there is I/O blocking …(He interrupted “suppose not because of I/O blocking.”)
Rong: That’s my way to analyse issue, I will rule out something to … 
(He interrupted “ok, let’s suppose it’s I/O issue, but it’s million line code application, 
and could be thousands of part involves I/O, how do you solve the problem?)
Rong: For this huge application, troubleshooting needs deep understanding of the codes.
(He said: suppose you know the code very well, and suppose you wrote the code yourself.)
I thought for a while and cannot answer. (I guess he has a specific answer, but I just cannot spot it.)
He: it’s ok, let’s move on to next question.
Do you have any idea about what he want? And this is also a Indian. I tend to conclude that either Indian think in a way different from mine, or he intended to mess up the interview, because all interviewers who throw me a vague question and refuse to give further hints are Indians.

Fwd: MS IV 20141211

Question 1:
void transfer(Account accA, Account accB, double amount) {
synch(accA) {
sync(accB) {
There is a deadlock issue, e.g Thread1 transfer from accA to accB, and Thread2 transfer from accB to accA. How to fix this bug?
Question 2: 
class Trade
OptionTrade extends Trade
BondTrade extends Trade
SwapTrade extends Trade
double getPV(OptionTrade trade)
double getPV(BondTrade trade)
double getPV(SwapTrade trade)
Trade trade = …//you got a trade instance
Pricer pricer = …//you got a pricer instance
//how to get price value of the trade
if(trade instanceOf OptionTrade) {
} else if(… instanceOf …) {
} else if (… instanceOf …){
He siad, this works, but looks he is not satisfied with this solution. He asked : “Is there a better way to do that?”
I cannot think of another way…, He followed with a question “what's polymorphism?”, so maybe the solution should be related to polymorphism.