Fetch paginated data from two APIs
Problem: Fetch and Join Paginated Data from Multiple APIs
Implement a function, get_enriched_records(api_a_url, api_b_url), that retrieves and processes data from two distinct, paginated API endpoints.
API Endpoints
api_a_url
This endpoint returns paginated JSON data with the following structure:
{
"data": [
{"id": 1, "ref_id": "x1"},
{"id": 2, "ref_id": "x2"}
],
"next_page": 2 // Or null if no more pages
}
api_b_url
This endpoint also returns paginated JSON data, structured as follows:
{
"data": [
{"ref_id": "x1", "target_field": "value1"},
{"ref_id": "x2", "target_field": "value2"}
],
"next_page": null // Or a page number if more pages
}
Requirements
The get_enriched_records function must fulfill the following criteria:
- Full Retrieval: Fetch all available pages from both
api_a_urlandapi_b_url. - Data Joining: Join records from the two APIs based on their common
ref_idfield. - Output Format: Return a list of objects, each structured as
{ "id": <id>, "target_field": <value> }. - Order Preservation: The order of the final enriched records must correspond to the original order of records retrieved from
api_a_url. - Record Filtering: Skip any record from
api_a_urlif any of the following conditions are met:- The
ref_idfield is missing from theapi_a_urlrecord. - No matching record exists in the data retrieved from
api_b_urlfor the givenref_id. - The
target_fieldin the matchedapi_b_urlrecord isnull, an empty string, or consists solely of whitespace characters.
- The
- Robustness: Implement defensive programming to safely handle potential issues such as missing keys in JSON objects, empty data pages, or
Nonevalues without causing the function to crash. - HTTP Handling: Use standard HTTP request mechanisms. Assume that the API responses will be mocked by the test harness, so actual network calls are not the focus.
The primary evaluation criteria will be the clarity of API retrieval logic, effective pagination handling, and robust data validation, rather than advanced algorithmic complexity.
AI interview prep
Core dilemma
Efficiently aggregating and validating ordered data from multiple paginated external APIs under varying data quality conditions and potential performance constraints.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Fetch paginated data from two APIs
Problem: Fetch and Join Paginated Data from Multiple APIs
Implement a function, get_enriched_records(api_a_url, api_b_url), that retrieves and processes data from two distinct, paginated API endpoints.
API Endpoints
api_a_url
This endpoint returns paginated JSON data with the following structure:
{
"data": [
{"id": 1, "ref_id": "x1"},
{"id": 2, "ref_id": "x2"}
],
"next_page": 2 // Or null if no more pages
}
api_b_url
This endpoint also returns paginated JSON data, structured as follows:
{
"data": [
{"ref_id": "x1", "target_field": "value1"},
{"ref_id": "x2", "target_field": "value2"}
],
"next_page": null // Or a page number if more pages
}
Requirements
The get_enriched_records function must fulfill the following criteria:
- Full Retrieval: Fetch all available pages from both
api_a_urlandapi_b_url. - Data Joining: Join records from the two APIs based on their common
ref_idfield. - Output Format: Return a list of objects, each structured as
{ "id": <id>, "target_field": <value> }. - Order Preservation: The order of the final enriched records must correspond to the original order of records retrieved from
api_a_url. - Record Filtering: Skip any record from
api_a_urlif any of the following conditions are met:- The
ref_idfield is missing from theapi_a_urlrecord. - No matching record exists in the data retrieved from
api_b_urlfor the givenref_id. - The
target_fieldin the matchedapi_b_urlrecord isnull, an empty string, or consists solely of whitespace characters.
- The
- Robustness: Implement defensive programming to safely handle potential issues such as missing keys in JSON objects, empty data pages, or
Nonevalues without causing the function to crash. - HTTP Handling: Use standard HTTP request mechanisms. Assume that the API responses will be mocked by the test harness, so actual network calls are not the focus.
The primary evaluation criteria will be the clarity of API retrieval logic, effective pagination handling, and robust data validation, rather than advanced algorithmic complexity.