PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates a candidate's ability to perform data aggregation and manipulate basic data structures by grouping records by key and combining numeric values.

  • easy
  • Notion
  • Coding & Algorithms
  • Software Engineer

Implement Table Aggregation

Company: Notion

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Technical Screen

You are given a table represented as a list of rows. Each row contains: - `key`: a string identifying a group - `value`: an integer Write a function that aggregates the table by `key` and returns, for each distinct key, the sum of all corresponding `value`s. Example: Input rows: - `(A, 3)` - `(B, 5)` - `(A, 2)` - `(B, 1)` - `(C, 4)` Output: - `(A, 5)` - `(B, 6)` - `(C, 4)` The output may be returned in any order unless otherwise specified. Follow-up discussion: 1. How would you extend the implementation so that it can support other aggregation functions such as `MAX` in addition to `SUM`? 2. If the table is too large to fit into memory, how would you process it efficiently?

Quick Answer: This question evaluates a candidate's ability to perform data aggregation and manipulate basic data structures by grouping records by key and combining numeric values.

You are given a table represented as a list of rows. Each row is a pair `[key, value]` where `key` is a string identifying a group and `value` is an integer. Aggregate the table by `key` and return, for each distinct key, the sum of all corresponding values. Return the result as a list of `[key, sum]` pairs sorted in ascending order by key (the underlying problem allows any order, but this console fixes a sorted order so the result is deterministic). Example: Input rows: `[["A", 3], ["B", 5], ["A", 2], ["B", 1], ["C", 4]]` Output: `[["A", 5], ["B", 6], ["C", 4]]` Follow-up discussion (not graded here): How would you generalize the implementation to support other aggregations such as `MAX` in addition to `SUM`? And how would you process the table efficiently if it is too large to fit in memory (e.g. external/streaming aggregation, or map-reduce style partial sums by key)?

Constraints

  • 0 <= number of rows <= 10^5
  • Each row is a [key, value] pair.
  • key is a non-empty string.
  • value is an integer that may be negative, zero, or positive.
  • Sums fit in a 64-bit integer.

Examples

Input: [["A", 3], ["B", 5], ["A", 2], ["B", 1], ["C", 4]]

Expected Output: [["A", 5], ["B", 6], ["C", 4]]

Explanation: A: 3+2=5, B: 5+1=6, C: 4. Sorted by key.

Input: []

Expected Output: []

Explanation: Empty table aggregates to an empty result.

Input: [["X", 10]]

Expected Output: [["X", 10]]

Explanation: A single row passes through as its own group.

Input: [["A", -3], ["A", 3], ["B", -5]]

Expected Output: [["A", 0], ["B", -5]]

Explanation: Negative values are summed normally; A's values cancel to 0.

Input: [["z", 1], ["a", 1], ["m", 2], ["a", 4]]

Expected Output: [["a", 5], ["m", 2], ["z", 1]]

Explanation: Insertion order is z, a, m but output is sorted ascending by key; a accumulates 1+4=5.

Hints

  1. Use a hash map keyed by the group string, adding each row's value into the running total for that key.
  2. Collect the distinct keys and sort them so the output order is deterministic.
  3. For the MAX follow-up, replace the '+=' accumulation with a reducer function (e.g. max) so the same loop supports any associative aggregation. For the out-of-memory follow-up, stream the rows and aggregate incrementally, or partition by key and aggregate each partition (map-reduce).
Last updated: Jun 26, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Find Top Errors in Time Window - Notion (medium)
  • Implement a DAG task graph - Notion (medium)
  • Implement text editor with undo/redo - Notion (Medium)
  • Design a text editor with undo/redo - Notion (Medium)