Code RoomDocument fingerprint anchors
MediumPrep Room Coding #4889

Document fingerprint anchors

CodingAlgorithms & data structuresMid–Senior~30 min

A duplicate finder fingerprints a document rather than keeping it whole. It cuts the document into grams, a gram being the run of gram_len characters starting at each position, so the gram at position i begins at index i. It folds each gram to a number: start at 0 and for every character of the gram set h to (h * 31 plus the ASCII code of that character) modulo 1000003. It then slides a window over the list of gram numbers, window_len numbers wide, and from each window it anchors the position of the smallest number, breaking a tie by taking the rightmost of the tied positions. Given document, gram_len and window_len, return the anchored positions, each listed once, ascending. Return an empty list when either length is zero or negative, when gram_len is longer than the document, or when there are fewer gram numbers than window_len.

Implement
winnow_anchor_positions(document: str, gram_len: int, window_len: int) → list[int]
Examples
in["abcabcabc",3,2]out[0,1,3,4,6]
in["aaaa",1,2]out[1,2,3]
in["abcd",1,1]out[0,1,2,3]
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
["abcabcabc",3,2][0,1,3,4,6]not run yetsample
["aaaa",1,2][1,2,3]not run yetsample
["abcd",1,1][0,1,2,3]not run yetsample