MediumPro challengePythonJavaScriptTypeScript

Range Header Parser

Node.jsHTTPParsing

Parse an HTTP Range: bytes=... header. Support multiple ranges.

solve('bytes=0-499', 1000)[{ start: 0, end: 499 }].
solve('bytes=500-', 1000)[{ start: 500, end: 999 }].
solve('bytes=-200', 1000)[{ start: 800, end: 999 }] (last 200 bytes).
solve('bytes=0-99,200-299', 1000) → two ranges.

Invalid header → null.

Sample tests

Test #1Suffix
Input: ["bytes=-200",1000]
Output: [{"end":999,"start":800}]
Test #2Multiple ranges
Input: ["bytes=0-99,200-299",1000]
Output: [{"end":99,"start":0},{"end":299,"start":200}]
Test #3Invalid prefix
Input: ["invalid",1000]
Output: null
Test #4Closed range
Input: ["bytes=0-499",1000]
Output: [{"end":499,"start":0}]
Test #5Open end
Input: ["bytes=500-",1000]
Output: [{"end":999,"start":500}]