Katkat

Implement Reduce and Derive Map and Filter from It

TeslaSoftware Engineer · Technical Screen

Last updated

Explain reduce semantics, derive ordered map and filter with a reducer, and compose them over table rows while handling empty input and accumulator costs.

Explain how to implement reduce, then implement the behavior of map and filter using your reduce operation. Discuss how these primitives can be composed to process rows of a data table.

Part 1 — Define Reduce

Specify the reducer callback, accumulator, iteration order, and behavior for empty input. Explain the invariant maintained as elements are consumed.

What This Part Should Cover

An explicit initial accumulator, left-to-right evaluation under a stated baseline, callback behavior, and correct empty-input handling.

Part 2 — Derive Map and Filter

Explain how each operation can be expressed through reduce without independently traversing the input. Use an explicitly hypothetical table transformation to illustrate composition.

What This Part Should Cover

One mapped output per input element, predicate-based selection, preserved order, and careful handling of mutable accumulators and performance.

Constraints

Choose a language and state its callback and collection semantics. The original table schema and requested transformation are unspecified, so a demonstration schema must be labeled hypothetical. This is a higher-order-function implementation discussion, not a JSON console with arbitrary callback code as input.

Clarifying Questions

  • Is an initial accumulator mandatory, and is evaluation eager or lazy?
  • May callbacks mutate state or raise exceptions?
  • Should map and filter preserve input order and leave the input unchanged?

Hints

Hint 1

State the prefix invariant: After processing the first k elements, explain exactly what the accumulator represents for reduce, map, and filter.

Follow-up questions

  • When does repeatedly concatenating accumulator lists become quadratic?
  • Which properties would a reducer need for safe parallel reduction?