Auth Tokens & Idempotency Interview Questions
DATA › Networking
How does token-based authentication typically work in a mobile app?
What a strong answer covers: The user logs in and the server returns a short-lived access token (often a JWT) and a long-lived refresh token. The app sends the access token in the Authorization header; when it expires (401), the app uses the refresh token to get a new pair. Tokens are stored securely, and logout clears them and revokes the refresh token on the server.
Where should you store auth tokens on Android?
What a strong answer covers: Not in plain SharedPreferences or files. Encrypt them with a key in the Android Keystore, for example by encrypting values before saving to DataStore with a Keystore-backed AES key (the Jetpack Security EncryptedSharedPreferences library is deprecated but still common). Keep the access token in memory where possible, and exclude token storage from backups.
What does idempotency mean for an API, and why does it matter on mobile?
What a strong answer covers: An idempotent operation gives the same result whether it runs once or several times. GET, PUT and DELETE are idempotent by design; POST usually isn't. On mobile, a request can succeed on the server but the response is lost on a flaky connection, so the app retries; without idempotency a retry could create a duplicate order or payment.
How do idempotency keys work, and how would you use them for placing an order?
What a strong answer covers: The client generates a unique key (a UUID) for each logical operation and sends it in a header. The server stores the key with the result; a repeat request with the same key returns the stored result instead of acting again. Generate the key when the user taps 'Place order', persist it with the pending order so retries after process death reuse it, and only create a new key for a genuinely new order.
Design the client side of a payment flow that has to cope with timeouts, retries, process death and duplicate taps.
What a strong answer covers: Disable the button or debounce taps; create a pending payment locally with an idempotency key before calling the server; submit through WorkManager or an app-scoped coroutine so it isn't tied to the screen; on timeout, don't assume failure: query the server for the status of that key; show pending, success or failure states from the stored record; and make every step resumable after process death. Never retry with a new key unless the user confirms a new attempt.