Posts

Showing posts with the label Android App Development

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

Image
      71. Design a Production-Ready Stopwatch Using MVVM (No Libraries) Architectural Approach For a production stopwatch , correctness and lifecycle safety matter more than UI frequency. The key principles are: Time source must be monotonic  Use SystemClock.elapsedRealtime()  — not currentTimeMillis()  — to avoid issues with: device clock changes NTP sync timezone updates 2. State must survive configuration & process death UI state → StateFlow Persist critical timing data → SavedStateHandle 3. Ticker should be lifecycle-aware Use viewModelScope Cancel explicitly on pause/reset Avoid leaking coroutines 4. UI refresh ≠ time precision Time precision is millisecond-accurate UI updates can be throttled (e.g., 16ms for ~60fps) Data Model (Immutable, UI-friendly) @Immutable data class StopwatchState ( val elapsedMs: Long = 0L , val isRunning: Boolean = false ) Immutable state ensures: predictable recomposition easy testing no race conditions in UI ViewMod...