0
0
Rest APIprogramming~20 mins

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

Choose your learning style9 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.