Code RoomFeed scroll anchor correction
EasyPrep Room Coding #4846

Feed scroll anchor correction

CodingAlgorithms & data structuresEntry–Mid~18 min

A virtualised feed re-measures its rows once images finish loading, and the scroll offset has to be corrected so the reader is not thrown. old_heights and new_heights give the pixel height of every row before and after the re-measure, in the same order and always the same length, and a height may be 0. Content begins at pixel 0 and each row sits directly below the one before it. The anchor is the row whose band holds scroll_top, where a row covers its top pixel up to but not including its bottom, so a zero height row can never be the anchor. Keep the reader the same distance into the anchor row, and when that row is now shorter than the distance, use its new height instead. Clamp the answer to the range 0 up to the new total height minus viewport, and return that maximum when scroll_top sat at or past the old content. Heights, scroll_top and viewport are never negative.

Implement
anchored_scroll_top(old_heights: list[int], new_heights: list[int], scroll_top: int, viewport: int) → int
Examples
in[[100,100,100,100,100,100],[150,100,100,100,100,100],250,100]out300
in[[100,200,100,100,100],[100,20,100,100,100],250,100]out120
in[[30,30,30],[30,30,30],45,30]out45
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 18 min
InputExpectedGot
[[100,100,100,100,100,100],[150,100,100,100,100,100],250,100]300not run yetsample
[[100,200,100,100,100],[100,20,100,100,100],250,100]120not run yetsample
[[30,30,30],[30,30,30],45,30]45not run yetsample