Manage your vehicle fleet, see aggregate and individual commercial vehicle insurance policy information.
git clone git@github.com:StephenKoller/flash.git
npm i --only=prod && npm run start
- http://localhost:3000/
Provide the following:
- data structure for a single car:
- id
- year
- make
- model
- purchase value
- VIN
- license plate state
- license plate number
- data structure for aggregate metrics:
- total count
- count by year
- total purchase price
- method to take in list of cars, return aggregate data
- design data structure or method to provide quick lookups by VIN
- design simple UI to display the results of numbers three and four
- provide response to another design question. *see section below
Design a data structure/method to provide quick lookups for automobiles by VIN.
My first thought was to implement a hash table for lightning-fast lookups by a unique id, until I realized that "In JavaScript (see also JSON), all objects behave as associative arrays with string-valued keys..." source: Wikipedia.
Therefore, my solution was to structure the vehicles data as a large JSON object, using the VIN as the key.
Additionally, by maintaining a consistent data shape to the vehicle objects, we can rely upon V8's performance optimizations using Shapes and Inline Caches under the hood. https://mathiasbynens.be/notes/shapes-ics
My very un-scientific results show a roughly O(n) lookup time:
Vehicle Count | Time to GET vehicle by VIN (ms) |
---|---|
100 | 5 |
1,000 | 31 |
10,000 | 315 |
100,000 | 3102 |
How would you design a data structure/method to provide sorted lists of car data structures by property field?
A memoized function returning the results of a hand-rolled method using Array.sort should be sufficient. V8 uses the Tim Sort algorithm to power Array.sort, which is pretty impressive - adaptive, stable, O(n) memory and O(n log n) time in the worst case.
memoizedSortByProperty (arrayOfCars, sortProperty)
if(memoized result) return memoized result
sortedArrayByProperty = arrayOfCars.sort((a, b) => {
if a[sortProperty] < b[sortProperty] {
return -1
}
if a[sortProperty] > b[sortProperty] {
return 1
}
return 0
})
memoized result = sortedArrayByProperty
return sortedArrayByProperty
end
Avoid:
- too many external packages / setup
- transpilation (React, TypeScript)
- bundlers (Webpack & friends)
- client-side rendering / SPA frameworks
Topics not touched on:
- security (XSS, CORS, input sanitization,
helmet
) - containerization
- databases
- extensive testing (e2e, integration)
- UX and visual design
Package | Reason |
---|---|
node | JavaScript on the server |
express | API & routing |
path | relative file path munging |
pug | JS templating |
fs | file I/O |
body-parser | parsing form data |
jest | unit testing |
json-schema | schema validation for car / aggregate data structures |
faker / randexp / uuid | random data generation |
morgan | simple server logging middleware |
lodash | countBy , sample collection utils |
eslint / prettier | DX - code linting / formatting |
eslint-config-airbnb | decent default lint config |