Bird
Raised Fist0
Rest APIprogramming~20 mins

Nested resources in Rest API - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Nested Resource Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of nested resource URL handling

Consider a REST API where /users/{userId}/posts/{postId} returns a specific post of a user. What is the output of the following request handler when called with /users/5/posts/10?

Rest API
def get_post(user_id, post_id):
    return f"User {user_id} requested post {post_id}"
A"User 5 requested post 10"
B"User 10 requested post 5"
C"Post 10 of user 5 not found"
D"Error: Invalid user or post ID"
Attempts:
2 left
💡 Hint

Look carefully at the order of parameters in the function and the URL.

🧠 Conceptual
intermediate
1:30remaining
Understanding nested resource URL structure

Which of the following URLs correctly represents a nested resource where comments belong to a specific post?

A/comments/123/posts/456
B/posts/comments/123/456
C/posts/456/comments/123
D/posts/123/456/comments
Attempts:
2 left
💡 Hint

Think about the hierarchy: comments belong to posts, so posts come first in the URL.

🔧 Debug
advanced
2:30remaining
Identify the error in nested resource route definition

Given the following Flask route definitions, which option correctly identifies the error?

@app.route('/users//posts/')
def get_post(user_id, post_id):
    return f"User {user_id} post {post_id}"

@app.route('/users//posts')
def get_posts(user_id):
    return f"Posts for user {user_id}"

@app.route('/posts/')
def get_post_global(post_id):
    return f"Post {post_id} globally"
AThere is no error; all routes are correctly defined.
BThe function get_post is missing a return statement.
CThe route '/users/<int:user_id>/posts' should be defined after '/users/<int:user_id>/posts/<int:post_id>'.
DThe route '/posts/<int:post_id>' conflicts with '/users/<int:user_id>/posts/<int:post_id>' causing ambiguity.
Attempts:
2 left
💡 Hint

Consider how Flask matches routes and if any overlap can cause confusion.

📝 Syntax
advanced
2:00remaining
Correct syntax for nested resource route in Express.js

Which of the following Express.js route definitions correctly handles a nested resource for /users/:userId/posts/:postId?

Aapp.get('/users/:userId/posts/:postId', (res, req) => { res.send(`User ${req.params.userId} Post ${req.params.postId}`); });
Bapp.get('/users/:userId/posts/:postId', (req, res) => { res.send(`User ${req.params.userId} Post ${req.params.postId}`); });
Capp.get('/users/:userId/posts/:postId', (req, res) => { res.send('User ' + req.userId + ' Post ' + req.postId); });
Dapp.get('/users/:userId/posts/:postId', (req, res) => { res.send(`User ${req.params.postId} Post ${req.params.userId}`); });
Attempts:
2 left
💡 Hint

Remember the order of parameters in Express route handlers and how to access URL parameters.

🚀 Application
expert
1:30remaining
Number of items returned by nested resource query

In a REST API, the endpoint /users/7/orders returns all orders for user 7. The database has the following data:

  • User 7 has 3 orders: IDs 101, 102, 103
  • User 8 has 2 orders: IDs 201, 202

If the API code filters orders by user ID correctly, how many items will the response contain when calling /users/7/orders?

A5
B2
C0
D3
Attempts:
2 left
💡 Hint

Count only orders belonging to user 7.

Practice

(1/5)
1. What is the main purpose of using nested resources in a REST API?
easy
A. To make URLs shorter and simpler
B. To show how one resource belongs inside another resource
C. To avoid using IDs in URLs
D. To separate unrelated data into different APIs

Solution

  1. Step 1: Understand nested resources concept

    Nested resources organize data by showing relationships, like a comment inside a post.
  2. Step 2: Identify the purpose of nesting

    Nesting clarifies that one resource belongs to another, helping API users find related data easily.
  3. Final Answer:

    To show how one resource belongs inside another resource -> Option B
  4. Quick Check:

    Nested resources = parent-child relationship [OK]
Hint: Nested URLs show parent-child links clearly [OK]
Common Mistakes:
  • Thinking nested resources make URLs shorter
  • Believing IDs are not needed in nested URLs
  • Confusing unrelated data separation with nesting
2. Which of the following is the correct nested URL format to get comments of a post with ID 5?
easy
A. /comments/5/posts
B. /comments/posts/5
C. /posts/5/comments
D. /posts/comments/5

Solution

  1. 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.
  2. Step 2: Check URL structure

    The correct format is /parent/{parent_id}/child, so /posts/5/comments is correct.
  3. Final Answer:

    /posts/5/comments -> Option C
  4. Quick Check:

    Parent ID before child resource [OK]
Hint: Parent resource and ID come before child resource in URL [OK]
Common Mistakes:
  • Placing child resource before parent
  • Swapping IDs and resource names
  • Using incorrect order of URL segments
3. Given the nested URL /users/10/orders/3, what resource does this URL represent?
medium
A. Order with ID 3 belonging to user with ID 10
B. User with ID 3 who placed order 10
C. All orders of user 10 and order 3 combined
D. User 10's profile and order 3 details merged

Solution

  1. Step 1: Analyze URL segments

    The URL shows 'users' with ID 10, then 'orders' with ID 3 nested inside.
  2. Step 2: Understand resource relationship

    This means order 3 belongs to user 10, so the URL points to that specific order.
  3. Final Answer:

    Order with ID 3 belonging to user with ID 10 -> Option A
  4. Quick Check:

    Nested resource = child inside parent [OK]
Hint: Read URL left to right: parent then child with IDs [OK]
Common Mistakes:
  • Mixing up which ID belongs to user or order
  • Thinking URL shows combined or merged data
  • Ignoring the order of resources in URL
4. You have this nested route defined: /projects/{projectId}/tasks/{taskId}. Which URL is incorrect to get task 7 of project 2?
medium
A. /projects/2/tasks/7/
B. /projects/2/tasks/7
C. /projects/2/tasks/07
D. /tasks/7/projects/2

Solution

  1. Step 1: Check correct nested URL format

    The correct format is /projects/{projectId}/tasks/{taskId}, so project ID comes before task ID.
  2. Step 2: Identify incorrect URL

    /tasks/7/projects/2 reverses the order to /tasks/7/projects/2, which breaks nesting rules.
  3. Final Answer:

    /tasks/7/projects/2 -> Option D
  4. Quick Check:

    Parent resource must come before child in URL [OK]
Hint: Parent resource always precedes child resource in nested URLs [OK]
Common Mistakes:
  • Swapping parent and child in URL
  • Adding trailing slashes incorrectly
  • Using leading zeros in IDs (usually allowed but check API)
5. You want to design a REST API for a blog where each post has multiple comments, and each comment can have multiple replies. Which nested URL correctly accesses reply 4 of comment 2 on post 10?
hard
A. /posts/10/comments/2/replies/4
B. /posts/10/replies/4/comments/2
C. /comments/2/posts/10/replies/4
D. /posts/10/comments/replies/4/2

Solution

  1. Step 1: Understand the resource hierarchy

    Post is the top parent, comments belong to posts, and replies belong to comments.
  2. 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.
  3. Final Answer:

    /posts/10/comments/2/replies/4 -> Option A
  4. Quick Check:

    Nested URL = parent -> child -> grandchild [OK]
Hint: List resources from top parent to deepest child in URL [OK]
Common Mistakes:
  • Mixing order of nested resources
  • Skipping intermediate resource IDs
  • Placing child before parent in URL
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard