← Questions

Build a rating card in Jetpack Compose

0
OpenAICodingLast asked May 2026

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 draws max stars, fills the first rating, and reports taps.
  • class RatingViewModel(private val submit: suspend (rating: Int, comment: String) -> Unit) : ViewModel exposing state: StateFlow<RatingUiState> and onRatingChange, 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: StarRating owns no state, so it is reusable and previewable; the ViewModel is the single source of truth.
  • StateFlow collected with collectAsStateWithLifecycle, updates through copy, and submission launched in viewModelScope with isSubmitting guarding 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 ViewModel test with a fake submit, 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

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding