A grouped count over a table of orders returns 11 rows though the business defines 14 categories — why?
answer
- count the result's rows, not the categories
- only what occurred can appear
- no rows means no key was ever formed
- a declared set is needed for a zero
basics
~20 sA grouped result carries one row per key value that actually occurred, so the three unlisted categories had no rows in this input. They are absent from the result rather than present with a zero.
solid answer
~50 sThe grouped operation — one pass that gives every row a key, runs a computation once per key, and reassembles the answers — can only emit a row for a key value it actually met. Three of the fourteen categories had no rows here, so nothing was formed for them; they are missing, not zero. A category with no rows can appear at all only if two separate things hold: the key column carries a **declared set of allowed values**, and the tool emits the *unobserved* members of that set rather than only what occurred. Designs differ on both, and several emit only the observed members even when a declared set exists. The point worth making in an interview is downstream: a missing row and a zero row read very differently to whatever consumes the result.
go deeper
Recall that a grouped result holds one row per key value that actually occurred, so a category no row used produces nothing at all rather than a zero.
Explain the two separate conditions for an unobserved category to get a row: the key column declares its allowed values, and the tool emits the unobserved members. Say that designs differ on both.
Treat the hole as a finding. Say what a consumer does with a missing row against an explicit empty one, and decide deliberately which shape the result should have before it ships.
Frame it as a standing choice: a complete-shape result costs a bigger output and a declared set somebody must maintain, while an occurred-only result is cheap and lets holes travel unnoticed into reports.
## What a grouped result is built from The **grouped operation** — one pass that gives every row a key, runs a computation once per key, and reassembles the answers into a result — has exactly one source for the rows it emits: the **grouping key** values it met while scanning the input. The key expression is evaluated for each row, rows carrying equal values are collected together, the per-key computation runs, and one output row comes back per key that was formed. A key value that no row carried was never formed, so nothing was emitted for it. The result therefore answers *what occurred*, not *what could occur*. Eleven rows out of a fourteen-category business means eleven distinct category values appeared in this input. The list of categories the business recognises lives somewhere else entirely — in a reference table, in a specification, in somebody's head — and the grouped operation never consulted it, because nothing handed it to the operation. ## Why this is not a defect Nothing failed, and "the grouping dropped three categories" is the wrong diagnosis: there were no rows to drop. Keeping four situations apart is most of the skill here, because they have different causes and different repairs: - **A declared category with no rows** — the value is legitimate but this input contains none of it. That is the case in the question. - **A combination that never occurred** — with two keys, a pair of values that no single row carried; it disappears the same silent way. - **A row whose grouping key has no value** — the row exists but carries no key, so it either forms a group of its own or leaves the computation entirely. - **A group left with no rows by an earlier step** — the rows were removed upstream and the key value went with them, so by the time the split ran there was nothing to key. Only the first two are about categories that never reached the result; the third is about rows, and the fourth happened before the grouped operation ran at all. ## The two conditions for an empty category to appear A result row for a category with no rows is not something a tool can invent out of the data. Two things must hold, and they are independent: 1. **The key column must carry a declared set of allowed values** — a list, attached to the column itself, of every value the column is permitted to take. Without that declaration the column is only the values it happens to hold, and a value that is not there is indistinguishable from a value that does not exist. 2. **The tool must emit the unobserved members of that set.** Carrying the declaration is not the same as emitting from it. Some designs emit every declared value, observed or not; some emit only what occurred even when the declaration is present; several expose it as a setting on the grouped operation. A design with no notion of a declared set cannot do it at all. Say the assumption out loud rather than guessing: *"if the key column declares its allowed values and the tool emits the unobserved ones, I get fourteen rows; otherwise I get eleven."* That sentence is the whole answer, and it is answerable without knowing which tool is in front of you. ## A missing row and a zero row are not the same downstream | What the consumer does | With the category missing | With an explicit empty row | |---|---|---| | A person reads the table | the category is not on screen, and its absence is easy to overlook | the category is visible and its emptiness is stated | | A step reads the result by position | later rows move up into positions that meant something else | positions stay stable from run to run | | Two runs are lined up side by side | the two results have different shapes and the comparison has to cope | both runs have the same shape | | A grand total is taken | unchanged — nothing was contributed either way | unchanged, for the same reason | The last row is the one candidates get wrong in both directions. A category with no rows changes **no total**; what it changes is the **shape** of the result, and everything downstream that depends on the shape. That is why the question is asked at all: the arithmetic is fine and the report is still misleading. ## What the interviewer is listening for Three things, in order. First, that you did not call it a bug — you can state the mechanism that produced eleven rows. Second, that you know a zero row is possible but conditional, and can name both conditions instead of asserting that some tool "always" fills the gap. Third, that you treat the hole as a finding rather than a cosmetic issue: before shipping the result you decide, deliberately, whether the consumer needs the complete set of categories or only the ones that occurred, and you make that shape explicit rather than inheriting whichever one the default gave you.
- What is the practical difference, for a downstream consumer, between a category that is absent and one present with a zero?Neither changes any total, so the arithmetic is identical. The difference is shape: an absent category makes the result narrower, moves every later row up if the consumer reads by position, and gives two runs different shapes. An explicit empty row keeps the shape fixed and states the emptiness where a reader can see it.
- If the key column declares its allowed values, is a row for an unobserved category guaranteed?No. Declaring the allowed values and emitting the unobserved ones are two separate behaviours. Several designs hold the declaration and still emit only the members that occurred, often with a setting to change it; designs with no declared-set concept cannot emit one at all. Check the behaviour and set it deliberately.
- Does a category with no rows ever change the grand total over the result?No. It contributes nothing to a sum and nothing to a count of rows, so every total over the result is the same whether the category is emitted or omitted. What changes is the number of rows in the result and anything keyed off that — positions, run-to-run shape, and whether a reader notices the category at all.
A sign-in sheet at the door records only the people who arrived. A register printed from the enrolment list has a line for every enrolled name, ticked or left blank. A grouped result is the sign-in sheet: you get the blank lines only if the column carries the enrolment list and the tool has been asked to print a line for every name on it.
saying these in an interview costs you the question
- Assuming every category in the reference list gets a row in the result
- Calling the shortfall a bug in the grouped operation
- Saying the three categories came back as zeros and were filtered out
- Treating an absent row and a zero row as interchangeable for a consumer
- Claiming a declared set of allowed values alone guarantees the empty rows
- Believing a category with no rows changes the grand total