Cursor-based infinite feed

Prefetch before the bottom, one request at a time, no duplicates, no stale pages.

Part 1 of 5

Basics

The concepts this component is built on. With these in mind, every line of the solution has a reason.

1.1Key concepts

One idea per card, in the order the solution uses them.

01Pagination

Loading a long list a page at a time instead of all at once.

02Cursor vs offset

An offset says 'skip 40 items'; a cursor says 'continue after this item'. When new posts arrive at the top, offsets skip or repeat items and cursors do not.

03Prefetching

Start loading the next page a few rows before the user reaches the end, so scrolling never stops at a spinner.

04State machine

The paginator is always in exactly one state: idle, loading, failed or end. Each action is allowed only in certain states, which prevents double loads.

Part 2 of 5

Question

What exactly you are asked to build, what must be true when you are done, and what to ask before you start.

2.1Prompt

The question as the interviewer asks it, and the requirements to check your solution against.

Interviewer

Implement the paging logic for a social feed. Users scroll fast, new posts arrive at the top while they read, and pull-to-refresh can happen mid-load.

Requirements

  • Load the next page by cursor
  • Never two loads at once
  • Prefetch a few rows before the end
  • Drop duplicate items
  • Ignore responses that arrive after a refresh

2.2Clarify first

Questions worth asking, and the answers this interviewer gives.

You askInterviewer answers
Offset or cursor?Cursor. Offsets skip or repeat items when new posts are inserted.
Page size?Server decides; around 20.
Where are items stored?In memory here; in the offline app they go to Room and the UI observes it.
Part 3 of 5

Detailed answer

The design decisions, the complete Kotlin solution, and a walk through it one block at a time.

3.1Design decisions

Settle these before writing code; each one shows up in the solution.

Explicit state machine

A sealed State of Idle, Loading, Failed and End: easy to render and easy to test.

Seen-ID set

Items that shift across page boundaries are filtered out in O(1).

Generation counter

Refresh bumps it; responses from an older generation are ignored.

StateFlow out

items and state are StateFlows, so Compose collects them and recomposes only on change.

3.2Solution

Complete, compiling Kotlin with coroutines, built with warnings as errors and covered by tests/mobile/MobileTests.kt. The Java and TypeScript tabs do the same job with each language's own tools: Java compiled and run in our tests, TypeScript run in our tests.

KotlinJavaTypeScript
FeedPaginator.ktKotlinCopy
1import kotlin.coroutines.cancellation.CancellationException2import kotlinx.coroutines.flow.MutableStateFlow3import kotlinx.coroutines.flow.StateFlow4import kotlinx.coroutines.flow.asStateFlow5 6data class FeedPage<T>(val items: List<T>, val nextCursor: String?)7 8/**9 * Cursor-based infinite feed: one request at a time, prefetch before the bottom,10 * drop duplicates when new posts shift the feed, and ignore stale responses after a refresh.11 * Confined to the main thread: call it from viewModelScope.12 */13class FeedPaginator<T, ID>(14  private val idOf: (T) -> ID,15  private val fetch: suspend (cursor: String?) -> FeedPage<T>,16) {17  sealed interface State {18    data object Idle : State19    data object Loading : State20    data class Failed(val message: String) : State21    data object End : State22  }23 24  private val _items = MutableStateFlow<List<T>>(emptyList())25  val items: StateFlow<List<T>> = _items.asStateFlow()26  private val _state = MutableStateFlow<State>(State.Idle)27  val state: StateFlow<State> = _state.asStateFlow()28 29  private var cursor: String? = null30  private val seen = HashSet<ID>()31  private var generation = 032 33  suspend fun loadNext() {34    when (_state.value) {35      State.Loading, State.End -> return               // single flight; nothing after the end36      State.Idle, is State.Failed -> Unit37    }38    _state.value = State.Loading39    val requestGeneration = generation40    try {41      val page = fetch(cursor)42      if (requestGeneration != generation) return      // a refresh happened meanwhile43      val fresh = page.items.filter { seen.add(idOf(it)) }44      _items.value += fresh45      cursor = page.nextCursor46      _state.value = if (page.nextCursor == null) State.End else State.Idle47    } catch (e: CancellationException) {48      throw e49    } catch (e: Exception) {50      if (requestGeneration == generation) _state.value = State.Failed(e.message ?: "Could not load")51    }52  }53 54  suspend fun refresh() {55    generation++56    _items.value = emptyList()57    seen.clear()58    cursor = null59    _state.value = State.Idle60    loadNext()61  }62 63  /** Call as each row is composed: start the next page when the user nears the end. */64  suspend fun onItemShown(index: Int, prefetchDistance: Int = 5) {65    if (index >= _items.value.size - prefetchDistance) loadNext()66  }67}
67 linesUTF-8Kotlin
FeedPaginator.javaJavaCopy
1import java.util.ArrayList;2import java.util.HashSet;3import java.util.List;4import java.util.Set;5import java.util.concurrent.CompletableFuture;6import java.util.function.Function;7 8/**9 * Cursor-based infinite feed: one request at a time, prefetch before the bottom,10 * drop duplicates when new posts shift the feed, and ignore stale responses after a refresh.11 * All state changes happen under one lock, so it is safe to call from any thread.12 */13public final class FeedPaginator<T, ID> {14  public record FeedPage<T>(List<T> items, String nextCursor) {}15 16  public sealed interface State permits Idle, Loading, Failed, End {}17  public record Idle() implements State {}18  public record Loading() implements State {}19  public record Failed(String message) implements State {}20  public record End() implements State {}21 22  private final Function<T, ID> idOf;23  private final Function<String, CompletableFuture<FeedPage<T>>> fetch;24  private List<T> items = List.of();25  private State state = new Idle();26  private String cursor;27  private final Set<ID> seen = new HashSet<>();28  private int generation;29 30  public FeedPaginator(Function<T, ID> idOf, Function<String, CompletableFuture<FeedPage<T>>> fetch) {31    this.idOf = idOf;32    this.fetch = fetch;33  }34 35  public synchronized List<T> items() { return items; }36 37  public synchronized State state() { return state; }38 39  public CompletableFuture<Void> loadNext() {40    final int requestGeneration;41    final String requestCursor;42    synchronized (this) {43      if (state instanceof Loading || state instanceof End) return CompletableFuture.completedFuture(null);   // single flight; nothing after the end44      state = new Loading();45      requestGeneration = generation;46      requestCursor = cursor;47    }48    return fetch.apply(requestCursor).handle((page, error) -> {49      synchronized (this) {50        if (requestGeneration != generation) return null;       // a refresh happened meanwhile51        if (error != null) {52          Throwable cause = error.getCause() != null ? error.getCause() : error;53          state = new Failed(cause.getMessage() != null ? cause.getMessage() : "Could not load");54          return null;55        }56        List<T> next = new ArrayList<>(items);57        for (T item : page.items()) if (seen.add(idOf.apply(item))) next.add(item);58        items = List.copyOf(next);59        cursor = page.nextCursor();60        state = page.nextCursor() == null ? new End() : new Idle();61        return null;62      }63    });64  }65 66  public CompletableFuture<Void> refresh() {67    synchronized (this) {68      generation++;69      items = List.of();70      seen.clear();71      cursor = null;72      state = new Idle();73    }74    return loadNext();75  }76 77  /** Call as each row is bound: start the next page when the user nears the end. */78  public CompletableFuture<Void> onItemShown(int index, int prefetchDistance) {79    boolean near;80    synchronized (this) { near = index >= items.size() - prefetchDistance; }81    return near ? loadNext() : CompletableFuture.completedFuture(null);82  }83}
83 linesUTF-8Java
FeedPaginator.tsTypeScriptCopy
1export interface FeedPage<T> {2  items: T[];3  nextCursor: string | null;4}5 6export type FeedState = { kind: "idle" } | { kind: "loading" } | { kind: "failed"; message: string } | { kind: "end" };7 8/**9 * Cursor-based infinite feed: one request at a time, prefetch before the bottom,10 * drop duplicates when new posts shift the feed, and ignore stale responses after a refresh.11 */12export class FeedPaginator<T, ID> {13  items: T[] = [];14  state: FeedState = { kind: "idle" };15  private idOf: (t: T) => ID;16  private fetchPage: (cursor: string | null) => Promise<FeedPage<T>>;17  private cursor: string | null = null;18  private seen = new Set<ID>();19  private generation = 0;20 21  constructor(idOf: (t: T) => ID, fetchPage: (cursor: string | null) => Promise<FeedPage<T>>) {22    this.idOf = idOf;23    this.fetchPage = fetchPage;24  }25 26  async loadNext(): Promise<void> {27    if (this.state.kind === "loading" || this.state.kind === "end") return;   // single flight; nothing after the end28    this.state = { kind: "loading" };29    const requestGeneration = this.generation;30    try {31      const page = await this.fetchPage(this.cursor);32      if (requestGeneration !== this.generation) return;                      // a refresh happened meanwhile33      const fresh = page.items.filter((item) => {34        const id = this.idOf(item);35        if (this.seen.has(id)) return false;36        this.seen.add(id);37        return true;38      });39      this.items = [...this.items, ...fresh];40      this.cursor = page.nextCursor;41      this.state = page.nextCursor === null ? { kind: "end" } : { kind: "idle" };42    } catch (e) {43      if (requestGeneration === this.generation) this.state = { kind: "failed", message: e instanceof Error ? e.message : "Could not load" };44    }45  }46 47  async refresh(): Promise<void> {48    this.generation++;49    this.items = [];50    this.seen.clear();51    this.cursor = null;52    this.state = { kind: "idle" };53    await this.loadNext();54  }55 56  /** Call as each row is rendered: start the next page when the user nears the end. */57  async onItemShown(index: number, prefetchDistance = 5): Promise<void> {58    if (index >= this.items.length - prefetchDistance) await this.loadNext();59  }60}
60 linesUTF-8TypeScript

3.3Line-by-line walkthrough

The same code in the order you would explain it in the room. The line numbers follow the Kotlin file; the Java and TypeScript tabs above have the same steps in the same order.

1

State the UI can render

Spinner, error with retry, or end-of-feed come straight from this sealed interface.

FeedPaginator.ktKotlinCopy
17  sealed interface State {18    data object Idle : State19    data object Loading : State20    data class Failed(val message: String) : State21    data object End : State22  }
lines 17–22UTF-8Kotlin
2

Single flight

Loading or at the end? Do nothing. Failed or idle? Go. when over a sealed type is exhaustive.

FeedPaginator.ktKotlinCopy
34    when (_state.value) {35      State.Loading, State.End -> return               // single flight; nothing after the end36      State.Idle, is State.Failed -> Unit37    }
lines 34–37UTF-8Kotlin
3

Remember the generation

If a refresh happens while we wait, this response belongs to the old feed and is dropped.

FeedPaginator.ktKotlinCopy
38    _state.value = State.Loading39    val requestGeneration = generation40    try {41      val page = fetch(cursor)42      if (requestGeneration != generation) return      // a refresh happened meanwhile
lines 38–42UTF-8Kotlin
4

De-duplicate and advance

HashSet.add returns false for ids already seen; append only new items; a null cursor means the end.

FeedPaginator.ktKotlinCopy
43      val fresh = page.items.filter { seen.add(idOf(it)) }44      _items.value += fresh45      cursor = page.nextCursor46      _state.value = if (page.nextCursor == null) State.End else State.Idle
lines 43–46UTF-8Kotlin
5

Refresh starts over

Bump the generation, clear state, load page one.

FeedPaginator.ktKotlinCopy
54  suspend fun refresh() {55    generation++56    _items.value = emptyList()57    seen.clear()58    cursor = null59    _state.value = State.Idle60    loadNext()61  }
lines 54–61UTF-8Kotlin
6

Prefetch near the bottom

Rows report their index as they compose; within five of the end, load the next page before the user gets there.

FeedPaginator.ktKotlinCopy
64  suspend fun onItemShown(index: Int, prefetchDistance: Int = 5) {65    if (index >= _items.value.size - prefetchDistance) loadNext()66  }
lines 64–66UTF-8Kotlin

Complexity: O(page size) per load; O(1) duplicate checks

Part 4 of 5

Q&A

The questions interviewers ask after the code works. Answer out loud, then open.

4.1Follow-up questions

How do you keep scroll position after refresh?
Insert new items above and keep LazyListState's first visible item, or show a 'new posts' pill instead of jumping.
How would this work offline-first?
Pages are written to Room and the list observes the database; the paginator only drives fetching (Paging 3's RemoteMediator).
Part 5 of 5

Extra depth

The cases that break naive solutions, how the same design looks on iOS, and what to practise next.

5.1Edge cases

Say these before the interviewer does.

Show detailsHide details
  • Server returns an empty page with a cursor
  • Pull-to-refresh during a load
  • Same post appears on two pages
  • Error, then retry from the same cursor

5.2On iOS

The same design in Swift and Apple's frameworks.

Show detailsHide details
ApproachSwift / iOS
SwiftUIList rows call .onAppear; an @Observable paginator on the main actor holds the same state machine.
CancellationStore the load Task and cancel it on refresh, in addition to the generation check.