Coding questions · Reevo
Fetch and aggregate paginated team data via API
This question evaluates API integration, pagination handling, robust error handling (retries/backoff and rate-limit awareness), data aggregation, sorting and top‑N selection skills within the Data Manipulation (SQL/Python) domain.
Implement a function getTopTeams(baseUrl, n) that calls a paginated HTTP API returning a JSON array of team objects on each page: [{"name": string, "wins": integer}]. Pagination is controlled by appending &page=<number> to baseUrl (pages start at 1 and continue until an empty or missing page is encountered). Retrieve and merge all pages, sort teams by wins descending then name ascending, and return the top n team names. Handle pagination, transient network errors (with retries/backoff), rate limits, and malformed records.
Get Top N Teams from Paginated API Data
You are given a table team_pages that stores the merged results of calling a paginated HTTP API. Each row represents a team returned on a particular page of the API response.
The table includes some malformed records:
- Rows where team_name IS NULL
- Rows where wins IS NULL
- Rows where wins is negative (wins < 0)
Using this table, write a SQL query that returns the top 3 team names based on these rules:
- Consider only valid records (exclude malformed rows as defined above).
- Sort teams by wins in descending order.
- For teams with the same number of wins, sort by team_name in ascending (alphabetical) order.
- Return only the top 3 team names.
Your output should be a single column named team_name containing the names of the top 3 teams in the correct order.
Table team_pages
| column | type | constraints |
|---|---|---|
| page_number | INT | NOT NULL |
| team_name | VARCHAR(100) | |
| wins | INT |
Sample data:
| page_number | team_name | wins |
|---|---|---|
| 1 | Sharks | 10 |
| 1 | Bears | 8 |
| 1 | Dragons | 15 |
| 2 | Cougars | 12 |
| 2 | NULL | 20 |
| 2 | Eagles | NULL |
| 2 | Bulls | -5 |
| 3 | Ants | 15 |
| 3 | Lions | 10 |
Expected output
Single-column result listing the top 3 valid teams, ordered by wins descending and then team_name ascending. Malformed rows (NULL or negative wins, NULL team_name) are excluded.
| team_name |
|---|
| Ants |
| Dragons |
| Cougars |
Hints
- Filter out malformed rows using conditions on team_name and wins in the WHERE clause.
- Order by wins DESC and team_name ASC, then limit the number of rows to 3.