skip to content

A wide table is folded back to one row per measurement and then widened again. When does that round trip return the original?

level: middleimportance: should knowfreq 50%

answer

  1. not automatically reversible
  2. two preconditions, plus a caveat about order
  3. every pair present exactly once
  4. the complete grid of key-and-header pairs

basics

~20 s

Only on a complete grid with each key-and-header pair present exactly once, and only for a caller indifferent to the order of columns and rows. Where the grid has holes, the two conversions disagree about cells the data never held.

solid answer

~50 s

The two conversions are inverses in shape, not in effect. Identity holds when three conditions are met: every combination of row key and header is present (**the complete grid**), each such pair occurs exactly once, and you do not care about the order of the columns or the rows. Break the first and the disagreement appears: a widening over a sparse key space has to create cells the data never held, and **whether a fold back emits those invented cells as rows or drops them varies between tools** — so the round trip may hand you extra rows, or may lose an entire header whose column was invented from end to end. Order fails separately and quietly: a widening derives its header order from the data, so even a perfect round trip can come back with the columns rearranged.

go deeper

for a junior

Know that the two conversions are a matched pair but not a guarantee: going one way and back does not automatically hand you the table you started with.

for a middle

State the conditions unprompted — a complete grid, each key-and-header pair exactly once, and no reliance on column or row order — and say what fails when each is missing.

for a senior

Recognise the round trip as a weak check that passes precisely where nothing was at risk, and complete the grid deliberately when identity actually matters.

for a principal

Decide what your team means by two tables being the same, since cells, headers, order and invented cells are four separate claims and tools differ on the last one.

## What the round trip actually is **A widening** turns one column's distinct values into new column headers, filled from a second column. **Folding back** turns a set of headers into one column holding the measure's name and one holding its number. Run one and then the other and you appear to be back where you started. People lean on that: as a way of checking a reshape, as a way of getting a table into the shape a step wants and then out again, as an argument that a conversion is harmless. It is a conditional identity, and the conditions are worth being able to state without being prompted. ## The three conditions 1. **Uniqueness.** Each pair of row key and header occurs exactly once in the long form. Before widening anything, count the distinct key-and-header pairs and compare that count with the row count; if they differ, you are not in the identity case at all and you should find out why before going further. 2. **A complete grid.** **The complete grid of key-and-header pairs** is every combination of a row key with every header, whether or not the data actually holds it. When the input is already complete, a widening creates nothing and a fold back has nothing extra to decide about. 3. **Indifference to order.** A widening derives its header order from the data, not from your code, and the rebuilt row order follows whatever the fold back emitted. If either order carried meaning, the round trip has already changed the table even when every cell matches. | condition | if it holds | if it does not | |---|---|---| | each key-and-header pair occurs once | the pairing is unambiguous | you are outside the identity case entirely | | the grid is complete | nothing is invented | invented cells appear, and their fate on the way back varies | | order does not matter | cells match, and that is enough | the table differs even with every cell equal | ## Where an incomplete grid breaks it Take three subjects and three measures: nine combinations, but only seven measurements were ever taken. - **Long, then wide, then long.** Widening the seven rows produces a three-by-three rectangle in which two cells had to be invented. Folding that back gives you seven rows if the tool drops invented cells, or nine if it emits them — and **which one it does is a default that varies by design**. Only the first is the table you started with. - **Wide, then long, then wide.** Starting from the rectangle, the fold back gives seven or nine rows, and widening either one rebuilds a three-by-three rectangle. Cell for cell this direction is more forgiving. But it has a failure the other does not: if one header's column was invented from end to end — every single cell of it created by the widening — and the fold back drops invented cells, that measure has no rows to carry it and the header **disappears** from the rebuilt result. The table quietly gets narrower. That asymmetry is the part candidates miss. The two directions are not equally safe, and neither is safe unconditionally. ## Order is not part of the deal Even in the clean case, two things you might have cared about are not preserved: - **Header order**, because the widening took it from the data rather than from your code. A column arrangement you had deliberately arranged comes back in whatever order this pass produced. - **Row order**, because the fold back emits rows in its own order and the widening groups them into row keys in its own. If either mattered, restore it explicitly. Comparing two tables after a round trip is a comparison you should make on a sorted key, not position by position. ## What to do instead of assuming - **Do not use a round trip as a validation of a reshape.** It can only ever confirm the case where nothing was at risk; the sparse case, which is the one worth checking, is exactly the one where the round trip legitimately differs. - **Complete the grid first** if you genuinely need identity: build every combination of row key and header explicitly, so the widening has nothing to invent and the fold back has nothing to decide. - **Say what you mean by identical.** Same cells, same headers, same order, and same treatment of cells the data never held are four different claims, and a round trip satisfies different subsets of them depending on the tool. - **Convert once at a boundary** rather than back and forth between steps. A pipeline that changes layout repeatedly accumulates exactly these conditional differences, and nobody reading it can see where.

  • What is the complete grid, and why does the round trip depend on it?
    It is every combination of a row key with a header, whether or not the data holds it. On a complete grid, a widening creates nothing and a fold back has nothing extra to decide. Where combinations are missing, the widening has to invent cells, and whether the fold back emits those as rows or drops them varies — that decision is what makes the trip lossy.
  • Does a round trip preserve the order of the columns and the rows?
    No, and not even in the clean case. A widening derives header order from the data, so the rebuilt columns come back in whatever order this pass produced, and row order follows the fold back's output. Compare two tables after a round trip on a sorted key rather than by position, and restore an order you cared about explicitly.
  • Can a round trip lose a whole column?
    Yes, going wide then long then wide. If one header's column consists entirely of cells the widening invented, and the fold back drops invented cells, that measure has no rows left to carry it and the header is simply not rebuilt. The result is narrower than the original and nothing reports it.

saying these in an interview costs you the question

  • Calling the two conversions exact inverses with no conditions
  • Assuming a sparse table survives the round trip unchanged
  • Expecting the column order to come back as it was
  • Using a round trip to prove a reshape was harmless
  • Assuming every tool folds an invented cell back into a row