This project provides a class that collects IP addresses and delivers the top 100 IP address results.
- Clone this rep
- Run
ruby ip_address_sort.rb
- Examine the output
I would build an actual web service. If this was a real-world example, I would also look at multi-threading so that each request could be handled by its own thread, thus not blocking any attempts to save an IP address.
I chose to build a hash with this structure:
{
10: ["1.1.1.1", "2.2.2.2", "3.3.3.3",
9: ["4.4.4.4"],
...
}
The key is the number of times an IP address has been seen. The value is an array of the IP addresses that have been seen that number of times.
I determine the top 100 by checking that a key is in the range of the existing top 100. I then access the hash by the top 100 counts and start inserting the IP addresses into a rankings array.
I chose not iterate or sort by all count keys in the counts hash. Rather, I do a comparison of the 100 counts and drop the lowest count key, which limits the number of count keys to 100.
I included a test example in my code. Basically, I do this:
- Generate IP addresses.
- Pass each address into the
request_handled
function - Execute
top100
. Measure the execution time. Confirm that the returned object actually contains the top100 IP addresses.