Notion logoNotion
CodingFree preview

Table Aggregation with Caching Follow-ups

Given `Row(tableId, value)` rows grouped by table, implement `computeAggregations(tableId)` returning COUNT, SUM, and AVERAGE. Follow-ups: serve many aggregations on immutable tables (precompute + cache, lazy cache when not read-heavy), handle mutable tables (versioned snapshots / incremental maintenance), add MAX with efficient updates, and handle tables too large for memory. A task/rows variant asks for an `applyModification` add/delete/move API.

SWE
medium
hashmap
caching
data-structure
Frequency
Medium
Last asked
2026-04-07
Stage
phone-screen · onsite-coding

Problem

Notion models a simple "table system": a table holds rows, and each row carries an integer value. Given a tableId, compute the table's COUNT (number of rows), SUM (sum of all values), and AVERAGE.

@dataclass
class Row:
    tableId: str
    value: int

TABLE_1 = "table_1"

tables: Dict[str, List[Row]] = {
    TABLE_1: [Row(TABLE_1, 3), Row(TABLE_1, 1), Row(TABLE_1, 8)]
}

def computeAggregations(tableId: str):
    pass

The base implementation is a straightforward read-and-compute. The round is really about the follow-ups: how aggregations stay cheap when there are many of them, first on immutable, read-heavy tables and then on mutable ones.

Variants

Task / rows with modifications

The same family appears framed as tasks that own rows:

class Node {
  String taskId;
  List<Row> rows;
}
  • Part 1: given a task, compute the sum of its rows.
  • Part 2: implement applyModification(Task task1, Task task2, Row row1, Row row2):
    • task1 == null, task2 != null: add row2 to task2.
    • task1 != null, task2 == null: delete row1 from task1.
    • task1 and task2 both non-null: move row1 from task1 to task2.

This is the mutable-table follow-up made concrete: every modification has to keep per-task aggregates correct.

Hints

Pitfalls

  • The coding portion is easy; most of the signal comes from the design-style follow-ups, so budget time for them.
  • The interviewer may steer toward the approach Notion itself uses. One candidate proposed precompute-plus-cache for a read-heavy workload, was then asked what changes if it's not read-heavy, and the expected answer turned out to be a lazy cache. Ask early about the read/write mix and say which assumption you're designing for.
  • For the mutable case, version-based snapshots and incremental maintenance are both valid directions, but explain them concretely. A candidate who named both without walking through the mechanics spent a long time re-explaining.
  • The MAX follow-up is sometimes discussion-only (no code). Be ready to reason about it out loud.
  • An empty table makes sum / count divide by zero. Pick a convention (0.0 or None) and state it.
  • Update must not change count; delete must subtract the removed row's own value, not a stale one.

Talking Points

  • Derive average from count and sum: only two values are cached, so there is no risk of three separately cached statistics drifting apart.
  • State complexity for both paths: reads are O(1) after precomputation, and mutations are O(1) when applying deltas.
  • Name the empty-table convention up front, since dividing by a zero count crashes.
  • Walk through the invariant (cached count and sum match the row list) to show you understand correctness, not just code.
  • A hash map keyed by tableId is the safe general cache; an array would only do if IDs are dense integers.
  • If reads and writes can run concurrently, sum += delta is not atomic in most languages, so mention locking or atomic updates.

Follow-ups

Preparation

  • Implement computeAggregations, then add a cache keyed by tableId and decide when it's invalidated.
  • Implement applyModification for add / delete / move, keeping COUNT and SUM updated incrementally with no rescans.
  • Explain out loud why COUNT/SUM/AVERAGE update in O(1) per change but MAX doesn't when the current max is removed, and sketch a structure that handles it.
  • Rehearse a two-minute explanation of lazy vs eager caching and snapshot vs incremental maintenance.
Was this article helpful?

Comments

Sign in to join the discussion
Loading...