Coding problem
Weekly averages from daily readings
The task
The energy dashboard turns a meter's daily readings into a weekly chart.
Complete WeeklyAverages(List<Reading> readings). Reading is a record with a Day and a Value; the list holds one reading per day, in day order. Split it into consecutive weeks of 7 readings, the first week starting with the first reading; the last week may have fewer than 7. Return a WeekAverage for each week: its Week number, starting at 1, the number of Days in it, and the Average of its values over those days, as an exact decimal, not rounded. An empty list gives an empty list. The list and its readings are never null.
Signature
public record Reading(int Day, decimal Value); public record WeekAverage(int Week, int Days, decimal Average); public static List<WeekAverage> WeeklyAverages(List<Reading> readings)
Examples
10 readings with the values [5, 7, 6, 8, 9, 5, 9, 4, 6, 8]→[(1, 7, 7), (2, 3, 6)]. The first seven add up to 49, an average of 7; the last week has only 3 days, 18 in all, an average of 6.[]→[]. No readings, no weeks.
Limits: Up to 700,000 readings.
The tests check: no readings, fewer than seven readings, exactly fourteen, a short last week averaged over its own days, weeks numbered from 1, averages with cents, and 700,000 readings in time.
Hints
Your code
Already included: using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Http; using System.Threading; using System.Threading.Tasks;
public record Reading(int Day, decimal Value);
public record WeekAverage(int Week, int Days, decimal Average);
public static class Solution
{
// Weeks of seven readings (the last may be shorter), numbered from 1, each with its own average.
public static List<WeekAverage> WeeklyAverages(List<Reading> readings)
{
throw new NotImplementedException();
}
}This problem comes with TechInterviewNotes Full Access
Unlock all paid content on TechInterviewNotes.