Skip to content
Search lessons, topics, tests…
Esc

    ↑ ↓ moveEnter openEsc close
    Lesson19 / 20

    Try it independently: build a study report from a blank file

    Can you turn a week’s recorded minutes into a report from a blank file? Use the loops and methods you already know. Decide the steps before opening the solution.

    Build with the tools you already know

    For study minutes 20, 30, 0, 40, report the total, the number of days meeting a 25-minute goal, and the average across all recorded days, including the zero day. For this exercise the input has at most seven days, each containing 0–1440 whole minutes; these small totals fit in an int. Display minutes as units. For an empty array, use total 0, goal days 0 and average 0.

    Before reading the solution, create a fresh Program.cs and make a plan. Aim for two small methods: TotalMinutes and CountGoalDays. Calculate the average in the caller. You may look at language references; first avoid copying the completed course project.

    Text
    daily minutes [20, 30, 0, 40]
           │
           ├─ add every value ───────> total 90
           ├─ count values >= 25 ───> goal days 2
           └─ total / day count ────> average 22.5

    Which average matches the stated requirement?

    A 30, because zero days are always excluded
    B 22.5, because 90 is divided by all 4 days
    C 90, because that is the total
    D 2, because two days met the goal
    Show answer

    Correct answer: B

    All recorded days count, so average = 90 / 4 = 22.5 minutes.

    Option A: Excluding zero changes the requirement.

    Option C: The total is not the per-day average.

    Option D: The number of goal days is a different measure.

    Hint: List the units of each requested result.

    Write and check your attempt

    Your output should be:

    Text
    Total: 90 minutes
    Goal days: 2
    Average: 22.5 minutes

    Try these additional cases before looking below: [25] should produce 25 total, 1 goal day and 25 average; [0, 0] should produce zeros; an empty array should also produce zeros. A day exactly at the goal must count.

    Which implementation detail prevents an empty input from causing division by zero?

    A Check Length before calculating the average
    B Use a larger int
    C Remove the zero values first
    D Add one to the result afterward
    Show answer

    Correct answer: A

    The empty-input rule is explicit: retain average 0 when there are no recorded days.

    Option B: A larger numeric range does not allow division by zero.

    Option C: Removing values changes the required dataset.

    Option D: Changing the result afterward does not prevent the invalid operation.

    Compare with one complete solution

    C#
    using System;
    
    int[] minutes = { 20, 30, 0, 40 };
    int total = TotalMinutes(minutes);
    int goalDays = CountGoalDays(minutes, 25);
    decimal average = 0;
    if (minutes.Length > 0) average = (decimal)total / minutes.Length;
    
    Console.WriteLine($"Total: {total} minutes");
    Console.WriteLine($"Goal days: {goalDays}");
    Console.WriteLine($"Average: {average.ToString(System.Globalization.CultureInfo.InvariantCulture)} minutes");
    
    static int TotalMinutes(int[] values)
    {
        int total = 0;
        foreach (int value in values) total += value;
        return total;
    }
    
    static int CountGoalDays(int[] values, int goal)
    {
        int count = 0;
        foreach (int value in values)
        {
            if (value >= goal) count++;
        }
        return count;
    }

    The (decimal) conversion happens before division, preserving the fractional result. ToString with InvariantCulture gives the shown dot in this example regardless of local number-format settings; it is a display choice, not part of the arithmetic rule.

    Review your design

    QuestionUseful evidence
    Does a day at the goal count?Try one value equal to 25.
    Do zero days remain in the average?Keep the day count at 4 for the given example.
    Is empty input handled?Use an empty array and observe three zeros.
    Can the methods be reused?Each accepts data rather than reading console input internally.

    Why keep Console.ReadLine out of TotalMinutes in this design?

    A Methods cannot read console input
    B The calculation becomes easier to reuse and check with known data
    C It makes an array immutable
    D It prevents all runtime errors
    Show answer

    Correct answer: B

    A method receiving explicit data can be called by a console app or by a repeatable check without pretending to be a user.

    Option A: Methods can read console input, but this method’s responsibility is calculation.

    Option C: The parameter does not make arrays immutable.

    Option D: Clear responsibilities help but cannot prevent every error.

    Make one extension your own

    Add a highest-minutes result. Define the empty-input result as 0. For the given array it should be 40. Start a maximum variable at 0 and compare each value with it. Then add a check for [5, 50, 10], which should give 50. This extension is intentionally small enough to complete without a new language feature.

    Explain what you built to another person: the data, the requirements, one boundary check and one choice that keeps the code understandable. If you needed the solution, close it and recreate the important methods in a later sitting.

    References: Microsoft — arrays and numeric conversions.

    Quick reference

    • Write inputs, rules and expected outputs before coding.
    • Include equality at the goal boundary.
    • Guard division when the collection may be empty.
    • Use decimal division when a fractional average matters.
    • Keep calculation methods separate from console input.
    Sign in to mark lessons done and keep your place in the course.Sign in

    TechInterviewNotes Editorial Team · Updated 27 Oct 2026 ·