cswork / libraryOpen practice workspace

Coding questions · Armada

Fetch paginated data from two APIs

This question evaluates a candidate's competency in robust API retrieval, pagination handling, defensive data validation, and joining records across endpoints while preserving the order of source records and safely handling missing or malformed fields.

Write a function get_enriched_records(api_a_url, api_b_url).

api_a_url returns paginated JSON in the form:

{
  "data": [
    {"id": 1, "ref_id": "x1"},
    {"id": 2, "ref_id": "x2"}
  ],
  "next_page": 2
}

api_b_url returns paginated JSON in the form:

{
  "data": [
    {"ref_id": "x1", "target_field": "value1"},
    {"ref_id": "x2", "target_field": "value2"}
  ],
  "next_page": null
}

Implement the function so that it:

  1. Retrieves all pages from both APIs.
  2. Joins the records from the two endpoints using ref_id.
  3. Returns a list of objects in the form { "id": <id>, "target_field": <value> }.
  4. Preserves the order of records from api_a_url.
  5. Skips any record if:
    • ref_id is missing,
    • the matching record does not exist in the second API,
    • target_field is null, an empty string, or whitespace-only.
  6. Handles missing keys, empty pages, and None values safely without crashing.
  7. Uses normal HTTP request handling; assume the test harness will mock the API responses.

The focus is not on advanced algorithms, but on clean API retrieval logic, pagination handling, and defensive data validation.

Join Paginated API Records

In this deterministic harness, each API is represented by a list of page dictionaries. Read every page, join on ref_id, preserve API A order, and skip invalid target fields.

Constraints

  • Pages are already supplied in pagination order.
  • target_field values that are null, empty, or whitespace-only are skipped.
  • For duplicate ref_id rows in API B, the first valid value is used.

Examples

Example 1

Input: ([{"data":[{"id":1,"ref_id":"x1"},{"id":2,"ref_id":"x2"}],"next_page":2},{"data":[{"id":3,"ref_id":"x3"}],"next_page":None}], [{"data":[{"ref_id":"x1","target_field":"v1"},{"ref_id":"x2","target_field":"v2"}],"next_page":None},{"data":[{"ref_id":"x3","target_field":""}],"next_page":None}])
Output: [{'id': 1, 'target_field': 'v1'}, {'id': 2, 'target_field': 'v2'}]

Explanation: Records are joined in API A order and blank target fields are skipped.

Example 2

Input: ([{"data":[{"id":1},{"id":2,"ref_id":"z"}]}], [{"data":[{"ref_id":"z","target_field":" ok "}]}])
Output: [{'id': 2, 'target_field': ' ok '}]

Explanation: Missing ref_id rows are skipped safely.

Hints

  1. Flatten the pages defensively.
  2. Build a ref_id lookup from API B before scanning API A.