Store static search data
How to store static search data in the application? I got such a question in an interview.
My first suggestion was a hash map. Second was storing the binary tree in an array where the root of the tree goes into 0 slots, 1,2 indexes occupied by root children etc. I quickly realized that the expected solution involves regularly sorted arrays with plain binary search. Which, as a general method, is very convenient given standard libraries in many languages.
I want to talk about my logic in more detail and how it differs from the traditional method.The layout I proposed is named Eytzinger (a.k.a. BFS) . It is very friendly for CPU cache and supports prefetching.
The tree:
will be placed in array as:
The access of children for any node ‘i’ would be:
Here is a sample function illustrating branchless binary search in Eytzinger layout, with CMOV instruction utilization.
The key improvement is that the left and right children are in addresses next to each other and on the same or adjusted cache lines.
The recursive construction of the layout from the sorted array:
Yes there are b-trees and s-trees, but those folks are for big data not HFT.
Eytzinger layout beats or is on par with the stored array solution due to CPU cache friendly data storage and no branching. It is extremely handy in instrument lookup during a busy pass in HFT trading.