Implement CRUD API and tests
BnsfSoftware Engineer · Online AssessmentLast updated
This question evaluates proficiency in implementing RESTful CRUD APIs, correct HTTP semantics and content negotiation (text/plain request bodies with JSON responses), robust error handling, and the ability to design unit tests from a function specification to cover edge cases and failure modes.
You are given two software-engineering tasks.
- Implement a simple REST API for a resource such as
NoteorMessage.
- Support basic CRUD operations: create, read, update, and delete.
- The request body for create/update is raw text, not JSON, so the input
Content-Typeistext/plain. - The API must return appropriate HTTP status codes and JSON responses.
- Handle common failure cases such as missing resources, invalid input, and unsupported media types.
A reasonable interpretation is:
POST /itemswithContent-Type: text/plaincreates a new item from the raw text body.GET /items/{id}returns the item if it exists.PUT /items/{id}withContent-Type: text/plainreplaces the item's content.DELETE /items/{id}removes the item.
Even though requests use plain text, all responses should be JSON, for example:
- success:
{ "message": "created", "id": 1 } - error:
{ "message": "item not found" }
- Write unit tests for a function when you cannot see the original implementation code.
- You only have the function's specification or expected behavior.
- Your goal is to design tests that cover as many meaningful scenarios as possible, including edge cases and failure modes.
Explain what behaviors should be tested and how you would structure the tests to maximize confidence in correctness.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.