Nested resources in Rest API - Time & Space Complexity
When working with nested resources in REST APIs, it's important to understand how the number of operations grows as we access deeper or more items.
We want to know how the time to get data changes when we have nested lists or details.
Analyze the time complexity of the following code snippet.
GET /users/{userId}/posts
// This endpoint fetches all posts for a specific user.
// It first finds the user, then retrieves their posts list.
This code fetches posts nested under a user resource.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Retrieving each post in the user's posts list.
- How many times: Once for each post belonging to the user.
As the number of posts for a user grows, the time to fetch all posts grows proportionally.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 posts | 10 operations to retrieve posts |
| 100 posts | 100 operations to retrieve posts |
| 1000 posts | 1000 operations to retrieve posts |
Pattern observation: The operations increase directly with the number of posts.
Time Complexity: O(n)
This means the time to get all posts grows in a straight line as the number of posts grows.
[X] Wrong: "Fetching nested resources is always constant time because it's just one API call."
[OK] Correct: Even though it's one call, the server processes each nested item, so time grows with the number of nested items.
Understanding how nested resource calls scale helps you design APIs and explain performance clearly in real projects.
"What if the API also fetched comments for each post nested inside? How would the time complexity change?"
Practice
Solution
Step 1: Understand nested resources concept
Nested resources organize data by showing relationships, like a comment inside a post.Step 2: Identify the purpose of nesting
Nesting clarifies that one resource belongs to another, helping API users find related data easily.Final Answer:
To show how one resource belongs inside another resource -> Option BQuick Check:
Nested resources = parent-child relationship [OK]
- Thinking nested resources make URLs shorter
- Believing IDs are not needed in nested URLs
- Confusing unrelated data separation with nesting
Solution
Step 1: Identify parent and child in URL
The parent is 'posts' with ID 5, and the child is 'comments'. The child comes after the parent.Step 2: Check URL structure
The correct format is /parent/{parent_id}/child, so /posts/5/comments is correct.Final Answer:
/posts/5/comments -> Option CQuick Check:
Parent ID before child resource [OK]
- Placing child resource before parent
- Swapping IDs and resource names
- Using incorrect order of URL segments
/users/10/orders/3, what resource does this URL represent?Solution
Step 1: Analyze URL segments
The URL shows 'users' with ID 10, then 'orders' with ID 3 nested inside.Step 2: Understand resource relationship
This means order 3 belongs to user 10, so the URL points to that specific order.Final Answer:
Order with ID 3 belonging to user with ID 10 -> Option AQuick Check:
Nested resource = child inside parent [OK]
- Mixing up which ID belongs to user or order
- Thinking URL shows combined or merged data
- Ignoring the order of resources in URL
/projects/{projectId}/tasks/{taskId}. Which URL is incorrect to get task 7 of project 2?Solution
Step 1: Check correct nested URL format
The correct format is /projects/{projectId}/tasks/{taskId}, so project ID comes before task ID.Step 2: Identify incorrect URL
/tasks/7/projects/2 reverses the order to /tasks/7/projects/2, which breaks nesting rules.Final Answer:
/tasks/7/projects/2 -> Option DQuick Check:
Parent resource must come before child in URL [OK]
- Swapping parent and child in URL
- Adding trailing slashes incorrectly
- Using leading zeros in IDs (usually allowed but check API)
Solution
Step 1: Understand the resource hierarchy
Post is the top parent, comments belong to posts, and replies belong to comments.Step 2: Build nested URL from parent to child
The URL should list post ID, then comment ID, then reply ID in order: /posts/10/comments/2/replies/4.Final Answer:
/posts/10/comments/2/replies/4 -> Option AQuick Check:
Nested URL = parent -> child -> grandchild [OK]
- Mixing order of nested resources
- Skipping intermediate resource IDs
- Placing child before parent in URL
Ready to prove it?
Pass the test and this pattern moves unknown
