Compose Mental Model & Phases Interview Questions
UI › Compose Core
What is Jetpack Compose, and how is declarative UI different from the View system?
What a strong answer covers: Compose is Android's declarative UI toolkit: you write composable functions that describe the UI for the current state, and when state changes Compose re-runs the affected functions and updates the UI. With Views you build a tree once and mutate it imperatively (setText, setVisibility), keeping UI and state in sync yourself. Declarative UI makes the UI a function of state.
What triggers recomposition?
What a strong answer covers: A change to a State object (mutableStateOf, a collected StateFlow, derivedStateOf) that was read during composition. Compose tracks which composable scopes read which state and schedules only those scopes to re-run. Changes to plain variables don't trigger anything. Parameters changing also cause a child to recompose unless they're stable and equal, in which case it can be skipped.
What are Compose's three phases, and why does it matter which phase reads state?
What a strong answer covers: Composition (what to show: runs composable functions), layout (where: measure and place), and drawing (how: render). A state read invalidates only the phase it was read in. Reading a scroll offset in composition recomposes every frame; reading it inside Modifier.offset { } (layout) or drawBehind (drawing) skips composition entirely. Deferring reads to later phases is a key performance technique.
What does it mean that composable functions should be idempotent and side-effect free, and what breaks if they aren't?
What a strong answer covers: A composable can run many times, in any order, possibly in parallel in the future, and may be skipped or abandoned. So it shouldn't modify external state, write to a database, increment a counter or start a network call while composing; it should only describe UI from its inputs. Side effects in the body run on every recomposition, giving duplicate requests or infinite loops. Use effect handlers (LaunchedEffect) or event callbacks.
How does Compose know which part of the UI to update? Explain the slot table, positional memoization and the role of keys.
What a strong answer covers: The compiler transforms composables to take a Composer, and during composition Compose records groups and remembered values in the slot table, identified by their position in the call tree. On recomposition, it walks the same positions and compares. remember works because values are stored at that position. When items move in a loop, position-based identity breaks, so key(item.id) or keys in LazyColumn give a stable identity so state moves with the item.