##c++good habits like java q[final]

  • Good habit — internal linkage
    • ‘static’ keyword on file-level static variables
    • anonymous namespace
  • if (container.end() ==itrFromLower_bound) {…}
  • use typedef to improve readability of nested containers
  • typedef — “typedef int FibNum” as source documentation
  • initialize local variables upon declaration.
  • use ‘const/constexpr’ whenever it makes sense. The c++11 constexpr is even better.
  • [c++11] – use ‘override’ or ‘final’ keywords when overriding inherited virtual methods. Compiler will break if there’s nothing to override.
Advertisements

1st lonewolf]unsorted int array

Q: if an integer appears only once in an array, then it’s a lone wolf. Find the first lone wolf in a given array.

This problem is mostly about efficient data structures.

— one idea — O(N) one-pass. linked list + hash table. https://github.com/tiger40490/repo1/blob/cpp1/cpp1/array/1stLoneWolf.cpp is my tested implementation.

In a green linked list, each link node holds {index}. Every index “point to” a lone wolf.

Every lone wolf int or repeated int is also in a hashtable {int -> {color, iterator in linked list}}

For every node encountered, we add it to the hashtable. If it is a new int, it’s appended to the linked list; if it turns from green to brown, then we remove it from linked list.

— one idea — O(N) two-pass. linkedHashMap
* any “new” element will be appended to a LinkedHashMap and painted green
* any “seen” element will be repainted brown in the same container.
* How to test for newness? Any examined element is tested by contains().
* 2nd scan to return 1st green.

— One idea — hash table + heap in fwd scan

  • use a hash table to hold payload objects, and progressively mark each payload green or brown
  • each payload consists of {color, index}. The key in the hash table is arr[index]
  • As we create a payload (to put into hash table), we also save its address in a min-heap, sorted on the index
  • After the scan, extract minimum from the heap, until we get a green element.

 

java exception passing between threads

Many people ask how to make a child thread’s exception “bubble up” to the parent thread.

Background — A Runnable task is unsure how to handle its own exception. It wants to escalate to parent thread. Note parent has to block for the entire duration of the child thread (right after child’s start()), blocked either in wait() or some derivative of wait().

This question is not that trivial. Here are my solutions:

1) Callable task and Futures results — but is the original exception escalated? Yes. P197 [[java threads]]

2) Runnable’s run() method can temporarily catch the exception, save the object in a global variable such as a blocking queue, and notifyAll(). Parent thread could check the global variable after getting notified. Any thread can monitor the gloabal.

If you don’t have to escalate to parent thread, then

3) setUncaughtExceptionHandler() – I think the handler method is called in the same exception thread — single-threaded. In the handler, you can give the task to a thread pool, so the exception thread can exit, but I don’t know how useful.

4) adopt invokeAndWait() design — invokeAndWait() javadoc says “Note that if the Runnable.run() method throws an uncaught exception (on EDT) it’s caught and rethrown, as an InvocationTargetException, on the callers thread”

In c#, there are various constructs similar to Futures.get() — seems to be the standard solutions for capturing child thread exception.
* Task.Wait()
* Task.Result property
* EndInvoke()

IRS trading system@@ – Eric of Citi

IRS is not transferable. IRS contract can be re-assigned in some cases, but the original 2 counter parties and the new party must all agree.

Both parties must scrutinize the other’s credit worthiness. Libor rate is for top-credit borrowers. If the floating-payer is lower, then the spread on Libor (or the fixed rate?) will reflect that – a.k.a. credit spread. Alternatively, the counter party (floating receiver) can demand collateral.

There’s no secondary market for IRS like there are in listed securities.

Q: Is there an IRS trading system?
%%A: Most needed system might be a deal management system that tracks all our unexpired IRS contracts. Since each deal is bespoke, volume is not high. The basic entity in the system is known not as a position, but a deal. It’s treated like a trade as there are 2 accounts involved, and multiple settlement dates.

Q: Is IRS market regulated?
A: Regulators set limits on total exposure. Participant’s quarterly balance sheets include these IR swaps. One big swap could push a company above the limit.

cod`IV – favored by the smartest companies

XR,

I see a few categories of IV questions:

1a) fundamentals — Some Wall St (also in Asia) tough interviews like deep, low-level (not obscure) topics like threading, memory, vtbl, RB-tree ..

1b) lang — Some (IMO mediocre) interviewers (like programming language lawyers) focus on language details unrelated to 1a)
2) Some (IMO mediocre) interviewers like architecture or high level design questions (excluding algo designs) like OO rules and patterns but I feel these are not so tough.

3) algo — Some tough interviews focus on algo problem solving in pseudo-code. See http://bigblog.tanbin.com/2007/09/google-interviews-apply-comp-science.html. I got these at Google and Facebook. Some quants get these too.

4) compiler coding question — is another tough interview type, be it take-home, onsite or webex.
With some exceptions (like easy coding questions), each of these skills is somewhat “ivory tower” i.e. removed from everyday reality, often unneeded in commercial projects. However these skills (including coding) are heavily tested by the smartest employers, the most respected companies including Google, Facebook, Microsoft… You and I may feel like the little boy in “emperor’s new dress”, but these smart guys can’t all be wrong.
I will again predict that coding questions would grow more popular as the logistic cost is driven down progressively.
Candidate screening is tough, time consuming and, to some extent, hit-and-miss. With all of these screening strategies, the new hire still can fail on TECHNICAL ground. Perhaps she/he lacks some practical skills — Code reading; debugging using logs; automation scripts; tool knowledge (like version control, text search/replace tools, build tools, and many linux commands)
Needless to say, new hire more often fail on non-technical ground like communication and attitude — another topic altogether.

In terms of real difficulty, toughest comp science problems revolve around algorithm and data structures, often without optimal solutions. Algo interview questions are the mainstay at big tech firms, but not on Wall St. Some say “Practice 6 months”. Second toughest would be coding questions —
* Often there’s too little time given
* Sometimes interviewer didn’t like our style but onsite coding tends to focus on substance rather than style.

Tan bin

P.S. I had webex style coding interviews with 1) ICE Feb 2011, 2) Barclays (swing), 3) Thomson Reuters, 4) Bloomberg

P.S. I tend to have more success with algo interviews and onsite coding than take-home coding. See blog posts.. (
http://tigertanbin2.blogspot.com/2015/05/sticky-weakness-revealed-by-interviews-c.html
http://tigertanbinpripri.blogspot.com/2015/03/high-end-developer-interviews-tend-to.html
)

##criteria: domains2specialize over30Y

See also post on top 5 expertise I could teach.

In the US job market, people often ask “What do you specialize in?”. I think most non-managers in this industry, esp. the successful ones, do specialize in something. Whether you like it or not, you are often perceived that way.

Clearly, many professionals are jack of many trades (or a jack of few trades), and don’t have any real expertise, depth or insight. Depending on your view, this may not be a problem for them.

Like property evaluation, I have a list of criteria:

  1. theoretical complexity — so most peers can’t master it. I get lower stress. For example, Threading, statistics, pricing models, algorithms, data science? …
  2. market depth (entry barrier) — eg: quant is too hard to get into and very few jobs in the mid-range or low-end
  3. opportunities in research/teaching, for my later years. Relatively few choices.
  4. aptitude — (aka personal advantage) easier to add value and receive appreciation and recognition.
  5. Something I believe in or care about, such as personal investment, or health. Self-knowledge: If it has commercial value I will care about it.
  6. ———– Rest are secondary —————-
  7. pathway to self-employment
  8. (obvious) accumulation and low churn — Look at grandpa
  9. premium on job market — low priority in my later career
  10. Something related to early childhood education

Some specific domains (See the spreadsheet for more details):

  • concurrency in java/c++
  • raw market data processing in c++
  • risk mgmt + derivative valuation

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.

software dev is a 10x profession

In software projects, one lead developer can replace 10 mediocre developers. Many successful products/solutions/tools are the elegant creations of single alpha geeks.

Look at building architects. One key person to control and influence most of the key areas of a construction project, involving hundreds of workers.

Look at car designers, phone designers.

Now contrast restaurant managers. A good manager can’t replace 10 average employees.

Also contrast accountants. No 10x individuals.

Also contrast teachers.

Exception.getSuppressed()

In c++, dtor should never throw. During stack unwinding due to an escalating exception, every object constructed on the stack would go through their destructors (in the exact reverse order of construction). If one of the destructors throws a new exception, there’s a dilemma with the two exceptions both yet to be caught. C++ standards designate that situation as undefined behavior.

https://docs.oracle.com/javase/7/docs/api/java/lang/Throwable.html#addSuppressed(java.lang.Throwable) contrasts two situations

  • AA) one exception causing another. so the Throwable.initCause() reveals the original. This is supported for a long time
  • BB) two unrelated exceptions
    • BB1 try-with
    • BB2 bulk operations. Very rare.

https://stackoverflow.com/questions/8946661/jdk-1-7-throwable-addsuppressed-method has sample code to illustrate BB2 and BB1

According to [[java SE 8 for the really impatient]] P181, an exception thrown in a finally block would simply wipe out the original exception.

With the try-with-resources, the BB situation becomes more serious. Therefore, Suppressed exception is a general solution — The secondary exception is attached to the original, primary exception, as a suppressed exception.

Another, similar situation is a throw in a catch block like

try{…
}catch( Exception ex){
throw new IllegalArgumentException(); //ignore ex but ex object is not lost. It’s still included in stack trace
}

fastest muscle-growth=b4/af job changes]U.S.

I now recall that my muscle-building and, to a lesser extent, zbs growth are clearly fastest in the 3 months around each job change. I get frequent interviews and positive feedback. This is a key (subconscious) reason why I prefer contracting even at a lower salary. I get the kick each time I change job.

My blogging activity shows the growth…

  • #1 factor … positive feedback from real offers from good companies.
  • #2 factor — I actually feel real zbs growth thought it tends to be less strategic in hindsight.
  • factor — on a new job, I am curious to learn things I have wanted to learn like Xaml, FIX, Tibco, kdb, SecDB, multicast, orderbook, curve building

Beside the months immediately b4/af job change, I also experienced significant growth in

No such environment in Singapore:(

[17] j^c++^c# churn/stability…

This comparison has a bias for java. My observation is finance-centric.

 notes worst – best factor
Microsoft doesn’t care about this c# jav c++ protection of Your investment
c# jav c++ churn
c++ has no single owner making those decisions #worst to best c# jav c++ stability of features
#younger to older c# jav c++ longevity(new tech tends to die young
or changing too much)
JVM is huge help c++ c# jav ease of troubleshooting
c++ c# jav maintainability of app
c# can be back-end but less proven;
c++ can crash easily — no exception to catch
c# c++ jav underlying stability as
long-running server
windows is murky. Java: type erasure c++ c# jav  dark corners undocumented
#high to low c++ c# jav syntax complexity
#worst to cleanest c++ c# jav clean language
#low to high jav c++ c# expressiveness
c++ much higher #low to high c++ entry barrier at senior level
c# can be ez to learn, but java is even cleaner  #easy to hard java c# c++ initial learning
only Microsoft provides #low to high c# c++ jav std+3rdParty libraries
c# — only on windows c++ c# jav popularity
c# — limited to Windows and GUI/web c# c++ jav wide appeal, general-purpose
each lang has x% high-end jobs but c++
percentage (30%) is highest
c# salary
c++ c# java job market depth

probability puzzle for tech IV

I think it pays to invest in this domain.

Q: Why the hell do interviewers ask those (mostly discrete) probability questions?

I get these questions all the time. Similar to brain teasers and abstract aptitude test:

  • Well-defined, easy to understand
  • Completely unrelated to the tech job
  • somewhat crack-proof, so (as interviewers would think) more useful as an objective aptitude test

A: I can only guess it’s related to IQ. See post on Facebook regex interview

unix dirname() pitfalls

https://www.systutorials.com/qa/681/get-the-directory-path-and-file-name-from-absolute-path-linux pointed a few pitfalls:

  1. dirname() modifies the argument, so we need to duplicate the string in advance.
  2. If we omit the strdup, then … seg fault.
#include <iostream>
#include <string.h> //strdup()
#include <libgen.h> //dirname()
using namespace std;

int main(){
  char * path="/var/opt/aquis/working/security-20180108.csv.20180108";
  char * clone = strdup(path); //needed
  cout<<dirname(clone);
}

RMI class bytecode sync

Consider an object serialized and sent from hostA to hostB.

If the object is not a standard type like String or a collection, how can the receiving hostB reconstruct it? The class bytecode needs to be sent!

[[java the good part]], written by an RMI authority, gave explicit examples. The serialized stream includes metadata on the data object, which describes where to locate the corresponding class bytecode (of course on a bytecode server). On the receiving end, hostB would attempt to load the class bytecode locally. Failing that, hostB would download the bytecode.

Thanks to dynamic class loading, hostB can reconstruct an exact replica.

java interfaces have only abstract method@@ outdated]java8

Compared to c#, java language design was cleaner and simpler, at the cost of lower power and flexibility. C++ is the most flexible, powerful and complex among the trio.

There was never strong reason to disallow static methods in an interface, but presumably disallowed (till java 7) for the sake of simplicity — “Methods in interface is always abstract.” No ifs and buts about it.

With “default methods”, java 8 finally broke from tradition. Java 8 has to deal with Multiple Inheritance issue. See other blog posts.

In total, there are now 4 “deviations” from that simple rule, though some of them are widely considered irrelevant to the rule.

  1. a concrete nested class in an interface can have a concrete method, but it is not really a method _on_ that interface.
  2. Suppose an interface MyInterFace re-declares toString() method of Object.java. That method isn’t really abstract.
    • There’s very few reasons to do this.
  3. static methods
  4. default methods — the only real significant deviation from the rule

See https://stackoverflow.com/questions/27833168/difference-between-static-and-default-methods-in-interface

*morePure* SAM^functional interface]java

Most experts and authors on the internet don’t differentiate the 2. [[functional thinking]] says that a java 8 functional interface can have default methods in addition to a SAM (single abstract method)

Later, I will probably add a bit more details, but this is the key message.

Q: is a lambda related to SAM or FI (with default methods)? Official answer seems to be FI but can a FI-with-default-methods?

RMI could send more than simple VO

[[java the good parts]], written by a RMI authority, is a thin book with a concise but in-depth chapter on RMI. One of the things pointed out was —

In a distributed system like RPC or RMI, sometimes the object sent over network are more than monolithic POJO “value objects” with trivial methods. For example, you could send a special FXDeal object having specialized virtual reval() method. Interface would declare a BaseDeal data type for it. Obviously, receiving end should NOT “slice” this FXDeal object down to BaseDeal and lose the specialized reval().

Not sure how CORBA handles it, but RMI was designed to support it. Basically the serialization stream of the object includes the location to download the FXDeal class bytecode.

tech minds moving off management

Q: what’s your value-add in the global economy?

I have seen a bit of evidence that several ex-colleagues moved away from management to shift their career direction into hands-on value creation such as building new systems.

* German
* Avichal Gupta
* Yang (Xie)
* Eric at ICE RTS
* Anshul
* Benny Zhao

In the U.S. (Singapore?) the hands-on roles can pay on par with management. That’s good for me. *******************************************************
This message may contain confidential information and is intended for specific recipients unless explicitly noted otherwise. If you have reason to believe you are not an intended recipient of this message, please delete it and notify the sender. This message may not represent the opinion of Intercontinental Exchange, Inc. (ICE), its subsidiaries or affiliates, and does not constitute a contract or guarantee. Unencrypted electronic mail is not secure and the recipient of this message is expected to provide safeguards from viruses and pursue alternate means of communication where privacy or a binding message is desired. *******************************************************