/Projects

Cursor-based infinite feed

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

PracticeMachine codingTier 2 · MechanismsCreate35 minMobile systems

You will be able to: build cursor-based infinite feed in 35 minutes, edge cases included.

Source: Tested in tests/mobile/MobileTests.kt

Mobile engineeringAll mobile pages

Day 4's cursor as a client component, with the races and duplicates a real scroll produces. Start from the basics in Day 12 →

Study with AI

35-minute round · MediumKotlin · coroutines · tested Watch as reels
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.

FeedPaginator.ktKotlin
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

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.ktKotlin
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.ktKotlin
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.ktKotlin
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.ktKotlin
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.ktKotlin
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.ktKotlin
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.
AI Engineer · Cursor-based infinite feed