HAL format overview in Rest API - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When working with HAL format in REST APIs, it's important to understand how the time to process responses grows as the data size increases.
We want to know how the number of operations changes when the API returns more linked resources.
Analyze the time complexity of the following HAL response processing code.
GET /orders
{
"_links": {
"self": { "href": "/orders" },
"next": { "href": "/orders?page=2" }
},
"_embedded": {
"orders": [ { "id": 1 }, { "id": 2 }, ... ]
}
}
This code fetches a list of orders with links and embedded order details in HAL format.
Look for repeated actions in processing the response.
- Primary operation: Iterating over the embedded orders array.
- How many times: Once for each order in the list.
As the number of orders grows, the processing time grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 operations (one per order) |
| 100 | 100 operations |
| 1000 | 1000 operations |
Pattern observation: The work grows directly with the number of orders.
Time Complexity: O(n)
This means processing time increases in a straight line as the number of embedded resources grows.
[X] Wrong: "Processing HAL responses is always constant time because the links are just references."
[OK] Correct: Even though links are references, processing embedded arrays requires visiting each item, so time grows with the number of items.
Understanding how response size affects processing time helps you design efficient APIs and handle data smartly in real projects.
"What if the HAL response included nested embedded resources? How would that affect the time complexity?"
Practice
_links section in a HAL formatted REST API response?Solution
Step 1: Understand the role of
The_linksin HAL_linkssection contains URLs pointing to related resources, helping clients navigate the API easily.Step 2: Compare options with HAL purpose
Options B, C, and D do not describe navigation or linking related resources, which is the key feature of_links.Final Answer:
To provide URLs to related resources for easy navigation -> Option DQuick Check:
HAL_links= URLs for related resources [OK]
_links always holds related resource URLs [OK]- Confusing
_linkswith data fields - Thinking
_linksstores authentication info - Assuming
_linksdefines API version
Solution
Step 1: Recall HAL syntax for links
HAL requires a_linksobject with named links, each having anhrefproperty.Step 2: Check each option's structure
"_links": { "self": { "href": "/orders/123" } } correctly uses_linkswithselfcontaining an object withhref. Others miss underscores, use wrong keys, or omithref.Final Answer:
"_links": { "self": { "href": "/orders/123" } } -> Option AQuick Check:
HAL link =_links+href[OK]
_links and href keys [OK]- Omitting underscore in
_links - Using string instead of object for link value
- Missing
hrefproperty inside link
{
"_links": {
"self": { "href": "/orders/123" },
"customer": { "href": "/customers/456" }
},
"orderNumber": "123"
}Solution
Step 1: Identify the
Thecustomerlink in_linkscustomerkey has anhrefvalue of "/customers/456" which points to the customer's details URL.Step 2: Confirm other options
selfpoints to the order URL "/orders/123". Other options mix order and customer IDs incorrectly.Final Answer:
/customers/456 -> Option BQuick Check:
Customer link =_links.customer.href= /customers/456 [OK]
_links for the named resource URL [OK]- Confusing
selfwithcustomerlink - Mixing order and customer IDs
- Ignoring the
hrefproperty
{
"_links": {
"self": "/orders/123",
"items": [{ "href": "/items/1" }, { "href": "/items/2" }]
}
}Solution
Step 1: Check the structure of the "self" link
In HAL, each link must be an object with anhrefproperty. Here, "self" is a string, which is incorrect.Step 2: Verify other parts
"items" is correctly an array of link objects. The_linkskey must have an underscore. So only "self" link is wrong.Final Answer:
"self" link should be an object with an "href" property -> Option CQuick Check:
HAL links = objects withhref[OK]
href keys [OK]- Using string instead of object for single links
- Removing underscore from
_links - Thinking arrays are not allowed for multiple links
Solution
Step 1: Verify the
{ "title": "My Post", "_links": { "self": { "href": "/posts/1" }, "author": { "href": "/users/42" }, "comments": [ { "href": "/comments/100" }, { "href": "/comments/101" } ] } } correctly uses_linkskey and link objects_linkswith each link as an object containinghref. Multiple comments are an array of link objects.Step 2: Check other options for errors
{ "title": "My Post", "links": { "self": "/posts/1", "author": "/users/42", "comments": ["/comments/100", "/comments/101"] } } uses "links" without underscore and strings instead of objects. { "title": "My Post", "_links": { "self": "/posts/1", "author": "/users/42", "comments": "/comments/100,/comments/101" } } uses strings instead of objects for links. { "title": "My Post", "_link": { "self": { "href": "/posts/1" }, "author": { "href": "/users/42" }, "comments": [ { "href": "/comments/100" }, { "href": "/comments/101" } ] } } uses incorrect key "_link" instead of "_links".Final Answer:
Option A JSON structure correctly follows HAL format -> Option AQuick Check:
HAL requires_linkswith objects havinghref[OK]
_links with objects and href for all links [OK]- Missing underscore in
_links - Using strings instead of objects for links
- Wrong key name like
_link
