More in Android & Kotlin — page 4

Explain Structured Concurrency in Kotlin Coroutines
WHAT IT TESTS: your grasp of Kotlin coroutine parent-child scope hierarchies. ANSWER OUTLINE: structured concurrency binds coroutines to a scope; parent cancellation propagates to all children, preventing leaks. RED FLAG: calling them unmanaged threads.

When would you choose Foreground Service over WorkManager?
Tests immediate user-visible work versus deferrable jobs. Strong answers cite a notification-driven use case the user actively expects, like live navigation or a workout. Red flag: claiming WorkManager replaces real-time foreground tasks.

What is the difference between viewModelScope and lifecycleScope?
This tests scope ownership across configuration changes. viewModelScope survives rotation because it lives in ViewModel, while lifecycleScope dies with UI; use former for logic and latter for UI tasks. Never launch data loads in lifecycleScope.

How do you make a network request in an Activity using coroutines?
This tests main thread safety and coroutine dispatchers. A strong answer cites NetworkOnMainThreadException, uses lifecycleScope, and switches to Dispatchers.IO. Red flag: suggesting Dispatchers.Main for the network call or omitting the exception entirely.

Replace a production NetworkService with a fake in Hilt instrumented tests
Tests whether you know Hilt test overrides beyond basic DI. A strong answer covers @BindValue for field-level fakes and @TestInstallIn or @UninstallModules for module swaps.

How would you access a Hilt dependency in a non-injectable ContentProvider?
Tests Hilt's escape hatch for framework classes. Strong answer: define an EntryPoint in SingletonComponent, expose the dependency, and retrieve it with EntryPointAccessors.fromApplication. Red flag: field injection or manual static singletons.

How do you inject two different OkHttpClient instances with Hilt?
Tests Hilt qualifiers for same-type bindings. Answer: define a custom @Qualifier (or @Named), annotate two @Provides methods and the injection site. Red flag: manual client creation or subclassing OkHttpClient instead of qualifying the binding.

Explain Hilt scoping: @Singleton vs @ActivityRetainedScoped
Tests Hilt component lifecycles. Answer: @Singleton lives for the app process; @ActivityRetainedScoped survives config changes via ActivityRetainedComponent but dies when the activity finishes.

How do you inject UserRepository into a ViewModel with Hilt?
Tests Hilt constructor injection for classes you own. Annotate UserRepository's constructor with @Inject so Hilt auto-provides it to consumers like a @HiltViewModel. Red flag: using a @Provides module for a class you control.

What is Dependency Injection and Hilt's benefit over manual instantiation?
Tests your grasp of inversion of control and why frameworks matter. A good answer defines DI as supplying dependencies from outside, states Hilt automates object graph creation and scoping, and notes manual instantiation scatters construction logic.
Configure OkHttp Cache for Retrofit and force network requests
This tests HTTP caching semantics in mobile networking. A strong answer covers Cache setup with a 50 MiB directory, server Cache-Control driving expiration, and CacheControl.FORCE_NETWORK to bypass.
Parse polymorphic media JSON with Kotlin sealed classes and custom serializers
WHAT IT TESTS: Polymorphic deserialization with Kotlin sealed classes. ANSWER OUTLINE: Sealed Media with Image and Video; use Moshi Factory or kotlinx.serialization keyed on the type field. RED FLAG: Manual JSONObject branching or reflection-based parsing.

How do you upload a file with text data in Retrofit?
Tests multipart uploads versus JSON bodies. Strong answer: @Multipart, @POST, @Part; file as MultipartBody.Part and text as RequestBody; avoid @Field. Red flag: suggesting @Body with a data class.

How do you differentiate network and server errors in suspend functions?
WHAT IT TESTS: Structured error handling in coroutines and mapping exceptions to recovery. ANSWER OUTLINE: Catch IOException for connectivity and HttpException for HTTP errors; wrap in a sealed Result. RED FLAG: Catching Exception or exposing raw errors to UI.
How do you configure Moshi or Kotlinx.serialization for JSON key mismatches?
This tests library-specific field-mapping annotations. For Kotlinx.serialization use @SerialName with the JSON key; for Moshi use @Json with the name parameter. A red flag is confusing the two or using manual mapping instead of the built-in decorator.

How would you implement a robust offline-first repository?
Tests single-source-of-truth discipline and network-local sync. Outline a Room repository with Flow, background refresh, disk persistence, immediate local emission, and conflict resolution with retry.

Room DAO: Flow<List<User>> vs suspend fun getUsers(): List<User>
This tests reactivity versus one-shot queries in Room and coroutines. Flow emits on every table change on Room's dispatcher, while suspend returns a single snapshot requiring manual refresh.

Preferences vs Proto DataStore: when is Proto significantly better?
This tests type safety and schema trade-offs. A strong answer contrasts Preferences key-value pairs with Proto typed protobuf schemas, then names nested settings or migration needs as the Proto win. A red flag is recommending Proto for a single boolean.

Model a Room one-to-many Playlist-to-Song relationship
Tests Room relational modeling. Strong answer: Song foreign key, `@Embedded` Playlist with `@Relation` to `List<Song>`, DAO wrapped in `@Transaction`. Red flag: embedding songs in Playlist or skipping `@Transaction`, which causes N+1 queries.

How do you add a non-null column with a default in Room?
WHAT IT TESTS: SQLite ALTER TABLE semantics and Room Migration wiring. ANSWER OUTLINE: Bump version; run ALTER TABLE ADD COLUMN with NOT NULL DEFAULT in a Migration; register via addMigrations. RED FLAG: Dropping tables or omitting DEFAULT on existing rows.