This project provides a simple REST API to generate Fibonacci numbers and lists of Fibonacci numbers. The calls are:
GET /fib/:n
GET /fib_list/:n
The approach taken was to maximize both n and throughput (concurrent
requests). Thus only a subset of the numbers in the sequence are
memoized. The rest are (re)computed as needed. Additionally, requests
to fib_list
are handled with a stream (see FibInputStream.scala).
This allows for (theoretically) arbitrarily large (up to n = Int.MaxValue
)
lists to be generated, as only fib(n - 2), fib(n - 1), and fib(n)
are stored in memory at any one time while fib(n) is streamed to the client.
Max values and error messages can be configured in application.conf.
Note: a slight change to the problem requirements was made: GET /fib_list/:n
will return up to fib(n) rather than the first n Fibonacci
numbers. This allows the last element in the list returned to equal
fib(n). Thus:
fib(0) = 0 fib_list(0) = [0]
fib(1) = 1 fib_list(1) = [0, 1]
fib(2) = 1 fib_list(2) = [0, 1, 1]
fib(3) = 2 fib_list(3) = [0, 1, 1, 2]
fib(4) = 3 fib_list(4) = [0, 1, 1, 2, 3]
fib(5) = 5 fib_list(5) = [0, 1, 1, 2, 3, 5]
...
- Scala 2.11.7
- sbt 0.13.11
-
If deploying to production:
activator playGenerateSecret
The
APPLICATION_SECRET
environment variable will need to be set in the production environment with the result of that command.
activator run
- Open http://localhost:9000/ to test with browser. The page functions as a rudimentary interactive shell.
- Try
curl http://localhost:9000/fib/100
orcurl http://localhost:9000/fib_list/20
to test with curl.
activator test
to run unit and integration tests./concurrent_test.sh
to run concurrent load test (against reference implementation)
- Reference implementation has minimal memory, thus querying for larger n values may not succeed.
- Inputing
fib n
orfib_list n
in the browser shell wheren > Int.MaxValue
is not handled on the client side but on the server. Ideally a check would be made before submitting a request.