I had got this question in a interview, I'm qurious to know other types of solutions for this question.
To debug a problem with occasional high latency on an API server that sits behind a CDN, we would like to figure out from the web server logs how many requests the server receives in a short time window.
The nginx access logs are available at the path given in the “Files” tab to the right. The nginx configuration contained this log format specification:
log_format elb_dockerflow
'$msec "$http_x_forwarded_for" "$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" $request_time $upstream_response_time '
'"$gzip_ratio" $upstream_cache_status';For this problem you will only need the first column, a floating-point number representing the Unix timestamp of the request.
Implement a function that receives the length of the time window in seconds, and returns the maximum number of requests that happened during the log period in any time window of that length. Note that the time windows don’t necessarily need to be aligned with whole second boundaries – they can start at any time.**
Sample log:
1609558982.011 "122.225.186.147, 157.198.132.211" "GET /v1/buckets/blocklists/collections/addons-bloomfilters/changeset?_expected=1608230359579&_since=%221603953425672%22 HTTP/1.1" 200 16671 "-" "Amazon CloudFront" 0.018 0.018 "-" EXPIRED
1609558994.816 "55.30.124.73, 250.179.222.52" "GET /v1/buckets/security-state/collections/intermediates/records?_expected=1609444767338&_sort=-last_modified&_since=1601690248797 HTTP/1.1" 200 107281 "-" "Amazon CloudFront" 0.048 0.048 "-" MISS
1609559004.113 "128.178.246.140, 132.27.4.41" "GET /v1/buckets/blocklists/collections/certificates/records?_sort=-last_modified&_since=1477732902328 HTTP/1.1" 200 578672 "-" "Amazon CloudFront" 0.055 0.053 "-" MISS
1609559023.037 "82.32.104.44, 146.111.211.130" "GET /v1/buckets/main/collections/normandy-recipes-capabilities/records?_expected=1609545671368&_sort=-last_modified&_since=1593128929046 HTTP/1.1" 200 92649 "-" "Amazon CloudFront" 0.021 0.021 "-" MISS
1609559049.922 "232.123.69.237, 252.227.131.105" "GET /v1/blocklist/3/%7Bec8030f7-c20a-464f-9b0e-13a3a9e97384%7D/17.0.9/ HTTP/1.1" 200 1020361 "-" "Amazon CloudFront" 0.131 0.127 "-" MISS
1609559064.879 "152.193.152.43, 161.244.204.254" "GET /v1/buckets/main/collections/normandy-recipes-capabilities/changeset?_expected=1609545671368&_since=%221590710479632%22 HTTP/1.1" 200 93539 "-" "Amazon CloudFront" 0.020 0.020 "-" EXPIRED
1609559082.895 "76.190.150.65, 144.126.188.236" "GET /v1/buckets/security-state/collections/cert-revocations/changeset?_expected=1609552754966&_since=%221604239154942%22 HTTP/1.1" 200 53822 "-" "Amazon CloudFront" 0.022 0.022 "-" EXPIRED
1609559098.565 "14.70.210.37, 225.135.76.124" "GET /v1/buckets/main/collections/normandy-recipes-capabilities/records?_expected=1609545671368&_sort=-last_modified&_since=1582243282208 HTTP/1.1" 200 95839 "-" "Amazon CloudFront" 0.022 0.022 "-" MISS
1609559108.558 "221.29.242.77, 219.45.206.230" "GET /v1/buckets/main/collections/normandy-recipes-capabilities/changeset?_expected=1609459288144&_since=%221609545671368%22 HTTP/1.1" 200 727 "-" "Amazon CloudFront" 0.015 0.015 "-" EXPIRED
1609559117.515 "143.67.156.201, 96.27.117.90" "GET /v1/buckets/main/collections/normandy-recipes-capabilities/changeset?_expected=1609545671368&_since=%221600462726547%22 HTTP/1.1" 200 89561 "-" "Amazon CloudFront" 0.020 0.020 "-" EXPIRED
1609559137.746 "224.118.240.121, 88.203.62.125" "GET /v1/buckets/main/collections/normandy-recipes-capabilities/records?_expected=1609545671368&_sort=-last_modified&_since=1586460283967 HTTP/1.1" 200 94853 "-" "Amazon CloudFront" 0.022 0.022 "-" MISS
1609559159.218 "121.76.112.133, 213.99.12.204" "GET /v1/buckets/security-state/collections/cert-revocations/changeset?_expected=1609552754966&_since=%221602424667137%22 HTTP/1.1" 200 61284 "-" "Amazon CloudFront" 0.029 0.029 "-" EXPIRED
1609559180.127 "183.102.53.97, 178.56.101.110" "GET /v1/buckets/main/collections/normandy-recipes/changeset?_expected=1609545680251&_since=%221596931880793%22 HTTP/1.1" 200 87256 "-" "Amazon CloudFront" 0.022 0.022 "-" EXPIRED
1609559190.842 "15.60.206.89, 144.127.181.212" "GET /v1/buckets/main/collections/fxmonitor-breaches/changeset?_expected=1607379052602&_since=%221588681718275%22 HTTP/1.1" 200 17333 "-" "Amazon CloudFront" 0.024 0.024 "-" EXPIRED
1609559200.867 "244.109.34.154, 244.135.122.190" "GET /v1/buckets/security-state/collections/cert-revocations/changeset?_expected=1609552754966&_since=%221606528655130%22 HTTP/1.1" 200 39444 "-" "Amazon CloudFront" 0.021 0.021 "-" EXPIRED
1609559217.079 "117.26.98.222, 202.7.36.224" "GET /v1/buckets/main/collections/public-suffix-list/changeset?_expected=1575468539758 HTTP/1.1" 200 763 "-" "Amazon CloudFront" 0.013 0.013 "-" EXPIRED
1609559227.403 "200.27.68.234, 166.106.222.11" "GET /v1/buckets/main/collections/normandy-recipes/changeset?_expected=1609545680251&_since=%221601407879126%22 HTTP/1.1" 200 85611 "-" "Amazon CloudFront" 0.022 0.022 "-" MISS
1609559242.311 "174.184.71.54, 67.77.230.121" "GET /v1/buckets/security-state/collections/cert-revocations/changeset?_expected=1608947867477&_since=%221609487890087%22 HTTP/1.1" 200 2408 "-" "Amazon CloudFront" 0.017 0.017 "-" MISS
1609559256.004 "72.130.102.41, 123.107.202.238" "GET /v1/blocklist/3/%7B3550f703-e582-4d05-9a08-453d09bdfdc6%7D/45.0/ HTTP/1.1" 200 969433 "-" "Amazon CloudFront" 0.122 0.117 "-" EXPIRED
1609559274.655 "92.158.206.174, 231.101.15.33" "GET /v1/buckets/security-state/collections/intermediates/changeset?_expected=1609444767338&_since=%221603209441493%22 HTTP/1.1" 200 66370 "-" "Amazon CloudFront" 0.043 0.043 "-" EXPIRED
Sample Input/output
1 second = 1