MyBank is an application for handling transaction log and statistics.
The project is based on a small web-service which is created using following technologies:
- Java 1.8
- Maven
- Spring Boot
For building and running the application you will need:
We can build the project using mvn as following:
mvn clean install
Please Note!
One of the tests sleeps for about a minute to test that statistics are relevant upto 60 seconds.
So mvn clean install
or running the StatisticsControllerTest
class can take a minute longer than normal.
We can run the application locally using various techniques mentioned below, which will start a webserver (Tomcat) on port 8080 (http://localhost:8080) and serves SwaggerUI where we can inspect and try endpoints.
We can start the application by executing com.mybank.MybankApplication
class from your IDE.
To run the application from command line use Spring Boot Maven plugin as following:
mvn spring-boot:run
This section will explain high level solution design for the given case.
- Above
sixtySecondStatisticsMetrics
is a fixed sizedVector<Statistics>
. - It can hold upto Maximum 60 elements.
- Each index in Metrics represent each Second in past one minute.
- Element at each index represents the Statistics of all transactions happened in that indexed second.
Please note again the last point that this structure DOES NOT contain any transaction data.
This section will explain how we perform various operations on statisticsMetrics
.
Get Statistics is needed for our /statistics
endpoint where user wants to get statistics of transactions happened in last 60 seconds.
- since each index represents the Statistics of transactions happened in that indexed second.
- We need to traverse and accumulate the complete metrics.
Since: we loop only for CONSTANT numbers (60) Hence:
O(1)
Since: Our Vector at maximum will contain CONSTANT(60) elements Hence:
O(1)
- With every passing second, each statistics in statisticsMetrics gets one second older.
- As we only need Statistics of last 60 seconds
- We need to continuously update statisticsMetrics
- and discard any states for transactions older than 60 seconds
- following diagram shows the high level design of algorithm to maintain this integrity.
Since: we loop only for CONSTANT numbers (60) Hence:
O(1)
Since: Our operation will create a Vector which at maximum will contain CONSTANT(60) elements Hence:
O(1)
- Whenever a transaction is posted on
/transaction
endpoint - Following algorithm evaluates it
- if it is older than 60 seconds, we will discard it: meaning that it will have no impact on statistics.
- otherwise we evaluate its age in seconds (which will be our index in vector)
- and update the statistics on given index as per new transaction
O(1)
O(1)
- To my beautiful family who supported me when I was working on this over the weekend.