Design a virtualized list component that renders only the visible rows

Quick Overview

Design a virtualized list component that renders only the rows inside a scrolling viewport, plus an overscan buffer above and below. It tests pixel-to-index arithmetic, clamping of the scroll position and indices, and keeping every operation constant time for lists of up to a million rows.

Design a virtualized list component that renders only the visible rows

Company: Riotgames

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Design a `VirtualizedList` component that renders only the items currently visible inside a viewport, instead of rendering the entire list. Frontend frameworks such as React and Vue use this pattern to render long lists with thousands of rows without slowing down. The list has a fixed `itemHeight`, a fixed `viewportHeight`, and a configurable `overscan`: the number of extra items to render above and below the visible window to reduce flicker during fast scrolls. As the user scrolls, the component tracks the current `scrollTop` and computes which items should be rendered. It also exposes the total content height (so the scrollbar reflects the full list size) and the vertical offset at which the first rendered item should be positioned. The constructor takes `totalItems`, `itemHeight`, `viewportHeight` and `overscan`, and the scroll position starts at 0. Implement these methods: - `scrollTo(scrollTop: int)`: update the current vertical scroll position, in pixels from the top of the list. Values are clamped to `[0, max(0, totalHeight - viewportHeight)]`. - `getRenderedRange() -> List[int]`: return `[startIndex, endIndex]` (inclusive) of the items to render, including the `overscan` buffer, clamped to valid indices. If the list is empty, return `[0, -1]`. - `getTotalHeight() -> int`: return the total pixel height of the virtual list, `totalItems * itemHeight`. - `getOffsetY() -> int`: return the vertical pixel offset `startIndex * itemHeight` to apply to the first rendered item so the visible rows line up inside the viewport. - `setTotalItems(totalItems: int)`: update the number of items in the list, and clamp the current `scrollTop` so it does not exceed the new maximum scroll position. Example 1, with 1,000 rows of 50 px in a 400 px viewport and an overscan of 2: ```text VirtualizedList(totalItems=1000, itemHeight=50, viewportHeight=400, overscan=2) getTotalHeight() -> 50000 getRenderedRange() -> [0, 9] getOffsetY() -> 0 scrollTo(1234) getRenderedRange() -> [22, 34] getOffsetY() -> 1100 ``` At scroll 0 the viewport covers pixels 0 to 399, which are rows 0 to 7. Overscan adds rows 8 and 9 below and nothing above. At scroll 1234 the viewport covers pixels 1234 to 1633, which are rows 24 to 32, and overscan widens that to rows 22 to 34. Row 22 starts at `22 * 50 = 1100`. Example 2, showing clamping and then a shrinking list: ```text VirtualizedList(totalItems=10, itemHeight=30, viewportHeight=100, overscan=1) scrollTo(500) getRenderedRange() -> [5, 9] getOffsetY() -> 150 setTotalItems(5) getTotalHeight() -> 150 getRenderedRange() -> [0, 4] getOffsetY() -> 0 ``` The content is 300 px tall, so the largest scroll position is 200 and `scrollTo(500)` stores 200. Rows 6 to 9 are visible, and overscan adds row 5 above but cannot go past row 9. After `setTotalItems(5)` the content is 150 px tall, the maximum scroll drops to 50, and the viewport (pixels 50 to 149) shows rows 1 to 4, widened to rows 0 to 4. ```hint Pixels to rows Row `i` covers pixels `i * itemHeight` up to, but not including, `(i + 1) * itemHeight`. Find the row under the top pixel of the viewport and the row under its bottom pixel, and decide carefully which pixel is the last one the viewport actually shows. ``` ```hint Where clamping happens There are two separate clamps: one on the scroll position and one on the indices. The bound on the scroll position depends on `totalItems`, which can change after construction. ``` ### Constraints & Assumptions - `1 <= totalItems <= 10^6` (the empty-list rule covers a list emptied with `setTotalItems(0)`) - `1 <= itemHeight <= 10^4` - `1 <= viewportHeight <= 10^5` - `0 <= overscan <= 100` - `0 <= scrollTop <= totalItems * itemHeight` - At most `10^5` calls to `scrollTo` and `getRenderedRange`. ### What a Strong Answer Covers - The arithmetic that maps the scroll position and viewport height to the first and last visible rows, with the bottom edge handled without an off-by-one. - Clamping the scroll position to its valid range, clamping indices after the overscan is applied, and the empty list. - Keeping the state consistent when `setTotalItems` shrinks the list below the current scroll position. - An offset that matches the first row actually rendered, so overscan rows do not shift the visible content. - Constant time and space per operation, and the integer overflow risk of `totalItems * itemHeight` in languages with 32-bit integers. ### Follow-up Questions - How would you support rows of different heights, including heights that are only known after a row is measured? - Items are inserted above the rows the user is looking at. How do you keep those rows from jumping on screen? - With `10^6` rows of `10^4` px the content is `10^10` px tall, far beyond the height a browser allows for an element. How would you still give the user a working scrollbar? - How would you choose the overscan, and would you make it depend on the scroll direction or speed?

Overview: Design a virtualized list component that renders only the rows inside a scrolling viewport, plus an overscan buffer above and below. It tests pixel-to-index arithmetic, clamping of the scroll position and indices, and keeping every operation constant time for lists of up to a million rows.

Riotgames logo
Riotgames
Nov 25, 2026
mediumSoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design a VirtualizedList component that renders only the items currently visible inside a viewport, instead of rendering the entire list. Frontend frameworks such as React and Vue use this pattern to render long lists with thousands of rows without slowing down.

The list has a fixed itemHeight, a fixed viewportHeight, and a configurable overscan: the number of extra items to render above and below the visible window to reduce flicker during fast scrolls.

As the user scrolls, the component tracks the current scrollTop and computes which items should be rendered. It also exposes the total content height (so the scrollbar reflects the full list size) and the vertical offset at which the first rendered item should be positioned.

The constructor takes totalItems, itemHeight, viewportHeight and overscan, and the scroll position starts at 0. Implement these methods:

  • scrollTo(scrollTop: int) : update the current vertical scroll position, in pixels from the top of the list. Values are clamped to [0, max(0, totalHeight - viewportHeight)] .
  • getRenderedRange() -> List[int] : return [startIndex, endIndex] (inclusive) of the items to render, including the overscan buffer, clamped to valid indices. If the list is empty, return [0, -1] .
  • getTotalHeight() -> int : return the total pixel height of the virtual list, totalItems * itemHeight .
  • getOffsetY() -> int : return the vertical pixel offset startIndex * itemHeight to apply to the first rendered item so the visible rows line up inside the viewport.
  • setTotalItems(totalItems: int) : update the number of items in the list, and clamp the current scrollTop so it does not exceed the new maximum scroll position.

Example 1, with 1,000 rows of 50 px in a 400 px viewport and an overscan of 2:

VirtualizedList(totalItems=1000, itemHeight=50, viewportHeight=400, overscan=2)
getTotalHeight()    -> 50000
getRenderedRange()  -> [0, 9]
getOffsetY()        -> 0
scrollTo(1234)
getRenderedRange()  -> [22, 34]
getOffsetY()        -> 1100

At scroll 0 the viewport covers pixels 0 to 399, which are rows 0 to 7. Overscan adds rows 8 and 9 below and nothing above. At scroll 1234 the viewport covers pixels 1234 to 1633, which are rows 24 to 32, and overscan widens that to rows 22 to 34. Row 22 starts at 22 * 50 = 1100.

Example 2, showing clamping and then a shrinking list:

VirtualizedList(totalItems=10, itemHeight=30, viewportHeight=100, overscan=1)
scrollTo(500)
getRenderedRange()  -> [5, 9]
getOffsetY()        -> 150
setTotalItems(5)
getTotalHeight()    -> 150
getRenderedRange()  -> [0, 4]
getOffsetY()        -> 0

The content is 300 px tall, so the largest scroll position is 200 and scrollTo(500) stores 200. Rows 6 to 9 are visible, and overscan adds row 5 above but cannot go past row 9. After setTotalItems(5) the content is 150 px tall, the maximum scroll drops to 50, and the viewport (pixels 50 to 149) shows rows 1 to 4, widened to rows 0 to 4.

Constraints & Assumptions

  • 1 <= totalItems <= 10^6 (the empty-list rule covers a list emptied with setTotalItems(0) )
  • 1 <= itemHeight <= 10^4
  • 1 <= viewportHeight <= 10^5
  • 0 <= overscan <= 100
  • 0 <= scrollTop <= totalItems * itemHeight
  • At most 10^5 calls to scrollTo and getRenderedRange .

What a Strong Answer Covers Guidance

  • The arithmetic that maps the scroll position and viewport height to the first and last visible rows, with the bottom edge handled without an off-by-one.
  • Clamping the scroll position to its valid range, clamping indices after the overscan is applied, and the empty list.
  • Keeping the state consistent when setTotalItems shrinks the list below the current scroll position.
  • An offset that matches the first row actually rendered, so overscan rows do not shift the visible content.
  • Constant time and space per operation, and the integer overflow risk of totalItems * itemHeight in languages with 32-bit integers.

Follow-up Questions Guidance

  • How would you support rows of different heights, including heights that are only known after a row is measured?
  • Items are inserted above the rows the user is looking at. How do you keep those rows from jumping on screen?
  • With 10^6 rows of 10^4 px the content is 10^10 px tall, far beyond the height a browser allows for an element. How would you still give the user a working scrollbar?
  • How would you choose the overscan, and would you make it depend on the scroll direction or speed?