Repository & Data Layer Interview Questions

ARCHITECTURE › Patterns

What is the repository pattern, and what problem does it solve?

What a strong answer covers: A repository is the single entry point for a type of data, hiding where it comes from (network, database, cache) behind a clean API. ViewModels ask the repository for data without knowing about Retrofit or Room, data source logic and caching live in one place, and you can swap or fake implementations in tests.

How should a repository combine a local database and a remote API?

What a strong answer covers: Usually the database is the single source of truth: the repository exposes a Flow from Room, and a refresh function fetches from the network and writes to the database, which re-emits to the UI. That gives offline support and consistency. Errors from the network are reported separately without wiping cached data.

What is the difference between a repository and a data source?

What a strong answer covers: A data source deals with one source: a RemoteDataSource wrapping Retrofit, a LocalDataSource wrapping a DAO. The repository coordinates one or more data sources and applies policies like caching, merging, conflict resolution and mapping into domain models. Splitting them keeps each focused and testable.

How do you decide when cached data is stale, and how does the repository expose freshness to the UI?

What a strong answer covers: Store a fetched-at timestamp per item or query, and refresh when it's older than a time-to-live or when the user pulls to refresh; servers can help with ETag or Cache-Control. Expose cached data immediately and refresh in the background, and give the UI a way to know a refresh is in progress or failed (a loading flag or a Resource type), so stale data can show with an indicator.

Two screens call the same repository method at once and trigger duplicate network requests. How would you deduplicate and share in-flight work?

What a strong answer covers: Keep in-flight requests in the repository: a map from key to Deferred (guarded by a Mutex or ConcurrentHashMap.computeIfAbsent), so a second caller awaits the same request; or expose a shared hot flow with shareIn in an application scope; or rely on the database as source of truth, with a single refresh guarded by a mutex. The repository must be a singleton for this to work, and failures must clear the entry so retries are possible.

Back to Repository & Data Layer