using System.Diagnostics;
using System.Collections.Generic;
public static Dictionary<string, double>[] DoAggregations(double[,] input)
int rowCount = input.GetLength(0);
int colCount = input.GetLength(1);
Dictionary<string, double>[] retResList = new Dictionary<string, double>[colCount];
double[,] inverted = new double[colCount,rowCount];
for(int i = 0; i < rowCount; i++)
for(int j = 0; j < colCount; j++)
inverted[j,i] = input[i,j];
for(int i = 0; i< colCount; i++)
HashSet<double> hs = new HashSet<double>();
for(int j = 0; j< rowCount; j++)
double currentVal = inverted[i, j];
retResList[i] = (new Dictionary<string, double>
{"AVERAGE", colSum / rowCount},
{"COUNT DISTINCT", hs.Count}
public static Dictionary<string, double>[] DoAggregations(double[,] input)
return new Dictionary<string, double>[]{
new Dictionary<string, double>{{"SUM", 9d}, {"AVERAGE", 2.25d}, {"COUNT DISTINCT", 3d}},
new Dictionary<string, double>{{"SUM", 14d}, {"AVERAGE", 3.5d}, {"COUNT DISTINCT", 2d}},
new Dictionary<string, double>{{"SUM", 16d}, {"AVERAGE", 4d}, {"COUNT DISTINCT", 4d}}};
public static void Main()
double[,] smallInput = new double[,] { { 1, 4, 3 }, { 2, 3, 4 }, { 1, 3, 7 }, { 5, 4, 2 } };
Console.WriteLine("=== Small Input - Mocked Execution ===");
var mockResult = Mock.DoAggregations(smallInput);
IsResultCorrect(mockResult);
Console.WriteLine("\n=== Small Input - Solution Execution ===");
var result = Solution.DoAggregations(smallInput);
Console.WriteLine("\n=== Large Input - Solution Execution ===");
var largeInput = GenerateLargeInput();
var sw = Stopwatch.StartNew();
Solution.DoAggregations(largeInput);
IsExecutionFastEnough(sw.ElapsedMilliseconds/5);
public static double[,] GenerateLargeInput()
var input = new double[numRows, numCols];
Random rnd = new Random(123);
for (int col = 0; col < numCols; col++)
for (int row = 0; row < numRows; row++)
input[row, col] = rnd.Next(100000);
public static void IsResultCorrect(Dictionary<string, double>[] result)
result[0]["SUM"] == 9d &&
result[1]["SUM"] == 14d &&
result[2]["SUM"] == 16d &&
result[0]["AVERAGE"] == 2.25d &&
result[1]["AVERAGE"] == 3.5d &&
result[2]["AVERAGE"] == 4d &&
result[0]["COUNT DISTINCT"] == 3d &&
result[1]["COUNT DISTINCT"] == 2d &&
result[2]["COUNT DISTINCT"] == 4d
Console.WriteLine("IsResultCorrect Passed");
Console.WriteLine("IsResultCorrect Failed");
public static void IsExecutionFastEnough(double milliSeconds)
Console.WriteLine($"IsExecutionFastEnough DIAMOND!!!: {milliSeconds}ms");
else if(milliSeconds < 105)
Console.WriteLine($"IsExecutionFastEnough GOLD!: {milliSeconds}ms");
else if(milliSeconds < 150)
Console.WriteLine($"IsExecutionFastEnough SILVER: {milliSeconds}ms");
Console.WriteLine($"IsExecutionFastEnough BRONZE: {milliSeconds}ms");