Build a rating card in Jetpack Compose
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in OpenAI's technical screen (coding), last asked May 2026. It tests android, jetpack compose and ui.
Build a rating card in Kotlin with Jetpack Compose: a card with a row of stars the user taps to rate, an optional comment, and a Submit button. Submitting calls a suspend function; the card shows progress while it runs and a success state when it finishes. Make the star row a reusable, stateless composable and keep the screen's state in a ViewModel.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
@Composable fun StarRating(rating: Int, onRatingChange: (Int) -> Unit, max: Int = 5, modifier: Modifier = Modifier)— stateless: it drawsmaxstars, fills the firstrating, and reports taps. -
class RatingViewModel(private val submit: suspend (rating: Int, comment: String) -> Unit) : ViewModelexposingstate: StateFlow<RatingUiState>andonRatingChange,onCommentChange,onSubmit. data class RatingUiState(val rating: Int = 0, val comment: String = "", val isSubmitting: Boolean = false, val submitted: Boolean = false, val error: String? = null)-
@Composable fun RatingCard(viewModel: RatingViewModel)wiring the two together.
Rules:
- Submit is disabled until a rating of at least 1 is chosen, and while a submission is running.
- A failed submission keeps the user's input and shows an error.
- After success the card shows a thank-you state instead of the form.
Example
initial stars 0/5, Submit disabled
tap star 4 stars 4/5, Submit enabled
tap Submit spinner, Submit disabled, stars and comment locked
submit() returns "Thanks for your rating!"
Clarifying questions
- Can the rating be cleared (tap the same star again)? Half stars?
- Is the comment required, and is there a length limit?
- What happens on rotation mid-submission?
- Should the star row be accessible as one slider-like control?
What a strong answer covers
- State hoisting:
StarRatingowns no state, so it is reusable and previewable; theViewModelis the single source of truth. -
StateFlowcollected withcollectAsStateWithLifecycle, updates throughcopy, and submission launched inviewModelScopewithisSubmittingguarding double taps. - Validation in one place (the
ViewModel), errors kept separate from success. - Recomposition kept small (stable parameters, lambdas that don't change every frame).
- Accessibility: content descriptions, touch targets, a semantics role so screen readers read "4 of 5 stars".
- Previews and tests: a
ViewModeltest with a fakesubmit, and a Compose UI test that taps a star and checks the button.
Follow-ups
- Support half-star ratings by dragging across the row.
- Persist a draft rating across process death.
- Use the same card in a list of products without one card's state leaking into another.
Where it's asked
- OpenAI · Technical screen: coding · Last asked May 202660 min · One problem in three to five growing parts filling the hour, usually in CoderPad; ML roles get a Colab notebook with autocomplete off · No AI assistants or autocomplete; documentation is fine
What it tests
android · jetpack compose · ui
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
