Read this README, implement something similar and you automatically land a second stage interview. No need to thank me.
Implement an order book according to the spec.
A very good and not demanding (in terms of implementation) solution is to use two priority queues, one for sell orders and the other forbuy orders. In C++ this is easily done by using a set. Sets in C++ are ordered and internally implemented as red black trees. This givesyou very nice properties - insertion is O(logn), removal of an arbitrary element is O(logn). You will use insertion while implementing adding an order to the book, deletion while cancelling an active order. WHy not use a regular priority queue? That's why
Buy order queue is a max heap, storing the orders with the biggest price at the top, sell order queue is a min heap, storing the lowestpriced ones at the top. You can easily implement a market order by using the same queues, just set their price to 0 in case of sell order and to MAX_INT in case of buy order and adjust your logic.
There is a O(1) insertion solution but it's not worth implementing as it is essentially a LRU cache reimplementation (doubly linked list with a hashmap) where you have to rely on pointers arithmetic a lot which is tricky to get without making a stupid one-off error. Solution described above is enough to pass the interview.
Tags: order book, kraken, crypto, exchange, order, execution, trade,crypto
How to run the server and test client.
- Build the Docker image
docker build -t krakentask .
- Run the container and exec into it
docker run -it krakentask /bin/bash
- cd into
/home
directory in the running container. - Run
cmake .
to generate build files. - Run
cmake --build .
to build the binary and tests.
- In a home directory in a shell inside the running container, run
./krakentask
to start the server on port 1234. - Open a new terminal and exec into the container to get a second shell. Firstly check the hash of a running docker image by running
docker ps
. Then exec into the container like thisdocker exec -it [CONTAINER_HASH] /bin/bash
. Go to the /home directory. - Run
python3.8 client.py inputFile.csv localhost 1234
to parse and send data contained ininputFile.csv
into the server.
You should see output data being printed in the first shell.
To start unit tests, run ./unit_tests
binary that's located inside the test
directory. From /home
directory you can run ./test/unit_tests
.