how to count number of requests in last second, minute and hour

后端 未结 10 2088
情歌与酒
情歌与酒 2020-12-07 09:35

There is a hypothetical web server which supports only one very simple API - count of requests received in the last hour, minute and second. This server is very popular in t

10条回答
  •  生来不讨喜
    2020-12-07 10:27

    My solution :

    1. Maintain a hash of 3600 ,which contains a count,timestamp as fields.

    2. For each request for:

      • get idx(index in arry for current element) by timestamp%3600.
      • if hash[idx].count=0, then hash[idx].count=1 and hash[idx].timestamp=inputTimeStamp
      • if hash[idx].count>0 ,then

      Case(1) : if i/p timestamp==hash[idx].timestamp,hash[count]++;

      Case(2) : if i/p timestamp>hash[idx].timestamp,then hash[idx].count=1 and hash[idx].timestamp=inputTimeStamp

      Case(3): : if i/p timestamp

    Now for any query for last second,minute,hour: Find idx as above, and as long as the timestamp matches with in given second/range/minute,keep on iterating back from from idx in circular manner.

提交回复
热议问题