How would you design a REST API for a simple blog with posts and comments? Mention URLs, methods and status codes.
What the interviewer wants to hear
- Resource-based URLs using nouns, such as /posts and /posts/{id}/comments
- Correct HTTP methods: GET, POST, PUT/PATCH, DELETE
- Meaningful status codes such as 200, 201, 400, 404
- Pagination and filtering for list endpoints
- Validation and consistent JSON error format
Model answer
I would model posts and comments as resources with noun URLs. GET /posts lists posts with pagination, for example ?limit=20&cursor=abc, and supports filters like ?author=5. POST /posts creates a post and returns 201 Created with the new post. GET /posts/{id} returns one post or 404 if it does not exist. PATCH /posts/{id} updates some fields and DELETE /posts/{id} removes it, returning 204. Comments are nested: GET /posts/{id}/comments and POST /posts/{id}/comments. I would validate input and return 400 with a consistent JSON error body such as { error: { code, message, fields } }. Writes need authentication, so 401 for no token and 403 when the user is not the author. I would also version the API, for example /v1, and document it with OpenAPI.
Can you answer it under time pressure?
Take a timed easy round and get scored on every answer.
More easy Backend Developer questions
- When would you choose a SQL database and when a NoSQL database?
- What is the difference between authentication and authorization? How would you implement both in an API?
- Explain what happens on the backend from the moment a request hits your server until a response is sent.
- What is a database index, and what are its costs? How do you decide which columns to index?
- What is a database transaction, and what do the letters in ACID mean? Give an example where you need one.
- What are logs, metrics and traces, and how would you use each one to run a backend service?
- How would you test a new POST /orders endpoint? Which kinds of tests would you write?
- Why do backend teams package services with Docker, and what makes a good Dockerfile?
- Explain the HTTP status code classes and give examples of codes a REST API commonly returns.