Video Cache Hit Rates from a CSV Log
Problem statement
A video CDN writes one CSV log line per request. You get the file as an array of lines, logLines. The first line is the header timestamp,status,url. Each other line has a request time, a cache status of hit or miss, and the requested url.
Only video requests count. A video URL comes in one of two formats, and both carry the video ID:
- Path format:
https://cdn.example.com/videos/<id>/<file>, for examplehttps://cdn.example.com/videos/v1/seg1.tshas IDv1. - Query format:
https://cdn.example.com/stream?vid=<id>&seg=<n>, for examplehttps://cdn.example.com/stream?vid=v1&seg=2also has IDv1.
Skip every other request (static files and health checks). Requests for the same ID count together, whichever format they use.
For each video, its cache hit rate is hits / total, where total is its number of requests and hits is how many of them have status hit. Return an array of two strings:
- The video with the highest hit rate among videos whose rate is not 1.
- The video with the lowest hit rate among videos whose rate is not 0.
Write each answer as "<id> <hits>/<total>" with that video's own counts, not reduced (for example "a7 2/4"). If several videos tie on the rate, choose the smallest ID in plain string order. If no video qualifies, that answer is "NONE".
Function
cacheHitRateExtremes(logLines: String[]) → String[]Examples
Example 1
logLines = ["timestamp,status,url","1,hit,https://cdn.example.com/videos/v1/seg1.ts","2,miss,https://cdn.example.com/stream?vid=v1&seg=2","3,hit,https://cdn.example.com/videos/v2/seg1.ts","4,hit,https://cdn.example.com/stream?vid=v2&seg=2","5,miss,https://cdn.example.com/static/logo.png","6,hit,https://cdn.example.com/videos/v3/seg1.ts","7,miss,https://cdn.example.com/videos/v3/seg2.ts","8,miss,https://cdn.example.com/stream?vid=v3&seg=3","9,miss,https://cdn.example.com/videos/v4/seg1.ts"]return = ["v1 1/2","v3 1/3"]- The
staticline is not a video request, so it is skipped. - Both URL formats give video IDs. The counts are
v1: 1 hit of 2,v2: 2 of 2,v3: 1 of 3,v4: 0 of 1. - Leaving out
v2(rate 1), the highest rate isv1with1/2. - Leaving out
v4(rate 0), the lowest rate isv3with1/3.
Example 2
logLines = ["timestamp,status,url","10,hit,https://cdn.example.com/stream?vid=b2&seg=1","11,miss,https://cdn.example.com/videos/b2/seg1.ts","12,hit,https://cdn.example.com/videos/a7/seg1.ts","13,miss,https://cdn.example.com/stream?vid=a7&seg=5","14,hit,https://cdn.example.com/videos/a7/seg9.ts","15,miss,https://cdn.example.com/stream?vid=a7&seg=7","16,hit,https://cdn.example.com/health","17,hit,https://cdn.example.com/videos/c3/seg1.ts","18,miss,https://cdn.example.com/videos/z9/seg1.ts","19,miss,https://cdn.example.com/stream?vid=z9&seg=2"]return = ["a7 2/4","a7 2/4"]The counts are a7: 2 of 4, b2: 1 of 2, c3: 1 of 1, and z9: 0 of 2. The health check is skipped. a7 and b2 both have rate one half, which is the highest rate below 1 and also the lowest rate above 0. On a tie the smaller video ID wins, so both answers are a7, written with its own counts 2/4.
Example 3
logLines = ["timestamp,status,url","20,hit,https://cdn.example.com/videos/v6/seg1.ts","21,miss,https://cdn.example.com/static/style.css","22,hit,https://cdn.example.com/stream?vid=v5&seg=1","23,hit,https://cdn.example.com/videos/v5/seg2.ts"]return = ["NONE","v5 2/2"]Every video has rate 1 (v5: 2 of 2, v6: 1 of 1), so no video has a rate below 1 and the first answer is "NONE". For the second answer both rates tie at 1, and "v5" is the smaller ID.
Constraints
1 <= logLines.length <= 501logLines[0]is exactly"timestamp,status,url".- Every other line is
timestamp,status,url, wheretimestampis1to10digits andstatusis"hit"or"miss". - Every
urlhas one of these four forms:https://cdn.example.com/videos/<id>/<file>,https://cdn.example.com/stream?vid=<id>&seg=<n>,https://cdn.example.com/static/<file>, orhttps://cdn.example.com/health. <id>has1to8lowercase letters or digits,<file>has1to12lowercase letters, digits or dots, and<n>has1to4digits.