How would you use Retrofit to GET /posts/{id} and key components?
This checks your understanding of Retrofit's three-layer setup. You need a data class for JSON parsing, an interface with @GET and @Path, and a Retrofit.Builder with baseUrl and a converter.
WHAT THIS TESTS: This question probes whether you can wire together Retrofit's three essential layers: the model, the service contract, and the instance builder. At the senior level it is table stakes, but interviewers use it to filter out candidates who have only used copy-pasted snippets without understanding the dependency graph. The interviewer wants to hear you name the converter factory explicitly and explain why the interface must be turned into an implementation by Retrofit.create(). They also care that you know the difference between synchronous and asynchronous execution.
A GOOD ANSWER COVERS: First, the data model: a Kotlin data class with fields that map to the JSON keys, optionally using annotations if names differ. Second, the service interface: an interface with a method annotated @GET("posts/{id}") that returns either a Call<Post> or a suspend function, taking @Path("id") String id as a parameter. Third, the Retrofit instance: show Retrofit.Builder(), set the baseUrl to the API root, addConverterFactory(GsonConverterFactory.create()), then call build() and create(PostService::class.java). Fourth, invocation: call the method on the service instance and enqueue() or await() the result. Mentioning that Retrofit generates the implementation at runtime via a proxy is a nice bonus.
COMMON WRONG ANSWERS: Omitting the converter factory entirely and assuming Retrofit parses JSON automatically. Using @Query instead of @Path for the path segment. Declaring the service as a class instead of an interface. Hardcoding the full URL into the @GET annotation instead of using baseUrl plus a relative path. Forgetting that Call is generic and writing Call without a type argument. Claiming you instantiate the interface directly with new instead of using retrofit.create(). Another red flag is suggesting that Retrofit itself performs the actual HTTP connection without mentioning OkHttp underneath.
LIKELY FOLLOW-UPS: How would you add query parameters or headers? What is the difference between Call and suspend functions in Retrofit? How do you handle network errors or non-200 responses? Why do we use an interface rather than an abstract class? How would you swap the JSON converter for Kotlinx Serialization or Moshi? Can you have multiple base URLs or dynamic endpoints?
ONE CONCRETE EXAMPLE: Imagine fetching post 42 from jsonplaceholder.typicode.com. The data class is data class Post(val userId: Int, val id: Int, val title: String, val body: String). The interface declares @GET("posts/{id}") suspend fun getPost(@Path("id") id: Int): Post. The builder sets baseUrl("https://jsonplaceholder.typicode.com/") and addConverterFactory(GsonConverterFactory.create()). You then build it, create the service, and call getPost(42) inside a coroutine. Without the GsonConverterFactory, Retrofit would not know how to turn the JSON body into a Post object and would throw an exception at runtime. If you used Call<Post> instead, you would invoke enqueue() with a Callback to get the result asynchronously.
Read the original → square.github.io
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.