What is dependency injection and how have you used it in a mobile app?
What the interviewer wants to hear
- Objects receive dependencies instead of creating them
- Makes testing easy by swapping fakes
- Tools: Hilt/Dagger, Koin, Swift initializer injection, get_it or Riverpod
- Scopes like singleton, per screen
- Avoid service locator overuse and hidden globals
Model answer
Dependency injection means a class gets the objects it needs from outside, usually through its constructor, instead of creating them itself. For example, a ProfileViewModel receives a UserRepository rather than building its own Retrofit client. This makes the code loosely coupled and easy to test, because in a unit test I can pass a fake repository. On Android I have used Hilt, which generates the wiring at compile time and handles scopes such as singleton for the database and ViewModel scope for screen objects. In Swift I usually do plain initializer injection with protocols, and in Flutter I have used Riverpod. I try to avoid hidden global singletons, because they make tests depend on each other and hide what a class really needs.
Can you answer it under time pressure?
Take a timed medium round and get scored on every answer.
More medium Mobile App Developer questions
- How would you add offline support to an app that currently only works online?
- Users say a list in your app stutters while scrolling. How would you find and fix the problem?
- Walk me through how you release a new version of a mobile app to the stores.
- What security practices do you follow when building a mobile app?
- How do you manage state in a Flutter or React Native app as it grows beyond a few screens?
- What testing strategy do you use for a mobile app, and what do you test at each level?
- How would you implement deep links in a mobile app from end to end?
- How do you make a mobile app accessible, and how do you check it?
- How would you set up CI/CD for a mobile app?