Posts

Showing posts from January 11, 2026

Cracking Android SDE2/SDE3 Interviews in 2026: Deep Dives, Code, Follow-ups | Part-4

Image
   Kotlin/Coroutines 41. CoroutineScope — Types, Usage, and Architectural Pitfalls What is a CoroutineScope (Architect View)? A CoroutineScope defines the lifetime of coroutines .  It answers one critical question : When should this coroutine be cancelled? If you get scopes wrong, you get: memory leaks crashes after configuration change background work running forever UI updates after destruction Scope choice = lifecycle correctness. Core Principle (Must Say in Interview) Never launch a coroutine without knowing who cancels it. Built-in Coroutine Scopes in Android 1. viewModelScope Lifecycle:  ✔ Active while ViewModel exists  ✖ Cancelled when ViewModel.onCleared() is called Threading: Default dispatcher: Dispatchers.Main.immediate Primary Use Case: Business logic Network calls Database operations State management Why it exists:  ViewModels survive configuration changes. If you used lifecycleScope , your network calls would restart on rotation. When to Use...