0
0
Rest APIprogramming~5 mins

Avoiding verbs in URLs in Rest API - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Avoiding verbs in URLs
O(n)
Understanding Time Complexity

When designing REST APIs, the way URLs are structured affects how the server handles requests.

We want to understand how the choice of URL style impacts the number of operations the server performs.

Scenario Under Consideration

Analyze the time complexity of handling requests with URLs that avoid verbs.


GET /users/123
POST /users
PUT /users/123
DELETE /users/123
    

This code snippet shows typical REST API URLs using nouns and HTTP methods instead of verbs in the URL path.

Identify Repeating Operations

Look at what the server does when it receives these requests.

  • Primary operation: Searching or updating user data by ID.
  • How many times: Once per request, the server looks up the user or modifies data.
How Execution Grows With Input

The server work depends on how many users exist and how it finds them.

Input Size (n)Approx. Operations
10 usersAbout 10 checks to find one user
100 usersAbout 100 checks
1000 usersAbout 1000 checks

Pattern observation: The time to find or update a user grows as the number of users grows.

Final Time Complexity

Time Complexity: O(n)

This means the server work grows linearly with the number of users when searching or updating.

Common Mistake

[X] Wrong: "Using verbs in URLs makes the server faster because it knows the action upfront."

[OK] Correct: The server still needs to find or update data; the URL style does not reduce the search or update work.

Interview Connect

Understanding how URL design affects server work helps you build clear and efficient APIs, a skill valued in many projects.

Self-Check

"What if the server used an index to find users instead of searching all? How would the time complexity change?"