Paging 3 Interview Questions
ARCHITECTURE › Components
What problem does Paging 3 solve, and what are its main components?
What a strong answer covers: It loads large data sets in pages as the user scrolls, saving memory and network. Components: PagingSource loads pages by key from one source; RemoteMediator handles network-plus-database paging; Pager builds a Flow<PagingData>; and the UI uses PagingDataAdapter or collectAsLazyPagingItems in Compose. It handles loading states, retry, deduplication and cancellation.
How do you implement a PagingSource, and what does getRefreshKey do?
What a strong answer covers: Override load(params) to fetch the page for params.key with params.loadSize, returning LoadResult.Page(data, prevKey, nextKey) or LoadResult.Error. getRefreshKey(state) returns the key to reload from when data is invalidated, usually based on state.anchorPosition, so after a refresh the user stays near where they were.
What is RemoteMediator, and when do you need it?
What a strong answer covers: RemoteMediator is for offline-first paging: the UI pages from a Room PagingSource (the source of truth), and the mediator fetches more from the network when the local data runs out, writing to the database inside a transaction, often with a remote keys table. Use it when you want cached, offline-capable lists; use a plain PagingSource for network-only paging.
How do you show loading, error and empty states with Paging 3?
What a strong answer covers: Observe loadState: refresh, append and prepend each are Loading, NotLoading or Error. Show a full-screen spinner or error for refresh, a footer for append (withLoadStateFooter or an item in LazyColumn), and empty state when refresh is NotLoading and itemCount is 0. retry() and refresh() trigger reloads. With RemoteMediator, check both source and mediator states.
How do you apply edits, like liking or deleting an item, to a paged list without breaking scroll position?
What a strong answer covers: PagingData is immutable, so the best approach is to update the source of truth: write the change to Room and the PagingSource is invalidated and reloads around the current position, keeping scroll. For network-only paging, keep a local map of overrides and combine it with the PagingData flow using map (applying edits per item), or invalidate the source. Avoid holding a PagingData snapshot and mutating it, and use cachedIn(viewModelScope).