DataStore & Preferences Interview Questions

DATA › Storage

What is DataStore, and why is it replacing SharedPreferences?

What a strong answer covers: DataStore is Jetpack's storage for small key-value or typed data, built on coroutines and Flow. SharedPreferences has a synchronous API that can block the main thread (even apply can block at lifecycle points via QueuedWork), no error signalling, no transactional guarantees, and isn't safe across processes. DataStore is async, transactional, signals errors and is consistent.

What's the difference between Preferences DataStore and Proto DataStore?

What a strong answer covers: Preferences DataStore stores key-value pairs with typed keys and no schema, like SharedPreferences. Proto DataStore stores a typed object defined by a schema (protocol buffers, or any type with a custom Serializer such as kotlinx.serialization JSON), giving type safety and default values. Use Proto or typed for structured settings.

How do you read and update values in DataStore?

What a strong answer covers: Read with dataStore.data, a Flow that emits the current data and changes; map it to the value you need and handle IOException with catch. Write with dataStore.edit { prefs -> prefs[KEY] = value } or updateData { it.copy(...) } for typed stores, which are suspend functions and run atomically: read-modify-write happens in one transaction.

What are DataStore's rules and limitations, like single instance and not storing large data?

What a strong answer covers: Create only one DataStore instance per file in the process (use the preferencesDataStore delegate at top level or a singleton), or you get errors and corruption. Use MultiProcessDataStore for several processes. It rewrites the whole file on every change, so it's not for large or relational data (use Room). Reads are async, so values needed synchronously at startup need a different approach or careful handling.

How would you migrate an app from SharedPreferences to DataStore without losing user settings, and handle code that reads settings synchronously?

What a strong answer covers: Pass SharedPreferencesMigration in the DataStore builder: it copies the keys on first access and deletes them from the old file. For synchronous readers, change them to collect or call first() from a coroutine, preload into an in-memory cache at startup, or keep a cached StateFlow. Avoid runBlocking on the main thread except as a last resort. Roll it out with tests covering upgraded installs.

Back to DataStore & Preferences