/interviews

Primary LanguageTypeScript

Car Prices Interview Solution

Setup

  • Create a .env file and specify parameters as seen in .env.example for your redis instance
  • Run yarn or npm install

Test

  • Run yarn test

Cache.ts exports an interface to allow creation of cache service for injection into the CarPrices class in CarPrices.ts. This allows the CarPrices to be cache service implementation agnostic. For the purpose of this interview, the cache service is implemented using redis as its backbone. The RedLock algorithm is also used to handle race conditions which can be created by multiple simultaneuos requests.

The 3rd party API responsible for retrieval of prices using number plates is mocked in ExternalPrice.ts. It's response time is faked using a random value between a minute and 2 minutes. To keep a repeatable and predictable price generated by this mocked 3rd party API, price is calculated as the sum of the ASCII representation of the number plate characters multiplied by 100.

A nodejs event emitter is being used to listen for responses to in-flight requests. This allows asynchronous retrieval of responses.