Compute 3-day rolling averages of daily deposits, rounded to 2 decimals

Quick Overview

Compute the 3-day rolling average of a list of daily deposits, rounding each window's average to 2 decimal places and returning an empty list when there are fewer than three days. It tests the fixed-size sliding window, exact integer running sums, and correct rounding of negative and fractional averages.

Compute 3-day rolling averages of daily deposits, rounded to 2 decimals

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given an array of integers `deposits` representing daily deposit values, calculate the 3-day rolling average for each valid window. Return the rolling averages as a list of floats, each rounded to 2 decimal places. The rolling average for day `i` (0-indexed, starting from `i = 2`) is the average of deposits on days `i-2`, `i-1`, and `i`. If the array has fewer than 3 elements, return an empty list. ### Function Signature ```python def rolling_average(deposits: list[int]) -> list[float]: ``` ### Rules - When there are at least 3 deposits, the output has `len(deposits) - 2` values in day order: the first is the average for day 2 and the last is the average for the final day. - Each value is `(deposits[i-2] + deposits[i-1] + deposits[i]) / 3` rounded to 2 decimal places and returned as a float, so an average of 200 is `200.0`. - The exact average of three integers never lies exactly halfway between two 2-decimal values, so rounding has one correct result for every window, negative ones included. ### Constraints - `0 <= len(deposits) <= 10⁵` - `-10⁶ <= deposits[i] <= 10⁶` ### Examples ```text Input: deposits = [100,200,300,400,500] Output: [200.0, 300.0, 400.0] ``` The windows are [100,200,300], [200,300,400] and [300,400,500], with averages 200, 300 and 400. ```text Input: deposits = [10,20] Output: [] ``` There are fewer than 3 elements, so there is no complete window. ```text Input: deposits = [5,-3,0,10] Output: [0.67, 2.33] ``` The window [5,-3,0] sums to 2, and 2 / 3 = 0.666... rounds to 0.67. The window [-3,0,10] sums to 7, and 7 / 3 = 2.333... rounds to 2.33.

Overview: Compute the 3-day rolling average of a list of daily deposits, rounding each window's average to 2 decimal places and returning an empty list when there are fewer than three days. It tests the fixed-size sliding window, exact integer running sums, and correct rounding of negative and fractional averages.

Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given an array of integers deposits representing daily deposit values, calculate the 3-day rolling average for each valid window.

Return the rolling averages as a list of floats, each rounded to 2 decimal places. The rolling average for day i (0-indexed, starting from i = 2) is the average of deposits on days i-2, i-1, and i.

If the array has fewer than 3 elements, return an empty list.

Function Signature

def rolling_average(deposits: list[int]) -> list[float]:

Rules

  • When there are at least 3 deposits, the output has len(deposits) - 2 values in day order: the first is the average for day 2 and the last is the average for the final day.
  • Each value is (deposits[i-2] + deposits[i-1] + deposits[i]) / 3 rounded to 2 decimal places and returned as a float, so an average of 200 is 200.0 .
  • The exact average of three integers never lies exactly halfway between two 2-decimal values, so rounding has one correct result for every window, negative ones included.

Constraints

  • 0 <= len(deposits) <= 10⁵
  • -10⁶ <= deposits[i] <= 10⁶

Examples

Input: deposits = [100,200,300,400,500]
Output: [200.0, 300.0, 400.0]

The windows are [100,200,300], [200,300,400] and [300,400,500], with averages 200, 300 and 400.

Input: deposits = [10,20]
Output: []

There are fewer than 3 elements, so there is no complete window.

Input: deposits = [5,-3,0,10]
Output: [0.67, 2.33]

The window [5,-3,0] sums to 2, and 2 / 3 = 0.666... rounds to 0.67. The window [-3,0,10] sums to 7, and 7 / 3 = 2.333... rounds to 2.33.