Build a pivot table class over shirt sales records with running and grand totals

Read the full interview experience this question came from →

Quick Overview

Implement a class that stores shirt sales records and builds pivot tables summing shirts sold by any row field and column field, then extend it with running totals and grand totals along both axes. It tests object-oriented structure, aggregation with dictionaries, deterministic layout, and consistency between cells and totals.

Build a pivot table class over shirt sales records with running and grand totals

Company: Sigmacomputing

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Technical Screen

You are given shirt sales records as a list of Python dictionaries. Each record has a store ID, a date, a color, a size and the number of shirts sold: ```python SHIRTS = [ {"Store ID": "abcd1234", "Date": "2021-05-25", "Color": "red", "Size": "XS", "Shirts sold": 1}, {"Store ID": "abcd1234", "Date": "2021-05-25", "Color": "blue", "Size": "L", "Shirts sold": 20}, {"Store ID": "abcd1234", "Date": "2021-05-25", "Color": "green", "Size": "M", "Shirts sold": 300}, {"Store ID": "abcd1234", "Date": "2021-05-26", "Color": "red", "Size": "XS", "Shirts sold": 4}, {"Store ID": "abcd1234", "Date": "2021-05-26", "Color": "black", "Size": "S", "Shirts sold": 50}, {"Store ID": "5678wxyz", "Date": "2021-05-25", "Color": "blue", "Size": "XL", "Shirts sold": 600}, {"Store ID": "5678wxyz", "Date": "2021-05-25", "Color": "green", "Size": "M", "Shirts sold": 7}, {"Store ID": "5678wxyz", "Date": "2021-05-25", "Color": "black", "Size": "L", "Shirts sold": 80}, {"Store ID": "5678wxyz", "Date": "2021-05-26", "Color": "blue", "Size": "S", "Shirts sold": 900}, {"Store ID": "e1f9g2h8", "Date": "2021-05-26", "Color": "red", "Size": "S", "Shirts sold": 1}, {"Store ID": "e1f9g2h8", "Date": "2021-05-27", "Color": "black", "Size": "M", "Shirts sold": 20}, ] ``` Write a class that receives these records in its constructor and produces pivot tables from them. A pivot table groups the records by the values of one field along the rows (the y axis) and by the values of another field along the columns (the x axis); each cell holds the total of a numeric field over the records with that row value and that column value. The interviewer expected an object-oriented structure: a class whose constructor takes and stores the input, with the pivot logic in methods, rather than a standalone function. Correct values come first, but the printed table should also be readable, with labeled and aligned rows and columns. ### Constraints and Clarifications - Field names are exactly as in the data, including spaces and capitalization, for example `"Store ID"` and `"Shirts sold"`. - Cell values are sums of `"Shirts sold"`. - Assume every record has all five fields unless the interviewer says otherwise. ### Clarifying Questions - Are the row and column fields fixed, for example stores by color, or chosen by the caller? - In what order should rows and columns appear: sorted, or in order of first appearance? - Should a combination with no records show `0` or an empty cell? - Should the class return the table as data, print it, or both? - How should the class treat a record that is missing a field or has a non-numeric count? ### Part 1 — Build the pivot table Implement the class and a method that takes a row field, a column field and a value field and builds the pivot table. For example, with `"Store ID"` as the rows, `"Color"` as the columns and `"Shirts sold"` as the value, each cell is the number of shirts of that color sold by that store over all dates and sizes. Print the result as a table. ```hint Accumulate, then lay out Separate adding up the numbers from arranging them into a grid. Think about which structure lets you add a record's count to its cell when all you know is the record's row value and column value. ``` ```hint Every row needs every column A store that never sold a given color still needs a cell in that color's column, or the printed columns will not line up. ``` #### What This Part Should Cover - A class whose constructor stores the records and whose method takes the pivot fields as parameters - Correct sums for every cell, including combinations with no records - A deterministic row and column order and a readable printed layout - Time and space complexity in terms of the number of records and of distinct row and column values ### Part 2 — Running and grand totals along both axes Extend the output with totals along the x axis and the y axis: running totals that accumulate from left to right across each row and from top to bottom down each column, and grand totals for every row, every column and the whole table. ```hint Work from the grid Once the pivoted grid exists, every total is a pass over it. Going back to the raw records for each total is slower and risks totals that disagree with the cells. ``` #### Clarifying Questions for this Part - Should running totals replace the cell values in a separate view, or appear next to them? - Do running totals follow the same row and column order as the displayed table? #### What This Part Should Cover - Grand totals per row, per column and overall, and a check that they agree - Running totals computed in the displayed order along each axis - An interface that adds totals as options without duplicating the pivot logic - Printed output in which the total row and the total column stay aligned ### What a Strong Answer Covers - A clean class design: input stored once, pivot parameters per call, computation separated from printing - Correct values for every cell and every total on the given data - Explicit decisions about ordering, empty cells and malformed records - Linear-time aggregation, with the complexity stated - Simple self-checks, such as the row totals and the column totals summing to the same grand total ### Follow-up Questions - How would you support more than one field on an axis, such as store and date together on the rows? - How would you support aggregations other than a sum, such as a count, an average or a maximum? - The records no longer fit in memory. How would you produce the same pivot table? - How would you add a filter, such as a single date, without changing the constructor?

Overview: Implement a class that stores shirt sales records and builds pivot tables summing shirts sold by any row field and column field, then extend it with running totals and grand totals along both axes. It tests object-oriented structure, aggregation with dictionaries, deterministic layout, and consistency between cells and totals.

Read the full Sigmacomputing Software Engineer interview experience this question came from

|Home/Software Engineering Fundamentals/Sigmacomputing
Sigmacomputing logo
Sigmacomputing
Jul 29, 2026
mediumSoftware EngineerTechnical ScreenSoftware Engineering Fundamentals
0
0

You are given shirt sales records as a list of Python dictionaries. Each record has a store ID, a date, a color, a size and the number of shirts sold:

SHIRTS = [
    {"Store ID": "abcd1234", "Date": "2021-05-25", "Color": "red", "Size": "XS", "Shirts sold": 1},
    {"Store ID": "abcd1234", "Date": "2021-05-25", "Color": "blue", "Size": "L", "Shirts sold": 20},
    {"Store ID": "abcd1234", "Date": "2021-05-25", "Color": "green", "Size": "M", "Shirts sold": 300},
    {"Store ID": "abcd1234", "Date": "2021-05-26", "Color": "red", "Size": "XS", "Shirts sold": 4},
    {"Store ID": "abcd1234", "Date": "2021-05-26", "Color": "black", "Size": "S", "Shirts sold": 50},
    {"Store ID": "5678wxyz", "Date": "2021-05-25", "Color": "blue", "Size": "XL", "Shirts sold": 600},
    {"Store ID": "5678wxyz", "Date": "2021-05-25", "Color": "green", "Size": "M", "Shirts sold": 7},
    {"Store ID": "5678wxyz", "Date": "2021-05-25", "Color": "black", "Size": "L", "Shirts sold": 80},
    {"Store ID": "5678wxyz", "Date": "2021-05-26", "Color": "blue", "Size": "S", "Shirts sold": 900},
    {"Store ID": "e1f9g2h8", "Date": "2021-05-26", "Color": "red", "Size": "S", "Shirts sold": 1},
    {"Store ID": "e1f9g2h8", "Date": "2021-05-27", "Color": "black", "Size": "M", "Shirts sold": 20},
]

Write a class that receives these records in its constructor and produces pivot tables from them. A pivot table groups the records by the values of one field along the rows (the y axis) and by the values of another field along the columns (the x axis); each cell holds the total of a numeric field over the records with that row value and that column value.

The interviewer expected an object-oriented structure: a class whose constructor takes and stores the input, with the pivot logic in methods, rather than a standalone function. Correct values come first, but the printed table should also be readable, with labeled and aligned rows and columns.

Constraints and Clarifications

  • Field names are exactly as in the data, including spaces and capitalization, for example "Store ID" and "Shirts sold" .
  • Cell values are sums of "Shirts sold" .
  • Assume every record has all five fields unless the interviewer says otherwise.

Clarifying Questions Guidance

  • Are the row and column fields fixed, for example stores by color, or chosen by the caller?
  • In what order should rows and columns appear: sorted, or in order of first appearance?
  • Should a combination with no records show 0 or an empty cell?
  • Should the class return the table as data, print it, or both?
  • How should the class treat a record that is missing a field or has a non-numeric count?

Part 1 — Build the pivot table

Implement the class and a method that takes a row field, a column field and a value field and builds the pivot table. For example, with "Store ID" as the rows, "Color" as the columns and "Shirts sold" as the value, each cell is the number of shirts of that color sold by that store over all dates and sizes. Print the result as a table.

What This Part Should Cover Guidance

  • A class whose constructor stores the records and whose method takes the pivot fields as parameters
  • Correct sums for every cell, including combinations with no records
  • A deterministic row and column order and a readable printed layout
  • Time and space complexity in terms of the number of records and of distinct row and column values

Part 2 — Running and grand totals along both axes

Extend the output with totals along the x axis and the y axis: running totals that accumulate from left to right across each row and from top to bottom down each column, and grand totals for every row, every column and the whole table.

Clarifying Questions for this Part Guidance

  • Should running totals replace the cell values in a separate view, or appear next to them?
  • Do running totals follow the same row and column order as the displayed table?

What This Part Should Cover Guidance

  • Grand totals per row, per column and overall, and a check that they agree
  • Running totals computed in the displayed order along each axis
  • An interface that adds totals as options without duplicating the pivot logic
  • Printed output in which the total row and the total column stay aligned

What a Strong Answer Covers Guidance

  • A clean class design: input stored once, pivot parameters per call, computation separated from printing
  • Correct values for every cell and every total on the given data
  • Explicit decisions about ordering, empty cells and malformed records
  • Linear-time aggregation, with the complexity stated
  • Simple self-checks, such as the row totals and the column totals summing to the same grand total

Follow-up Questions Guidance

  • How would you support more than one field on an axis, such as store and date together on the rows?
  • How would you support aggregations other than a sum, such as a count, an average or a maximum?
  • The records no longer fit in memory. How would you produce the same pivot table?
  • How would you add a filter, such as a single date, without changing the constructor?