Skip to content
FrontDepth, Home

Back to the topic

Practice: List virtualization

Answer first, then compare. Rate yourself honestly — it drives your weak-areas list.

3 predict-output · 4 open

Questions up to level

Shows questions up to the selected level.

No practice questions for this level yet.

  1. Predict the outputMiddle

    A fixed-height virtual list: 1,000 rows of 40 px, a 400 px viewport and an overscan of 3. Which ranges does it render, and how tall is the spacer?

    JavaScript
    const ROW_HEIGHT = 40;
    const VIEWPORT = 400;
    const OVERSCAN = 3;
    const COUNT = 1000;
    
    function visibleRange(scrollTop) {
      const first = Math.floor(scrollTop / ROW_HEIGHT);
      const last = Math.ceil((scrollTop + VIEWPORT) / ROW_HEIGHT);
      const start = Math.max(0, first - OVERSCAN);
      const end = Math.min(COUNT, last + OVERSCAN);
      return start + "-" + end + " offset " + start * ROW_HEIGHT;
    }
    
    console.log(visibleRange(0));
    console.log(visibleRange(1010));
    console.log(visibleRange(39600));
    console.log(COUNT * ROW_HEIGHT);
  2. Predict the outputMiddle

    The list is scrolled far down when a search shortens it from 20,000 rows to 30. What does the naive range give compared with the clamped one? (32 px rows, a 480 px viewport, overscan 5; the last number on lines 2 and 3 is the height of the top spacer.)

    JavaScript
    const ROW_HEIGHT = 32;
    const VIEWPORT = 480;
    const OVERSCAN = 5;
    
    function naiveRange(count, scrollTop) {
      const start = Math.max(0, Math.floor(scrollTop / ROW_HEIGHT) - OVERSCAN);
      const end = Math.min(count, Math.ceil((scrollTop + VIEWPORT) / ROW_HEIGHT) + OVERSCAN);
      return [start, end];
    }
    
    function clampedRange(count, scrollTop) {
      const maxScrollTop = Math.max(0, count * ROW_HEIGHT - VIEWPORT);
      return naiveRange(count, Math.min(scrollTop, maxScrollTop));
    }
    
    const scrollTop = 64000;
    
    const [a, b] = naiveRange(20000, scrollTop);
    console.log(a, b, b - a);
    
    const [c, d] = naiveRange(30, scrollTop);
    console.log(c, d, Math.max(0, d - c), c * ROW_HEIGHT);
    
    const [e, f] = clampedRange(30, scrollTop);
    console.log(e, f, f - e, e * ROW_HEIGHT);
  3. Open questionMiddle

    Why can rendering a list of 10,000 rows freeze a page, and how does windowing fix it?

    Think it through first

    Answer out loud or in your head as if you were in an interview, then reveal.

  4. Predict the outputSenior

    A model of a variable-height list: prefix sums, a binary search for the first visible row, and a measurement that changes a row above the viewport. What does it log?

    JavaScript
    const heights = [40, 120, 40, 80, 40, 200, 40, 40];
    
    function offsetsOf(sizes) {
      const offsets = [0];
      for (const size of sizes) offsets.push(offsets[offsets.length - 1] + size);
      return offsets;
    }
    
    function indexAt(offsets, y) {
      let lo = 0;
      let hi = offsets.length - 2;
      while (lo < hi) {
        const mid = Math.ceil((lo + hi) / 2);
        if (offsets[mid] <= y) lo = mid;
        else hi = mid - 1;
      }
      return lo;
    }
    
    let offsets = offsetsOf(heights);
    let scrollTop = 300;
    const anchor = indexAt(offsets, scrollTop);
    const inside = scrollTop - offsets[anchor];
    console.log(offsets[heights.length]);
    console.log(anchor, inside);
    
    heights[1] = 100;
    offsets = offsetsOf(heights);
    console.log(offsets[anchor], indexAt(offsets, scrollTop));
    
    scrollTop = offsets[anchor] + inside;
    console.log(scrollTop, indexAt(offsets, scrollTop));
  5. Open questionSenior

    How do you virtualize a list whose rows have different heights that you cannot know in advance?

    Think it through first

    Answer out loud or in your head as if you were in an interview, then reveal.

  6. Open questionSenior

    What does windowing cost in accessibility, and how do you reduce those costs?

    Think it through first

    Answer out loud or in your head as if you were in an interview, then reveal.

  7. Open questionSenior

    A product needs to show a long list. How do you choose between rendering everything, content-visibility, pagination and windowing?

    Think it through first

    Answer out loud or in your head as if you were in an interview, then reveal.