Mobile System Design Flashcards
ADVANCED › System Design
- How would you structure a feature using Android's recommended architecture?
- Three layers: a UI layer (Compose UI with a ViewModel state holder), an optional Domain layer of use cases for reusable or complex business logic, and a Data layer of repositories plus single-source data sources. The UI observes immutable state and sends events down toward the data layer.
- What is the single source of truth (SSOT) and why does it matter?
- One owner per data type that exposes the data as an immutable type and is the only place that can mutate it; others request changes via functions or events. It centralizes changes, resolves multi-source conflicts, and makes bugs traceable. For offline-first, the local database (Room) is the device SSOT.
- Compare cache-first, network-first, and stale-while-revalidate.
- Cache-first (offline-first) serves cached data and only hits the network on a miss or to refresh. Network-first fetches fresh data and falls back to cache on failure. Stale-while-revalidate shows cached data instantly for responsiveness, then revalidates in the background and updates the UI when fresh data arrives.
- How do you implement paginated data with local caching on modern Android?
- Use Paging 3: a PagingSource (or a RemoteMediator that writes network pages into Room as the SSOT). A Pager exposes a Flow<PagingData> that the repository surfaces and the UI collects, so the database drives the list and supports offline scrolling.
- What pagination strategies exist and which suits an infinite, frequently-updated feed?
- Offset/limit (simple but suffers page drift and slow large offsets), keyset/seek (indexed and fast but leaks the ordering field), cursor/token (opaque token, drift-resistant, hides DB internals), and page-number (offset's cons). Cursor pagination is usually best for an infinite, changing feed.
- How should you model loading, empty, and error states for a screen?
- Expose a single sealed UI state (e.g., Loading, Success, Empty, Error) as a StateFlow from the ViewModel so states are mutually exclusive and exhaustively handled. Map exceptions to an Error state with retry, distinguish an empty-success from loading, and drive retries via events under UDF.
- What is unidirectional data flow (UDF)?
- State flows in one direction, from the SSOT down to the UI, while events (user actions) flow up from the UI to the SSOT where data is mutated. This keeps data consistent, reduces bugs, and makes the system easier to test and debug.
- How would you design an offline news reader?
- Repository with Room as the SSOT; WorkManager schedules periodic/connectivity-aware sync to download articles and prefetch images (Coil). The UI observes a Room Flow so it works fully offline, network refreshes revalidate the cache (stale-while-revalidate), and Paging 3 + RemoteMediator handle paged article lists.
- Outline a testing strategy across the architecture layers.
- Unit-test ViewModels and repositories with hand-written fakes (Android favors fakes over mocks), use Turbine to assert Flow/StateFlow emissions, an in-memory Room database for DAO tests, and MockWebServer for the API layer. Add Compose UI/instrumented tests for screens, testing each layer in isolation behind clear interfaces.