1. How would you design a REST API for a simple blog with posts and comments? Mention URLs, methods and status codes.
A strong answer covers:
- 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
Show 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.