skip to content

An optimized tag filter is claimed to return exactly the documents the brute-force one does — what does an argument by mutual inclusion give you that spot-checks do not?

level: seniorimportance: nice to knowfreq 26%

answer

  1. sampling checks examples, not the claim
  2. equality is two containments
  3. take an arbitrary matching document
  4. one direction is often all you need
  5. an empty result satisfies one side

basics

~20 s

Coverage of every input rather than the sampled ones. Two sets are equal exactly when each contains the other, so arguing both directions over an arbitrary document rules out both failure modes — missing results and extra ones — while a spot-check only shows the cases you happened to pick.

solid answer

~50 s

Set equality decomposes into two containments: `X = Y` exactly when `X ⊆ Y` and `Y ⊆ X`. Arguing each direction means taking an **arbitrary** document that the one filter returns and showing the other must return it too, which covers every possible corpus rather than the handful in a fixture. The two directions also catch different bugs and are worth naming separately: `optimized ⊆ brute` says the optimization never invents a result, and `brute ⊆ optimized` says it never loses one. In practice one direction is usually easy and the other is where the counterexample lives — an empty label, a document carrying every label, a label added after the index was built. That asymmetry is the payoff: a failed direction hands you the *class* of inputs that breaks the rewrite, not a single failing row. Note also that `⊆` allows equality while a proper subset `⊂` requires the sets to differ, so 'is a subset of' is never on its own evidence that two filters disagree.

go deeper

for a junior

Know that two sets are equal exactly when each is a subset of the other, and that a subset may equal its container while a proper subset may not.

for a middle

Argue over an arbitrary element rather than examples, and say which bug each direction excludes: one rules out invented results, the other rules out lost ones.

for a senior

Use the stalled direction as the deliverable: the step where the argument fails names the class of inputs that breaks the rewrite, which is worth more than one failing fixture row.

for a principal

Specify the relation the system actually needs. Permission filters and cheap pre-filters are deliberately one-directional, and writing them as equalities hides the safety property you meant to guarantee.

## Subset, proper subset, equality Three relations, routinely blurred: - `X ⊆ Y` — **subset**: every member of `X` is a member of `Y`. It permits `X = Y`. - `X ⊂ Y` — **proper subset**: `X ⊆ Y` and `X ≠ Y`, so `Y` has at least one member `X` lacks. (Some authors use `⊂` for any subset, so in writing that matters, say which you mean or use `⊆` and `⊊`.) - `X = Y` — equality, which by definition means the two have exactly the same members. The bridge is the one that does the work here: **`X = Y` if and only if `X ⊆ Y` and `Y ⊆ X`.** Equality is not a separate thing to check; it is two containments. ## What each direction rules out For an optimized filter `O` and the brute-force filter `B` over the same corpus: | Direction | Plain reading | The bug it excludes | |---|---|---| | `O ⊆ B` | the fast one invents nothing | false positives: documents returned that do not match | | `B ⊆ O` | the fast one loses nothing | false negatives: matching documents silently missing | The second is the one that hurts in production, because a missing row looks like an empty state and nobody files a ticket. Naming the directions separately is therefore not pedantry: it tells you which risk you have actually retired. ## Why an arbitrary element beats a sample The argument shape is always the same: *let `d` be any document the first filter returns; here is why the second must return it too*. Because `d` was arbitrary — nothing about it was assumed except that it satisfied the condition — the conclusion holds for every document in every corpus, including corpora that do not exist yet. A spot-check does the opposite. It confirms the claim on the documents in the fixture and says nothing about the rest. The inputs that break a filter rewrite are rarely the ones a person types into a fixture: - a filter naming a label **no document carries** (an empty operand), - a document carrying **every** label in the taxonomy, - the **empty selection**, where a union is empty but an intersection is the whole universe, - a label created after the accelerating structure was built, - a document appearing in two operands where the fast path assumed disjointness. Each is a class of input, not a single row, which is exactly what the containment argument quantifies over and a sample does not. ## The failure is the useful part Attempt the direction `B ⊆ O` and follow it honestly: take a document the brute-force filter returns, and try to show the optimized path reaches it. When the argument stalls, the step where it stalls names the condition the fast path relies on — 'this only works if every label in the request has an index entry', say. That condition is now either a precondition to enforce or a bug to fix, and it came with a description of *all* the inputs that violate it. A failing test gives you one row and leaves the generalisation to you. ## Where a one-directional claim is what you actually want Equality is not always the goal, and asking for the wrong relation is its own defect. A permission-aware filter must never return more than the unrestricted one: the requirement is `permitted ⊆ unrestricted`, and demanding equality would defeat the point of the restriction. Similarly a cheap pre-filter used to narrow a candidate list must be a **superset** of the true answer — it may over-return, and a later exact pass removes the extras, but it must never drop a real match. Both are containments deliberately held in one direction only, and stating them that way is what makes them testable. ## Two traps worth naming First, the **vacuous direction**. The empty set is a subset of every set, so a rewrite that always returns nothing satisfies `O ⊆ B` perfectly. A one-directional argument that looks easy is often easy because it is vacuous; the other direction is where the content is. Second, **equality of results is not equality of behaviour**. Two filters can return the same set while differing in ordering, in paging, in cost, or in what they do when the taxonomy is mid-migration. Set equality is a statement about membership only, and claiming more from it than that is how a correct rewrite still breaks a screen. ## What an interviewer is listening for That you decompose equality into two containments and can say what each one buys; that you reach for an arbitrary element rather than examples; that you name the input classes at the edges; and that you recognise the cases where a single direction is the real requirement. Nobody wants a written proof at a whiteboard — they want the shape of the argument and the counterexample class it produces when it fails.

  • Which of the two directions usually matters more in production, and why?
    That the fast filter loses nothing. Extra results are visible and get reported; missing results look like an ordinary empty or short page and nobody notices. So the direction saying every brute-force match is also reached by the optimized path is the one that retires the expensive risk, and it is usually the harder of the two to argue.
  • Why is 'the optimized filter returns a subset of the brute-force one' a weak claim on its own?
    Because the empty set is a subset of every set, so a rewrite that returns nothing at all satisfies it. A one-directional claim excludes false positives only; without the other direction nothing rules out silently dropping every match. An easy direction is often easy because it is close to vacuous.
  • Where is one-directional containment the actual requirement rather than a weaker claim?
    Wherever over- or under-returning is deliberately asymmetric. A permission-aware filter must return a subset of the unrestricted result, never more. A cheap candidate pre-filter must return a superset of the true matches, so a later exact pass can remove extras but nothing real was dropped. Demanding equality in either case would be the wrong specification.
  • Does equal result sets mean the two filters are interchangeable?
    No. Set equality is a statement about membership only. Two filters returning the same documents can differ in ordering, paging behaviour, cost, and how they behave while the taxonomy is being migrated. A rewrite can be provably equal on results and still break a screen that depended on one of those, so the claim has to be stated for what it covers.

saying these in an interview costs you the question

  • Calls two filters equal after checking a handful of documents.
  • Uses subset and proper subset as if they meant the same thing.
  • Shows one containment and declares the two result sets identical.
  • Forgets the empty set is a subset of every set.
  • Assumes equal result sets imply identical ordering and paging.
  • Demands equality where a one-directional containment is the requirement.