KatInterview

Fix Summary Metrics That Ignore Active Filters

System DesignModel answer
MediumModel answerSystem Designdebuggingdata-lineageapi-design

Last updated

Diagnose and fix an API endpoint where row results respect filters but the computed summary still reflects the full unfiltered dataset.

Fix Summary Metrics That Ignore Active Filters

A dashboard endpoint accepts filter parameters and returns two things: a list of matching rows and a summary block (totals, averages, counts). Users have noticed the rows respect whatever filters they apply, but the summary numbers always reflect the full dataset as if no filters were active.

A stripped-down version of the endpoint logic looks like this:

rows = repository.load_rows()
metrics = summarize(rows)
rows = apply_filters(rows, request.filters)
return {"rows": rows, "metrics": metrics}

Your job has three parts:

  1. Diagnose — Identify the exact defect and explain why it produces the observed symptom.
  2. Fix — Propose the smallest safe correction. Address the additional wrinkle that real endpoints typically apply pagination after filtering; the summary must cover all matched rows, not just the current page.
  3. Test — Design tests at both the unit level and the endpoint level that would catch this class of bug if it were reintroduced.

Context and constraints

  • summarize produces correct output for whatever rows it receives; the function itself is not broken.
  • Filters determine which rows should contribute to the summary. The summary is not intended to be a global/unfiltered figure.
  • The shape of the API response must stay the same — callers expect both rows and metrics in the same payload.
  • Authorization and tenant isolation may be enforced separately from user-facing filters; your fix must not accidentally bypass those controls.
  • You are working in an unfamiliar Python web framework, and the frontend that surfaces the numbers is JavaScript. Describe how you would trace the bug end-to-end under those conditions.

Clarifying questions worth raising

  • Are all summary values expected to reflect the filtered population, or do some fields intentionally show global figures?
  • Is authorization/tenant scoping handled as a filter in apply_filters, or is it enforced earlier in the stack before load_rows is called?
  • Should the summary describe every row that matches the filters, or only the rows on the current page?
  • Can filtering be pushed into the storage query rather than done in Python, and what consistency guarantees exist between the two result sets?

Hints

Hint 1

Start by tracing the data lineage of the wrong number backward from what the user sees: browser → network response → endpoint logic → data layer. Determine the earliest point where the filtered and unfiltered populations diverge.

Hint 2

Write a test fixture where the filtered and unfiltered summaries are necessarily different — e.g., two rows where one is excluded by the filter and contributes a non-zero amount to the aggregate. If your fix is correct the test will fail before and pass after.

Hint 3

Pagination is a separate concern from filtering. Make sure your fix computes the summary over all matched rows first, then slices the page, rather than computing the summary over only the page that happens to be returned.