skip to content

What distinguishes a selection that is a window onto the original's bytes from one holding storage of its own?

level: juniorimportance: should knowfreq 58%

answer

  1. one selection, two answers
  2. ask who owns the bytes
  3. identical values, different lifetimes
  4. nothing allocated for the values
  5. a view against a copy

basics

~20 s

A window reads the original's storage: no bytes were allocated for the values, and the original cannot be released while the window lives. Storage of its own holds freshly allocated bytes and is independent of the source.

solid answer

~50 s

A selection answers two questions at once: which rows you asked for, and where the values you were handed live. A **window onto the same bytes** holds no values of its own - it holds a reference to the original's storage plus a description of which part to read, so producing it allocates nothing and the original's buffer stays alive as long as the window does. **Storage of its own** means bytes were allocated for this result and the selected values were written into them, at a cost proportional to the size of the result, after which the result no longer depends on the original for anything. The values you read out are identical either way, so the output on screen never tells you which one you got. The interview words for the two are *a view* and *a copy*.

go deeper

for a junior

Recall the two outcomes and name them: one reads the original's storage, the other holds bytes allocated for it. The values are the same either way.

for a middle

Explain the mechanics: a window carries a reference plus a description of where to start, how many and how far apart, so it allocates nothing and keeps the source resident.

for a senior

Show the production consequence: a long-lived small result taken as a window pins a large source, and the fix is to materialise it deliberately at the boundary where the source should go away.

for a principal

Frame it as an interface question - what a function returning a selection promises about its caller's memory, and whether that promise is stated or inherited from whichever tool happens to be underneath.

## Two answers to one selection Every selection answers two questions at the same time. The first is **which rows or columns you asked for**, and that is the one people read off the screen. The second is **where the values you were handed actually live** - in the original's storage, or in bytes allocated for this result. The second answer is invisible in the output and decides how the result behaves for the rest of its life. A **window onto the same bytes** is a result that holds no values of its own. It holds a reference to the original's storage, plus a description of which part of it to read: where to start, how many values, how far apart they sit. Nothing was allocated for the values, and producing it costs roughly the same whether it covers ten rows or ten million. **Storage of its own** is the other outcome. Bytes were allocated for this result and the selected values were written into them. Producing it costs work and memory proportional to the size of the result, and the result then depends on the original for nothing at all. The borrowed words you will hear in an interview are *a view* and *a copy*. They are worth knowing because everyone uses them, and worth unpacking because *copy* is used for at least three different things: fresh storage holding the values, a duplicate of the container whose cells still point at the same underlying objects, and - under designs that defer the duplicate until something is written - a duplicate that has been promised while no bytes have moved yet. ## What the two outcomes differ in | | window onto the same bytes | storage of its own | |---|---|---| | bytes allocated for the values | none | proportional to the result | | work to produce | roughly constant | proportional to the result | | holds the original's buffer alive | yes, while the window lives | no | | survives the original going away | no | yes | | values you read out | identical | identical | The last row is the awkward one and it is the whole reason this is an interview question. The numbers are the same either way, so printing the result, checking its row count or looking at its labels tells you nothing about ownership. Ownership is a property of the object you were handed, not of the values inside it. ## What does not decide which one you got - **How small the result looks.** Ten rows out of a million can be a window; a selection that keeps nearly everything can be freshly allocated storage. - **Whether the operation felt instant.** A window is cheap to produce precisely because it allocated nothing, but a small gather is also cheap, so speed is a weak signal rather than a proof. - **Which verb you happened to write.** Two surfaces that read almost identically in source can differ in what they return, and the same text can differ between tools. - **What you named the variable, or whether you assigned it at all.** ## What actually decides it Two things, in this order: 1. **The shape of the selection.** Only a selection describable as a start, a count and a step can be expressed as a window, because that description is all the result needs to carry. A selection of positions that form no regular pattern, or one driven by a full-length column of outcomes, has to collect its values somewhere - there is no short description of where they sit. 2. **What the design chose.** Being describable as a stride makes a window *possible*, not certain. Some designs duplicate eagerly and hand back storage of its own whatever the shape. Designs that are immutable by default return a new handle that shares every buffer the step did not touch. Designs that defer the duplicate until something is written hand back a logical duplicate and allocate only when one side is modified. So the identical expression can answer differently in two tools of the same family. ## Why the distinction earns its place Because the values are identical, the difference shows up only in **lifetime and footprint**. A window keeps the original's buffer resident: as long as the small result lives, the large thing it came from cannot be released. Storage of its own has the opposite profile - it paid for its bytes up front and lets the source go. Neither is the better outcome in general; they are different trades between allocation now and retention later, and the mistake is not picking the wrong one but not knowing which one you have. ## How to answer this in an interview Define both in terms of **ownership of bytes**, not in terms of syntax: one reads the original's storage, the other holds storage allocated for it. Then say the consequence that follows - a window cannot outlive its source cheaply, because it keeps the source alive - and finish by noting that which one you get depends on the shape of the selection and on the design, so it is something to establish rather than assume.

  • If the values are identical either way, what observable difference is left?
    Lifetime and allocation. A window allocated nothing for the values and keeps the original's buffer resident for as long as it lives; storage of its own paid for its bytes when it was produced and lets the source be released once nothing else refers to it. Same numbers, different memory behaviour.
  • Why is the word copy ambiguous enough to be worth avoiding?
    It names three different situations: fresh storage holding the values, a duplicate of the container whose cells still point at the same underlying objects, and a duplicate that has been promised under a design that defers allocation until something is written. Saying storage of its own fixes which one you mean.

saying these in an interview costs you the question

  • Says a small result must mean a small footprint
  • Thinks printing the values reveals which one you got
  • Claims every selection allocates fresh storage
  • Treats ownership as decided by the verb you wrote
  • Uses copy for three different things without distinguishing them