Code RoomMinimum external sort files
HardPrep Room Coding #4884

Minimum external sort files

CodingDistributed systemsAlgorithms & data structuresSenior–Staff~30 min

An external sort turns a stream of records into sorted runs before it merges them. Records arrive one at a time in a fixed order, and every open run file has to stay non-decreasing on the sort key, so a record can be appended to an open file only when its key is at least the last key that file already holds. When no open file can take a record, the sort has to open another one. Every record is written exactly once, and no record is held back or reordered. Given the keys in arrival order, return the fewest files the sort can finish with. Keys are integers, they may repeat, and they may be negative. An empty stream needs no files at all. Appending to any file that happens to fit is not the same as appending to the right one.

Implement
fewest_run_files(keys: list[int]) → int
Examples
in[[5,1,6,3]]out2
in[[8,1,9,3,0,3]]out3
in[[4,3,2,1]]out4
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 30 min
InputExpectedGot
[[5,1,6,3]]2not run yetsample
[[8,1,9,3,0,3]]3not run yetsample
[[4,3,2,1]]4not run yetsample