Bird
Raised Fist0
Rest APIprogramming~20 mins

Hierarchical resource paths in Rest API - Practice Problems & Coding Challenges

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

Consider a REST API with the following hierarchical resource paths:

/users/{userId}/posts/{postId}

If a client sends a GET request to /users/42/posts/7, what is the expected resource path matched and the extracted parameters?

AResource path: /posts/{postId}, Parameters: postId=7
BResource path: /users/{userId}/posts/{postId}, Parameters: userId=42, postId=7
CResource path: /users/{userId}, Parameters: userId=42
DResource path: /users/posts, Parameters: none
Attempts:
2 left
💡 Hint

Think about how hierarchical paths capture parameters at each level.

🧠 Conceptual
intermediate
1:30remaining
Understanding hierarchical resource path design

Why is it beneficial to design REST API resource paths hierarchically, such as /users/{userId}/posts/{postId}, instead of flat paths like /posts/{postId} only?

AIt prevents clients from accessing nested resources.
BIt reduces the number of API endpoints needed by combining unrelated resources.
CIt makes the API slower because of longer URLs.
DIt helps organize resources logically and clarifies ownership or relationships between resources.
Attempts:
2 left
💡 Hint

Think about how URLs reflect real-world relationships.

🔧 Debug
advanced
2:00remaining
Identify the error in hierarchical path matching

Given the following API route definitions:

/users/{userId}/posts/{postId}
/users/{userId}/posts
/users/all

A GET request to /users/all/posts returns a 404 error. Why?

ABecause <code>/users/all/posts</code> matches <code>/users/{userId}/posts</code>; 'all' is treated as a userId but no posts exist for it.
BBecause <code>/users/all</code> is a static path and cannot be followed by /posts.
CBecause the API does not support nested resources under static paths.
DBecause the route <code>/users/{userId}/posts</code> requires a numeric userId.
Attempts:
2 left
💡 Hint

Consider how path parameters and static segments are matched.

📝 Syntax
advanced
1:30remaining
Correct hierarchical path syntax

Which of the following is the correct syntax for defining a hierarchical resource path with two parameters in a REST API?

A/users/userId/posts/postId
B/users/:userId/posts/:postId/
C/users/{userId}/posts/{postId}
D/users/{userId}/posts/postId
Attempts:
2 left
💡 Hint

Look for the standard placeholder format for path parameters.

🚀 Application
expert
2:30remaining
Determine the number of unique resource paths

An API defines these hierarchical resource paths:

/companies/{companyId}/departments/{departmentId}/employees/{employeeId}
/companies/{companyId}/departments/{departmentId}
/companies/{companyId}

If the API has 3 companies, each with 4 departments, and each department has 10 employees, how many unique resource paths exist for /companies/{companyId}/departments/{departmentId}/employees/{employeeId}?

A120
B17
C70
D30
Attempts:
2 left
💡 Hint

Multiply the counts at each level of the hierarchy.

Practice

(1/5)
1. What does a hierarchical resource path in a REST API represent?
easy
A. A nested structure showing relationships between resources
B. A random list of unrelated resources
C. A single resource without any relation
D. A method to encrypt API data

Solution

  1. Step 1: Understand resource organization

    Hierarchical paths show how resources are connected, like folders inside folders.
  2. Step 2: Identify the purpose of hierarchical paths

    They help represent parent-child relationships clearly in the API URL.
  3. Final Answer:

    A nested structure showing relationships between resources -> Option A
  4. Quick Check:

    Hierarchical paths = nested resource relationships [OK]
Hint: Think of folders inside folders to understand hierarchy [OK]
Common Mistakes:
  • Confusing hierarchy with unrelated resource lists
  • Ignoring the relationship between resources
  • Thinking it's about data encryption
2. Which of the following is the correct syntax for a hierarchical resource path to access comments of a post with ID 10?
easy
A. /posts/10/comments
B. /comments/posts/10
C. /posts/comments/10
D. /10/posts/comments

Solution

  1. Step 1: Identify the parent resource and its ID

    The parent resource is 'posts' with ID 10, so it comes first in the path.
  2. Step 2: Append the child resource

    Comments belong to the post, so '/comments' follows '/posts/10'.
  3. Final Answer:

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

    Parent resource + ID + child resource = /posts/10/comments [OK]
Hint: Parent resource and ID come before child resource [OK]
Common Mistakes:
  • Placing child resource before parent
  • Swapping resource names and IDs
  • Using incorrect order of segments
3. Given the REST API path /users/5/orders/12/items, what resource does this path represent?
medium
A. All orders of all users
B. Order 12 of user 5
C. User 5's profile information
D. Items in order 12 of user 5

Solution

  1. 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.
  2. Step 2: Understand the full resource

    The path points to the items inside order 12 which belongs to user 5.
  3. Final Answer:

    Items in order 12 of user 5 -> Option D
  4. Quick Check:

    Path shows nested resources = items in order 12 of user 5 [OK]
Hint: Read path left to right as parent to child resources [OK]
Common Mistakes:
  • Ignoring the nested order and item relationship
  • Confusing order with user profile
  • Assuming it lists all users or orders
4. Identify the error in this hierarchical REST API path: /projects//tasks/7
medium
A. Missing task ID before 7
B. Double slash between 'projects' and 'tasks' is invalid
C. Resource names should be capitalized
D. The path should end with a slash

Solution

  1. Step 1: Check the path syntax

    The path has two slashes '//' between 'projects' and 'tasks', which is not valid in REST API paths.
  2. Step 2: Understand correct path format

    Only one slash should separate resource names and IDs to maintain proper hierarchy.
  3. Final Answer:

    Double slash between 'projects' and 'tasks' is invalid -> Option B
  4. Quick Check:

    Only single slashes separate path segments [OK]
Hint: Check for extra or missing slashes in paths [OK]
Common Mistakes:
  • Ignoring double slashes as harmless
  • Thinking resource names must be capitalized
  • Believing trailing slash is mandatory
5. You want to design a REST API path to access all reviews for a specific product variant. The product ID is 20 and the variant ID is 5. Which path correctly represents this hierarchical resource?
hard
A. /products/reviews/20/variants/5
B. /reviews/products/20/variants/5
C. /products/20/variants/5/reviews
D. /variants/5/products/20/reviews

Solution

  1. 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.
  2. Step 2: Construct the path in correct order

    The correct order is /products/20/variants/5/reviews to show this nested relationship clearly.
  3. Final Answer:

    /products/20/variants/5/reviews -> Option C
  4. Quick Check:

    Parent to child order: product -> variant -> reviews [OK]
Hint: Order path segments from general to specific resource [OK]
Common Mistakes:
  • Placing reviews before products or variants
  • Mixing order of product and variant IDs
  • Using unrelated resource order
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