Hierarchical resource paths in Rest API - Time & Space Complexity
When working with hierarchical resource paths in REST APIs, it's important to understand how the number of operations grows as the API handles nested resources.
We want to know how the time to process requests changes when the depth or number of nested resources increases.
Analyze the time complexity of the following REST API path handling.
GET /users/{userId}/orders/{orderId}/items
// The server fetches user data,
// then fetches the order for that user,
// then fetches all items in that order.
This code represents a hierarchical path where each resource depends on the previous one.
Look for repeated actions or loops in handling nested resources.
- Primary operation: Fetching items in the order (likely a list traversal)
- How many times: Once per item in the order, which depends on the number of items
As the number of items in an order grows, the time to fetch all items grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 items | About 10 fetch operations |
| 100 items | About 100 fetch operations |
| 1000 items | About 1000 fetch operations |
Pattern observation: The operations increase directly with the number of items.
Time Complexity: O(n)
This means the time to handle the request grows linearly with the number of items in the nested resource.
[X] Wrong: "Fetching nested resources is always constant time because it's just one API call."
[OK] Correct: Even though the path looks like one call, the server often fetches each nested resource separately, so time grows with the number of nested items.
Understanding how nested resource paths affect time helps you explain API performance clearly and shows you can think about real-world data sizes.
What if the API used pagination to limit items returned per request? How would that change the time complexity?
Practice
Solution
Step 1: Understand resource organization
Hierarchical paths show how resources are connected, like folders inside folders.Step 2: Identify the purpose of hierarchical paths
They help represent parent-child relationships clearly in the API URL.Final Answer:
A nested structure showing relationships between resources -> Option AQuick Check:
Hierarchical paths = nested resource relationships [OK]
- Confusing hierarchy with unrelated resource lists
- Ignoring the relationship between resources
- Thinking it's about data encryption
Solution
Step 1: Identify the parent resource and its ID
The parent resource is 'posts' with ID 10, so it comes first in the path.Step 2: Append the child resource
Comments belong to the post, so '/comments' follows '/posts/10'.Final Answer:
/posts/10/comments -> Option AQuick Check:
Parent resource + ID + child resource = /posts/10/comments [OK]
- Placing child resource before parent
- Swapping resource names and IDs
- Using incorrect order of segments
/users/5/orders/12/items, what resource does this path represent?Solution
Step 1: Break down the path segments
/users/5 means user with ID 5, /orders/12 means order with ID 12 for that user, and /items means the items in that order.Step 2: Understand the full resource
The path points to the items inside order 12 which belongs to user 5.Final Answer:
Items in order 12 of user 5 -> Option DQuick Check:
Path shows nested resources = items in order 12 of user 5 [OK]
- Ignoring the nested order and item relationship
- Confusing order with user profile
- Assuming it lists all users or orders
/projects//tasks/7Solution
Step 1: Check the path syntax
The path has two slashes '//' between 'projects' and 'tasks', which is not valid in REST API paths.Step 2: Understand correct path format
Only one slash should separate resource names and IDs to maintain proper hierarchy.Final Answer:
Double slash between 'projects' and 'tasks' is invalid -> Option BQuick Check:
Only single slashes separate path segments [OK]
- Ignoring double slashes as harmless
- Thinking resource names must be capitalized
- Believing trailing slash is mandatory
Solution
Step 1: Identify the resource hierarchy
Reviews belong to a variant, which belongs to a product. So the path should start with product ID, then variant ID, then reviews.Step 2: Construct the path in correct order
The correct order is /products/20/variants/5/reviews to show this nested relationship clearly.Final Answer:
/products/20/variants/5/reviews -> Option CQuick Check:
Parent to child order: product -> variant -> reviews [OK]
- Placing reviews before products or variants
- Mixing order of product and variant IDs
- Using unrelated resource order
Ready to prove it?
Pass the test and this pattern moves unknown
