Fetch paginated article data from an HTTP API and rank usable titles by comment count with deterministic tie-breaking. Handle title fallback, missing fields, invalid limits, pagination drift, duplicate records, malformed responses, timeouts, retries, rate limits, partial failures, and mocked tests.
# Fetch and Rank Articles from a Paginated API
Implement `top_articles(limit)` against a paginated HTTP endpoint. Page `p` returns JSON with `data` and `total_pages`. Each article may have `title`, `story_title`, and `num_comments`.
Use `title` when it is non-empty, otherwise use `story_title`. Ignore an article when both are missing or empty. Treat a missing or null comment count as zero. Return up to `limit` selected titles ordered by descending comment count and then ascending title. A negative or zero limit returns an empty list without making a request.
Discuss HTTP failure handling, pagination consistency, timeouts, and tests. Do not assume the endpoint always returns valid JSON or that every page succeeds.
### Constraints & Assumptions
- Page numbers begin at one.
- `total_pages` comes from the first successful page and is a positive integer.
- The same article may appear twice only if the endpoint itself is inconsistent; state whether you deduplicate and by which stable key.
### Clarifying Questions to Ask
- Is partial data acceptable if a later page fails?
- Does the API provide a stable article ID and snapshot token?
- Are retries safe and rate-limited?
- Is title comparison case-sensitive?
### What a Strong Answer Covers
- Boundary handling before network access
- A small request helper with timeout and status checks
- Validation of response shape on every page
- Correct title fallback and sorting keys
- Explicit all-or-nothing or partial-result semantics
- Bounded retries, rate-limit awareness, and useful non-secret diagnostics
- Unit tests with a mocked HTTP client
### Follow-up Questions
- How would you fetch pages concurrently while respecting rate limits?
- How would you process more articles than memory can hold?
- How would you make results reproducible while the remote data changes?
Quick Answer: Fetch paginated article data from an HTTP API and rank usable titles by comment count with deterministic tie-breaking. Handle title fallback, missing fields, invalid limits, pagination drift, duplicate records, malformed responses, timeouts, retries, rate limits, partial failures, and mocked tests.
Implement top_articles(limit) against a paginated HTTP endpoint. Page p returns JSON with data and total_pages. Each article may have title, story_title, and num_comments.
Use title when it is non-empty, otherwise use story_title. Ignore an article when both are missing or empty. Treat a missing or null comment count as zero. Return up to limit selected titles ordered by descending comment count and then ascending title. A negative or zero limit returns an empty list without making a request.
Discuss HTTP failure handling, pagination consistency, timeouts, and tests. Do not assume the endpoint always returns valid JSON or that every page succeeds.
Constraints & Assumptions
Page numbers begin at one.
total_pages
comes from the first successful page and is a positive integer.
The same article may appear twice only if the endpoint itself is inconsistent; state whether you deduplicate and by which stable key.
Clarifying Questions to Ask Guidance
Is partial data acceptable if a later page fails?
Does the API provide a stable article ID and snapshot token?
Are retries safe and rate-limited?
Is title comparison case-sensitive?
What a Strong Answer Covers Guidance
Boundary handling before network access
A small request helper with timeout and status checks
Validation of response shape on every page
Correct title fallback and sorting keys
Explicit all-or-nothing or partial-result semantics
Bounded retries, rate-limit awareness, and useful non-secret diagnostics
Unit tests with a mocked HTTP client
Follow-up Questions Guidance
How would you fetch pages concurrently while respecting rate limits?
How would you process more articles than memory can hold?
How would you make results reproducible while the remote data changes?