FastPrepBuild a Pivot Table with Totals

Build a Pivot Table with Totals

Sigma Computing logoSigma Computing● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

You are given shirt-sales records. Each record contains [store, date, color, size, shirtsSold], with shirtsSold represented as a nonnegative decimal string.

Build a pivot table using rowField and columnField, each one of store, date, color, or size. Sort row and column labels lexicographically, sum shirts sold into cells, and maintain row, column, and grand totals.

Return a string matrix. The first row is an empty corner, column labels, and Total. Each data row is its label, cell values, and row total. The final row is Total, column totals, and the grand total.

Function

buildPivotTable(records: String[][], rowField: String, columnField: String) → String[][]

Examples

Example 1

records = [["A","2026-01-01","red","M","3"],["A","2026-01-02","blue","S","2"],["B","2026-01-01","red","L","4"]]rowField = "store"columnField = "color"return = [["","blue","red","Total"],["A","2","3","5"],["B","0","4","4"],["Total","2","7","9"]]

Rows are stores, columns are colors, and the last row and column contain the maintained totals.

Constraints

  • 1 <= records.length <= 100000
  • Every record has exactly five fields and shirtsSold is between 0 and 1000000000.
  • The grand total fits signed 64-bit arithmetic.
  • rowField != columnField.
  • If R and C are the numbers of distinct selected row and column labels, then (R + 2) * (C + 2) <= 10000, including header and totals cells.
See Sigma Computing hiring insights
public String[][] buildPivotTable(String[][] records, String rowField, String columnField) {
    // Write your code here.
}
records[["A","2026-01-01","red","M","3"],["A","2026-01-02","blue","S","2"],["B","2026-01-01","red","L","4"]]
rowField"store"
columnField"color"
expected[["", "blue", "red", "Total"], ["A", "2", "3", "5"], ["B", "0", "4", "4"], ["Total", "2", "7", "9"]]
Checking account…