Link relations in responses in Rest API - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When a REST API includes link relations in its responses, it adds extra steps to build those links.
We want to know how the time to create these links grows as the response size grows.
Analyze the time complexity of the following code snippet.
function buildResponse(items) {
return items.map(item => ({
data: item,
links: {
self: `/items/${item.id}`,
related: `/items/${item.id}/related`
}
}));
}
This code creates a response array where each item includes its data and two link relations.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The map function loops over each item once.
- How many times: Exactly once per item in the input array.
As the number of items grows, the number of link creations grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 20 link creations (2 per item) |
| 100 | 200 link creations |
| 1000 | 2000 link creations |
Pattern observation: The work grows directly with the number of items, doubling for twice as many items.
Time Complexity: O(n)
This means the time to build the response grows in a straight line with the number of items.
[X] Wrong: "Adding link relations does not affect time because itβs just a small addition."
[OK] Correct: Even small additions happen for every item, so the total time adds up as the list grows.
Understanding how adding links affects response time helps you design APIs that stay fast as data grows.
What if we added nested link relations inside each related item? How would the time complexity change?
Practice
link relations in REST API responses?Solution
Step 1: Understand link relations concept
Link relations describe the relationship between resources and provide URLs to related resources or actions.Step 2: Identify the purpose in REST API responses
They help clients navigate the API by following links instead of hardcoding URLs.Final Answer:
To describe how different resources are connected and provide URLs for related actions -> Option DQuick Check:
Link relations = resource connections and URLs [OK]
- Confusing link relations with data encryption
- Thinking link relations define data format
- Mixing link relations with HTTP method definitions
Solution
Step 1: Recall standard link relation format in JSON
Standard uses a_linksobject with named relations likeselfcontaining anhrefURL.Step 2: Match the correct JSON structure
"_links": { "self": { "href": "/users/123" } } matches this format exactly, others do not follow the standard naming or structure.Final Answer:
"_links": { "self": { "href": "/users/123" } } -> Option AQuick Check:
Standard link relation = _links with self and href [OK]
- Using 'links' instead of '_links'
- Missing 'href' inside the relation object
- Using arrays instead of objects for link relations
{
"_links": {
"self": { "href": "/orders/42" },
"cancel": { "href": "/orders/42/cancel" }
}
}What URL should a client use to cancel order 42?
Solution
Step 1: Locate the 'cancel' link relation in the JSON
The 'cancel' relation has the href value "/orders/42/cancel" which is the URL to cancel the order.Step 2: Confirm the correct URL for cancellation
The client should use the URL exactly as given in the 'cancel' href to perform the cancel action.Final Answer:
/orders/42/cancel -> Option CQuick Check:
Cancel URL = /orders/42/cancel [OK]
- Using the 'self' URL instead of 'cancel'
- Rearranging URL parts incorrectly
- Guessing URL instead of reading from response
{
"_links": {
"self": { "href": "/products/7" },
"edit": { "url": "/products/7/edit" }
}
}What is wrong with the link relations in this response?
Solution
Step 1: Check the property names inside link relations
Standard link relations use 'href' to specify the URL, not 'url'.Step 2: Identify the incorrect property
The 'edit' relation incorrectly uses 'url' instead of 'href'.Final Answer:
The 'edit' relation uses 'url' instead of 'href' -> Option AQuick Check:
Link relation URLs must use 'href' key [OK]
- Thinking 'url' is acceptable instead of 'href'
- Expecting full domain in href for relative URLs
- Renaming '_links' to 'links' incorrectly
Solution
Step 1: Recall the correct link relation format
Each link relation should be an object with an 'href' key inside the '_links' object.Step 2: Evaluate each option's structure
{ "_links": { "self": { "href": "/posts/10" }, "author": { "href": "/users/5" }, "comments": { "href": "/posts/10/comments" } } } correctly uses '_links' with 'self', 'author', and 'comments' keys, each having an 'href' URL. Options B and D use 'url' instead of 'href', and C uses strings instead of objects.Final Answer:
{ "_links": { "self": { "href": "/posts/10" }, "author": { "href": "/users/5" }, "comments": { "href": "/posts/10/comments" } } } -> Option BQuick Check:
Use '_links' with objects containing 'href' URLs [OK]
- Using 'url' instead of 'href' for links
- Using arrays instead of objects for link relations
- Assigning string URLs directly without 'href' objects
