An underground railway system is keeping track of customer travel times between different stations. They are using this data to calculate the average time it takes to travel from one station to another.
Implement the UndergroundSystem
class:
check_in(id, station_name, time)
- A customer with a card
id
, checks in at the stationstation_name
at thetime
. - A customer can only be checked into one place at a time.
- A customer with a card
check_out(id, station_name, time)
- A customer with a card
id
, checks out from the stationstation_name
at thetime
.
- A customer with a card
get_average_time(start_station, end_station)
- Returns the average time it takes to travel from
start_station
toend_station
.
- Returns the average time it takes to travel from
- The average time is computed from all the previous traveling times from
start_station
toend_station
that happened directly, meaning a check in atstart_station
followed by a check out fromend_station
. - The time it takes to travel from
start_station
toend_station
may be different from the time it takes to travel fromend_station
tostart_station
. - There will be at least one customer that has traveled from
start_station
toend_station
beforeget_average_time
is called. - You may assume all calls to the
check_in
andcheck_out
methods are consistent. - If a customer checks in at time t1 then checks out at time t2, then t1 < t2.
- All events happen in chronological order.
Commands file:
check<in/out>,id,station_name,time
get_average,start_station,end_station
Example
check_in,45,Leyton,3
check_in,32,Paradise,8
check_out,45,Waterloo,15
check_out,32,Cambridge,22
get_average,Paradise,Cambridge
Paradise,Cambridge,14.0
Input
check_in,45,Leyton,3
check_in,32,Paradise,8
check_in,27,Leyton,10
check_out,45,Waterloo,15
check_out,27,Waterloo,20
check_out,32,Cambridge,22
get_average,Paradise,Cambridge
get_average,Leyton,Waterloo
check_in,10,Leyton,24
get_average,Leyton,Waterloo
check_out,10,Waterloo,38
get_average,Leyton,Waterloo
Output
Paradise,Cambridge,14.0
Leyton,Waterloo,11.0
Leyton,Waterloo,12.0
- I am assuming there was a typo in the Example 1 Input and I've changed one of the lines from
check_in,10,Waterloo,38
tocheck_out,10,Waterloo,38
- This solution is over-engineered for such a simple problem to solve, however, I've decided to use Ruby and Kafka to solve this code assessment since I know these are the technologies used by the team and as a way to demonstrate proficiency on the confluent's tech stack
- The setup to run kafka is not configured to be highly available and in a real production environment this needs to be configured differently
- I've decided to not use Ruby on Rails in order to reduce some framework dependencies given the reduced scope of this problem
- The KSQL query to calculate trips average time can be way more performant and further optimisations could be applied incremental-averaging
rspec
./scripts/test-integration.sh
To install gettext on Mac OS X with brew execute brew bundle
in this directory or just brew install gettext