This is a toy web application written to solve the n26 backend software engineer coding challenge.
I've decided to use Spark Java as the service framework, as it's very lightweight, easy to set up and I've never used it before, so it's a little additional fun for me.
I didn't have time to do a lot of unit testing, but I was able to test it sufficiently to make sure that it works. Normally I would prefer to create more unit tests as well as integration tests that spin up the service and test the REST API. As it is, I only had time to do ad hoc testing of the REST API, but my unit tests cover the underlying logic so hopefully I didn't miss any bugs.
The challenge calls for an in-memory, no database (or in-memory db) solution, so for these purposes I'm going to ignore the problem of scaling this solution to multiple machines (for which I would probably use a clustered in-memory db).
Basically we create a buffer that can hold 60 seconds worth of summary data (it is configurable for different bucket granularities, but I have it set for a granularity of 1 ms). When a new transaction is posted, we shift any still-valid buckets by whatever amount of time has elapsed since the last insert, removing any that are too old. This is done with System.ArrayCopy so it should be pretty fast.
The summary report is generated by iterating over the array and summing everything together, which is O(1) time because the size of the array never changes.
./gradlew clean build run
This will also execute the unit tests.
The server is configured to run on port 4567.
To get the current stats:
curl http://localhost:4567/statistics
To post a transaction:
curl -i http://localhost:4567/transactions -d "{\"amount\": 2.0, \"timestamp\" : 1524778982694}"