Bird
Raised Fist0
Rest APIprogramming~20 mins

API analytics and usage metrics in Rest API - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
API Analytics Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
API Request Count Calculation
Given the following API usage log data, what is the total number of requests made to the API?
Rest API
api_logs = [
  {"endpoint": "/users", "method": "GET"},
  {"endpoint": "/users", "method": "POST"},
  {"endpoint": "/orders", "method": "GET"},
  {"endpoint": "/users", "method": "GET"},
  {"endpoint": "/orders", "method": "POST"}
]
total_requests = len(api_logs)
print(total_requests)
A5
B2
C4
D3
Attempts:
2 left
💡 Hint
Count each dictionary entry as one request.
🧠 Conceptual
intermediate
1:30remaining
Understanding API Rate Limiting
Which of the following best describes the purpose of API rate limiting?
ATo cache API responses for faster access
BTo encrypt API data for secure transmission
CTo log all API errors for debugging
DTo restrict the number of API calls a client can make in a given time to prevent overload
Attempts:
2 left
💡 Hint
Think about controlling traffic to the API.
❓ Predict Output
advanced
2:00remaining
Calculating Average Response Time
What is the output of the following code that calculates the average response time from API metrics?
Rest API
response_times = [120, 150, 100, 130, 110]
avg_response_time = sum(response_times) / len(response_times)
print(round(avg_response_time))
A122.0
B122.5
C122
D121
Attempts:
2 left
💡 Hint
Sum all times and divide by count, then round to nearest integer.
🔧 Debug
advanced
2:30remaining
Identify the Error in API Usage Data Aggregation
What error will the following code raise when trying to aggregate API calls by endpoint?
Rest API
api_calls = [
  {"endpoint": "/login", "count": 10},
  {"endpoint": "/logout", "count": 5},
  {"endpoint": "/login", "count": 7}
]

aggregated = {}
for call in api_calls:
    aggregated[call["endpoint"]] += call["count"]
print(aggregated)
AKeyError
BTypeError
CSyntaxError
DNo error, outputs {'/login': 17, '/logout': 5}
Attempts:
2 left
💡 Hint
Check if dictionary keys exist before adding.
🚀 Application
expert
3:00remaining
Determine the Number of Unique API Users
Given the following API usage data, how many unique users accessed the API?
Rest API
api_usage = [
  {"user_id": "u1", "endpoint": "/data"},
  {"user_id": "u2", "endpoint": "/data"},
  {"user_id": "u1", "endpoint": "/status"},
  {"user_id": "u3", "endpoint": "/data"},
  {"user_id": "u2", "endpoint": "/status"}
]
unique_users = len(set(entry["user_id"] for entry in api_usage))
print(unique_users)
A2
B3
C5
D4
Attempts:
2 left
💡 Hint
Count distinct user_id values.

Practice

(1/5)
1. What is the main purpose of API analytics in REST APIs?
easy
A. To generate API documentation
B. To create new API endpoints automatically
C. To encrypt API data during transmission
D. To track how the API is used and find issues

Solution

  1. Step 1: Understand API analytics role

    API analytics collect data on API usage and performance.
  2. Step 2: Identify main goal

    The main goal is to track usage and detect problems.
  3. Final Answer:

    To track how the API is used and find issues -> Option D
  4. Quick Check:

    API analytics = usage tracking [OK]
Hint: API analytics = usage + problem detection [OK]
Common Mistakes:
  • Confusing analytics with encryption
  • Thinking analytics create endpoints
  • Assuming analytics generate docs
2. Which of the following is the correct way to request API usage metrics grouped by date using query parameters?
easy
A. POST /metrics group_by=date
B. GET /metrics/date
C. GET /metrics?group_by=date
D. PUT /metrics?date=group_by

Solution

  1. Step 1: Identify HTTP method for data retrieval

    GET is used to request data without changing it.
  2. Step 2: Check query parameter syntax

    Query parameters follow ?key=value format, so ?group_by=date is correct.
  3. Final Answer:

    GET /metrics?group_by=date -> Option C
  4. Quick Check:

    GET + ?group_by=date = correct syntax [OK]
Hint: Use GET with ?key=value for queries [OK]
Common Mistakes:
  • Using POST instead of GET for data retrieval
  • Incorrect query parameter placement
  • Confusing URL path with query parameters
3. Given this API call: GET /api/usage?start=2024-01-01&end=2024-01-07&group_by=endpoint, what is the expected output?
medium
A. Usage data grouped by API endpoint from Jan 1 to Jan 7, 2024
B. Error because 'group_by' is not a valid parameter
C. Data for all endpoints without grouping
D. Usage data only for Jan 7, 2024

Solution

  1. Step 1: Analyze query parameters

    start and end define the date range; group_by=endpoint means grouping results by endpoint.
  2. Step 2: Determine output meaning

    The API returns usage data grouped by endpoint for the specified date range.
  3. Final Answer:

    Usage data grouped by API endpoint from Jan 1 to Jan 7, 2024 -> Option A
  4. Quick Check:

    start/end + group_by=endpoint = grouped usage data [OK]
Hint: Check query keys: start, end, group_by for output clues [OK]
Common Mistakes:
  • Ignoring group_by parameter effect
  • Assuming single date instead of range
  • Thinking group_by is invalid
4. You wrote this API call to get usage metrics but get a 400 Bad Request error:
GET /metrics?start_date=2024-01-01&end_date=2024-01-07&groupby=endpoint
What is the likely cause?
medium
A. Incorrect query parameter names; should be 'start' and 'end' and 'group_by'
B. GET method cannot have query parameters
C. Missing API key in the URL
D. Using 'groupby' instead of 'groupby_endpoint'

Solution

  1. Step 1: Check query parameter names

    The API expects 'start', 'end', and 'group_by' parameters, not 'start_date', 'end_date', or 'groupby'.
  2. Step 2: Understand error cause

    Wrong parameter names cause the API to reject the request with 400 Bad Request.
  3. Final Answer:

    Incorrect query parameter names; should be 'start' and 'end' and 'group_by' -> Option A
  4. Quick Check:

    Parameter names must match API spec [OK]
Hint: Match query parameter names exactly as API docs say [OK]
Common Mistakes:
  • Using wrong parameter names
  • Thinking GET can't have query parameters
  • Assuming missing API key causes 400 error
5. You want to build a dashboard showing daily API call counts and average response times grouped by endpoint for the last 30 days. Which API call best fits this need?
hard
A. GET /api/metrics/daily?group=endpoint&fields=count,response_time_avg
B. GET /api/metrics?start=2024-05-01&end=2024-05-30&group_by=endpoint&metrics=count,response_time_avg
C. POST /api/metrics with JSON body {"group_by":"endpoint","metrics":["count","response_time_avg"]}
D. GET /api/metrics?date_range=last30days&group_by=endpoint

Solution

  1. Step 1: Identify required parameters

    We need a date range (last 30 days), grouping by endpoint, and metrics count and average response time.
  2. Step 2: Match API call to requirements

    GET /api/metrics?start=2024-05-01&end=2024-05-30&group_by=endpoint&metrics=count,response_time_avg uses GET with start/end dates, group_by=endpoint, and metrics=count,response_time_avg which fits perfectly.
  3. Step 3: Check other options

    POST /api/metrics with JSON body {"group_by":"endpoint","metrics":["count","response_time_avg"]} uses POST which is unusual for fetching metrics; A uses different parameter names not standard; D lacks metrics selection.
  4. Final Answer:

    GET /api/metrics?start=2024-05-01&end=2024-05-30&group_by=endpoint&metrics=count,response_time_avg -> Option B
  5. Quick Check:

    Date range + group_by + metrics = GET /api/metrics?start=2024-05-01&end=2024-05-30&group_by=endpoint&metrics=count,response_time_avg [OK]
Hint: Use GET with start/end, group_by, and metrics parameters [OK]
Common Mistakes:
  • Using POST instead of GET for data retrieval
  • Missing metrics parameter
  • Using incorrect parameter names
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