Skip to content
tezvyn:

Explain the roles of ViewModel, Repository, and data source in MVVM

Source: developer.android.comEasyHow cards are made

Explain the roles of ViewModel, Repository, and data source in MVVM

Tests your grasp of unidirectional data flow and separation of concerns in Android MVVM. A strong answer maps ViewModel to UI state, Repository to data coordination, and Room or Retrofit to I/O.

What's really being asked

The interviewer wants to see if you can draw clean architectural boundaries in Android MVVM. Specifically, they care whether you understand that each layer owns a single responsibility and that dependencies point inward toward data, not outward toward the UI. They also want to hear how state flows up and events flow down.

The full answer

First, the ViewModel acts as a state holder and transformation layer. It receives raw data from the Repository, converts it into UI state, and survives configuration changes. It never talks to Retrofit or Room directly. Second, the Repository serves as a single source of truth and abstraction boundary. It mediates between different data sources, for example returning cached Room data immediately while triggering a Retrofit refresh in the background. Third, the data source does the actual I/O. Room handles local persistence and SQL mapping; Retrofit handles network serialization and HTTP calls. Fourth, the interaction pattern is unidirectional. The UI observes ViewModel state; the ViewModel calls Repository methods; the Repository decides which data source to query; the data source returns results back up the chain. Fifth, testing implications. ViewModels are tested with fake Repositories; Repositories are tested with fake data sources; and data sources contain the only truly slow I/O.

The mistakes people make

A major red flag is describing the ViewModel as a place for business rules or direct network calls. Another is claiming the Repository is just unnecessary boilerplate that can be skipped in small apps. Some candidates blur the lines by saying Room lives inside the ViewModel or that the Repository emits UI-specific strings. Confusing the Repository pattern with a DAO is also common.

What usually comes next

The interviewer may ask how you handle errors across these layers, where coroutines and Flow fit in, or how you inject these dependencies with Hilt. They might also ask what happens if the Repository needs to merge local and remote data, or how you prevent memory leaks when the ViewModel outlives the UI.

A concrete example

Imagine a news app. The user opens the headlines screen. The Fragment observes a UiState sealed class from the ViewModel. The ViewModel asks the NewsRepository for headlines. The Repository checks Room for cached articles and returns them immediately via Flow, then calls Retrofit to fetch updates. Retrofit returns JSON that the Repository maps to entities and writes back to Room. Room emits the new cache, the Repository passes it along, and the ViewModel maps it to UiState.Success. The Fragment simply renders what it receives.

Interview question

When an app must show cached Room data immediately while fetching remote updates in the background, which layer owns this coordination?

  • a.Fragment, since it triggers the initial load and should choose the fastest data source
  • b.ViewModel, since it prepares UI state and survives configuration changes
  • c.Repository, since it mediates between Room and Retrofit as the single source of truthCorrect
  • d.Room DAO, since it controls the local cache and decides when data is stale
Why?

The Repository is defined as the single source of truth that returns cached Room data immediately while triggering a Retrofit refresh in the background. The ViewModel is a tempting distractor, but the card explicitly states it never talks to Retrofit or Room directly.

Just read this? Test yourself on what you have been reading.

Read the original → developer.android.com

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on android — each one lists the topics its interview covers.

See open roles