Android System Design Interview Questions
How does "Where Is My Train" work without internet?
Tier: Less commonDifficulty: Medium
It does not use GPS or the network for its core trick. It uses the cell tower the phone is already camped on, since a phone keeps a tower connection for calls and SMS even with data off or unusable. That tower id is looked up in a map of towers to positions that shipped with the app. The position is then matched against the route of the train the user picked.
What I'd clarify first
- Does the user tell us which train they are on, because knowing the route turns a coarse position into a useful one.
- How large can the offline bundle be, since the tower map and the timetable both have to fit on a cheap phone.
- Is battery a constraint, given a journey is hours long and the screen is often on.
The mechanism
- An offline bundle, a map of tower ids to positions along the network, plus the timetable for every route.
- A reading of the currently camped tower, taken from the telephony APIs, which need no data connection.
- A match of the observed tower sequence against the chosen route, which is what turns a coarse reading into a position.
- A delay estimate, the difference between where the train is and where the schedule says it should be.
The tower map is crowdsourced rather than bought. Comprehensive tower locations are not reliably public in this market. So the app records the tower it is camped on whenever the device does have a GPS fix, and those observations are pooled and shipped back to everyone. That implies a bootstrapping problem, the first users on a new route get nothing, and it resolves quickly on any busy line because so many phones walk it every day.
The timetable is the other half, and it is easy to forget. Position alone cannot tell you a train is late. The delay is position against schedule, so both have to be on the device.
Matching the sequence rather than each reading on its own is what makes this usable. A single tower gives you kilometres of error. A run of towers in the order the route visits them rejects a spurious reading, because a train does not go backwards. It also tells you which side of a station you are on.
The permission, which is the real constraint
No data connection is needed, but location permission is. For apps targeting Android 10 and up, getAllCellInfo() and requestCellInfoUpdate() require ACCESS_FINE_LOCATION at runtime, where coarse used to be enough. That permission is foreground only unless you also ask for background location, so the readings come while the app is on screen. For this app that is fine, the user is looking at the screen because they want to know where the train is.
// API 29 and up, and it needs ACCESS_FINE_LOCATION granted at runtime.
telephony.requestCellInfoUpdate(executor, object : TelephonyManager.CellInfoCallback() {
override fun onCellInfo(cells: MutableList<CellInfo>) {
// isRegistered marks the tower the phone is actually camped on.
val camped = cells.firstOrNull { it.isRegistered }
}
})
Why this works and what it trades away
The accuracy is coarse. A tower can cover a few hundred metres in a city and several kilometres in open country, so the answer is "somewhere near this tower", not a GPS coordinate. That is an acceptable trade, because a passenger wants to know roughly where the train is and how late it is, not a coordinate.
Battery is the cost nobody mentions. Asking for a fresh cell reading every few seconds for a six hour journey adds up. The cadence is tied to how fast the train is moving, and it drops right down when the tower has not changed.
Once the device does have internet, the server takes over, because live position and delay from the operator beat anything derived on the phone. The precedence rule is worth stating. A fresh server position wins, and the tower estimate fills the gaps in between rather than being averaged with it.
What I'd call out as the interesting design choice
This works precisely because it does not try to be precise. It accepts tower level accuracy in exchange for a signal that is already there for free, with no GPS lock to wait for. A design built on offline GPS instead would still need a view of the sky, which a train interior does not reliably give you. That is the constraint the tower approach sidesteps.
Read more Privacy changes in Android 10 (opens in a new tab)