Self link for current resource in Rest API - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When a REST API returns a self link for the current resource, it often builds a URL dynamically.
We want to understand how the time to create this link changes as the resource data grows.
Analyze the time complexity of the following code snippet.
GET /users/{id}
function getUserResponse(userId) {
const user = database.findUserById(userId);
const selfLink = `/users/${user.id}`;
return { data: user, links: { self: selfLink } };
}
This code fetches a user by ID and creates a self link URL for that user.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Searching the database for the user by ID.
- How many times: Once per request, no loops in link creation.
The time to build the self link string stays about the same regardless of user data size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 users | Search once + build link once |
| 100 users | Search once + build link once |
| 1000 users | Search once + build link once |
Pattern observation: The link creation cost does not grow with input size; it is constant.
Time Complexity: O(1)
This means creating the self link takes the same amount of time no matter how many users exist.
[X] Wrong: "Building the self link takes longer if the user data is bigger."
[OK] Correct: The self link is just a short string using the user ID, so its creation time stays constant regardless of user data size.
Understanding how small parts of an API scale helps you write efficient and predictable code, a skill valued in real projects and interviews.
"What if the self link included a list of all related resources? How would the time complexity change?"
Practice
self link in a REST API response?Solution
Step 1: Understand the role of self link
The self link points to the current resource's URL, allowing clients to access or refresh it easily.Step 2: Differentiate from other links
Other links may point to related resources or metadata, but self specifically means the current resource.Final Answer:
To provide the URL of the current resource for easy access -> Option AQuick Check:
Self link = current resource URL [OK]
- Confusing self link with related resource links
- Thinking self link shows API version
- Assuming self link lists all endpoints
https://api.example.com/items/42?Solution
Step 1: Identify the standard key names
The standard way is to use an object named_linkscontaining aselfkey with the URL string.Step 2: Compare options
{"_links": {"self": "https://api.example.com/items/42"}} matches the standard exactly. Others use wrong keys or structure.Final Answer:
{"_links": {"self": "https://api.example.com/items/42"}} -> Option BQuick Check:
Use _links with self key for self link [OK]
- Using 'links' instead of '_links'
- Using 'self_link' or 'current' instead of 'self'
- Placing self URL outside _links object
{
"id": 10,
"name": "Book",
"_links": {
"self": "https://api.example.com/products/10",
"category": "https://api.example.com/categories/5"
}
}Solution
Step 1: Locate the self link in the _links object
The self link is under_linkswith keyself, value is the current resource URL.Step 2: Identify the URL value
The value is "https://api.example.com/products/10", which is the URL for this product resource.Final Answer:
https://api.example.com/products/10 -> Option AQuick Check:
Self link URL = https://api.example.com/products/10 [OK]
- Choosing related links like category URL
- Picking base API URL instead of self
- Ignoring the _links object structure
{
"id": 5,
"title": "Article",
"_links": {
"related": "https://api.example.com/articles/related"
}
}
What is the best fix to add a self link for the current resource at https://api.example.com/articles/5?Solution
Step 1: Identify where self link belongs
The self link must be inside the_linksobject with key exactly "self".Step 2: Choose correct key and location
Add "self": "https://api.example.com/articles/5" inside the _links object adds the correct key "self" with the URL inside_links. Other options use wrong keys or wrong placement.Final Answer:
Add "self": "https://api.example.com/articles/5" inside the _links object -> Option CQuick Check:
Self link key = "self" inside _links [OK]
- Using wrong key names like self_link or link
- Placing self link outside _links object
- Omitting the self link entirely
https://api.example.com/users/7/orders?Solution
Step 1: Confirm self link key and URL
The self link must be under_linkswith key "self" and URL "https://api.example.com/users/7".Step 2: Choose appropriate key for related orders link
Using "orders" for the orders link is a standard practice to indicate the related resource. { "id": 7, "name": "Alice", "_links": { "self": "https://api.example.com/users/7", "orders": "https://api.example.com/users/7/orders" } } uses "orders" correctly.Step 3: Check other options for correctness
{ "id": 7, "name": "Alice", "_links": { "self": "https://api.example.com/users/7", "orders": "https://api.example.com/users/7/orders" } } uses "orders" key which is standard. { "id": 7, "name": "Alice", "links": { "self": "https://api.example.com/users/7", "orders": "https://api.example.com/users/7/orders" } } uses "links" instead of "_links". { "id": 7, "name": "Alice", "_links": { "self_link": "https://api.example.com/users/7", "orders_link": "https://api.example.com/users/7/orders" } } uses non-standard keys "self_link" and "orders_link". The option with "related": "https://api.example.com/users/7/orders" uses a generic rel name which is less descriptive for the specific orders resource.Final Answer:
{ "id": 7, "name": "Alice", "_links": { "self": "https://api.example.com/users/7", "orders": "https://api.example.com/users/7/orders" } } -> Option DQuick Check:
Use _links with self and orders keys for current and orders URLs [OK]
- Using 'links' instead of '_links'
- Using non-standard keys like self_link
- Using generic 'related' instead of specific 'orders'
