0
0
Vueframework~20 mins

Virtual scrolling for large lists in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Virtual Scrolling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the visible output of this virtual scrolling component?

Consider a Vue 3 component using virtual scrolling to display a list of 1000 items. The viewport height shows only 10 items at a time. The component renders only visible items plus a small buffer.

Given the code below, what will be the number of <li> elements rendered in the DOM when the user scrolls to item 50?

Vue
import { ref, computed } from 'vue';

export default {
  setup() {
    const itemHeight = 30;
    const viewportHeight = 300;
    const totalItems = 1000;
    const scrollTop = ref(0);

    const visibleCount = Math.ceil(viewportHeight / itemHeight);
    const buffer = 3;

    const startIndex = computed(() => Math.floor(scrollTop.value / itemHeight));
    const endIndex = computed(() => Math.min(startIndex.value + visibleCount + buffer, totalItems));

    const visibleItems = computed(() => {
      const items = [];
      for (let i = startIndex.value; i < endIndex.value; i++) {
        items.push(i);
      }
      return items;
    });

    // Simulate scroll to item 50
    scrollTop.value = 50 * itemHeight;

    return { visibleItems };
  }
};
A53 <li> elements rendered
B10 <li> elements rendered
C13 <li> elements rendered
D50 <li> elements rendered
Attempts:
2 left
💡 Hint

Calculate visibleCount + buffer to find how many items are rendered.

📝 Syntax
intermediate
1:30remaining
Which option correctly updates the scrollTop reactive value on scroll event?

In a Vue 3 virtual scrolling component, you want to update the scrollTop ref when the user scrolls the container. Which option correctly binds the scroll event and updates scrollTop?

Vue
<template>
  <div class="viewport" @scroll="onScroll" style="height: 300px; overflow-y: auto;">
    <!-- list items here -->
  </div>
</template>

<script setup>
import { ref } from 'vue';
const scrollTop = ref(0);

function onScroll(event) {
  // update scrollTop here
}
</script>
AscrollTop = event.scrollTop;
BscrollTop.value = event.target.scrollTop;
CscrollTop.value = event.scrollTop;
DscrollTop = event.target.scrollTop;
Attempts:
2 left
💡 Hint

Remember to update the value of a ref using .value and access the scroll position from the event target.

🔧 Debug
advanced
2:30remaining
Why does the virtual list flicker when scrolling fast?

A Vue 3 virtual scrolling list flickers or shows blank space when scrolling quickly. The component uses computed properties to calculate visible items but does not debounce scroll updates. What is the most likely cause?

AThe scroll event updates too frequently causing rapid re-renders without throttling or debouncing.
BThe itemHeight is set incorrectly causing wrong calculations.
CThe buffer size is too large causing too many items to render.
DThe component uses a v-for without a key attribute.
Attempts:
2 left
💡 Hint

Think about how scroll events fire rapidly and how that affects rendering.

🧠 Conceptual
advanced
1:30remaining
What is the main advantage of virtual scrolling over normal scrolling for large lists?

Why do developers use virtual scrolling when displaying very large lists in web apps?

AIt automatically paginates data from the server.
BIt disables scrolling to prevent user interaction.
CIt preloads all list items in memory for faster access.
DIt renders only visible items, reducing DOM nodes and improving performance.
Attempts:
2 left
💡 Hint

Think about what happens when you render thousands of items in the DOM.

state_output
expert
2:00remaining
What is the final value of startIndex after these scrollTop updates?

Given the Vue 3 setup below, what is the value of startIndex.value after executing the scrollTop updates in order?

const itemHeight = 20;
const scrollTop = ref(0);
const startIndex = computed(() => Math.floor(scrollTop.value / itemHeight));

scrollTop.value = 45;
scrollTop.value = 39;
scrollTop.value = 60;
A3
B2
C1
D0
Attempts:
2 left
💡 Hint

Calculate floor of scrollTop divided by itemHeight after last update.