Here's a mock interview question that pushes MVVM to its technical limits!
This question is meant to be completed (comfortably) within a 60-minute timeslot.
Most companies will allow you to ask (rather extensive) questions to your interviewer AND use internet searches (minus Generative AI) to complete this task.
As a disclaimer, this is an Android question specifically for Jetpack Compose. Jetpack Compose is industry standard, but older / more sluggish companies may have Android Native questions. Android Native is an outdated framework we do not teach in this course.
This app heavily tests understanding of state management. You may also encounter questions about Networking, Concurrency, UI building, etc., but those are not tested in this question.
Ready for your question? Here's your task!
The Infinite Entropy Row
This has NO release code. Simply solve this from an empty activity.
This is a simple, one-screen (not-very-actually-useful) app that tests difficult use of state management / MVVM by having you implement an infinitely scrolling row of items that each have some remembered state!
In general, you'll try to match this UI:
First, watch this demo video to see what you'll aim to build:
Requirements
Here's a detailed breakdown of your requirements!
Entropy Counter
At the top of the screen, you'll see a counter for the current "Entropy" value within the app.
Entropy is a fancy math term for "randomness."
You'll also see a little text for the most recently-spawned entropy value.
For now:
Make a large text for the current entropy value!
Make a smaller text that says something like "Recent Value: XXX"
In general, try to match our screenshot for UI (but that's not important).
The Infinite Row
Under the entropy counter, you'll build the infinite scrolling row!
Here are some requirements specifying this row:
It scrolls infinitely! You shouldn't be able to realistically hit the end of the scroll.
Each entry in the row is a simple UI element that basically just displays a text. You can add a shape/surface to make it look pretty.
Scrolling on a new entry in the row should "spawn a new entropy value," and place it in the row entry as a simple text.
This should be reflected in the "Recent Value" text.
Spawning an entropy value means selecting a random value +- 100 around the entropy value (e.g. if the entropy value is 1000, it should select from 900 to 1100). Don't worry about inclusive/exclusive.
Scrolling BACK on old entries should NOT overwrite their previous value. They should remember their sampled value.
Every time you scroll on a new entry, the entropy value should increase by 1000.
This should be reflected in the "Entropy' text.
Many Infinite Rows (Challenge!)
Can you make several infinite rows that have separate contents and scrolls?
They should all sample values from the same entropy value, and contribute to the same total entropy.
Tips
You *can* solve this with MVVM (and I recommend it! State management is so much easier with MVVM). But you can also just solve it with mutable states.
If you DO solve it with MVVM, don't bother setting up Hilt or whatever. You can just make mock VMs as regular objects, or ViewModels. (Not Hilt).
You don't necessarily need to follow best practices by, for example, making different files for each Composable, to save time.
Ignore how in the demo, if I scroll too fast, N/As appear. You can assume we'll scroll at a reasonable speed.
HINTS:
(Expand for hints!)
Using items(Int.MAX_VALUE) within a LazyColumn will render a ton of of items in the row, with little performance cost due to how LazyColumn "recycles" views.
Random.nextInt(X, Y) chooses a random integer in the value range.
Use LaunchedEffect(Unit) {} to determine when a view is scrolled on. Then, have some onRender() viewModel function that takes in the item index to determine if it's been initialized yet.
You can represent each list with a FINITE List<Int> , where non-existent indices indicate that the user hasn't scrolled on those indexes yet. When the scroll occurs, you then can add to the list to indicate the entropy value on those items.
To make multiple rows, you can consider using a List<List<Int>> where each List<Int> represents one row.
Solution!
See the .zip solution below! There's even a bit too much polish to this; you can be scrappier if needed!
Was that tough? It probably should have been. This is a very difficult state management question; if you can master this you'll have mastered this cornerstone of Compose!