Code RoomHTTP range request parser
MediumPrep Room Coding #324

HTTP range request parser

CodingNetworking & APIsAlgorithms & data structuresMid–Senior~22 min

Parse an HTTP Range request header for a resource of known total_size bytes. The header has the form 'bytes=START-END', where either side may be omitted: 'bytes=0-499' is the first 500 bytes, 'bytes=500-' is from 500 to the end, and 'bytes=-500' is the last 500 bytes. If only the first byte-range-spec is needed, ignore anything after a comma. Clamp END to total_size-1. Return [start, end] as resolved inclusive offsets, or [-1, -1] if the unit is not 'bytes', the range is unsatisfiable (start beyond the resource), or a suffix length of 0.

Implement
parse_range(header: str, total_size: int) → list[int]
Examples
in["bytes=0-499",1000]out[0,499]
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 22 min
InputExpectedGot
["bytes=0-499",1000][0,499]not run yetsample