PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates programming fluency in numerical data processing and algorithmic implementation for computing moving averages and handling edge cases in time-series sequences, and it applies to Data Scientist roles while falling under the Coding & Algorithms category.

  • Medium
  • Amazon
  • Coding & Algorithms
  • Data Scientist

Calculate 7-Day Rolling Average for Energy Consumption

Company: Amazon

Role: Data Scientist

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Onsite

##### Scenario Hiring manager wants a quick coding exercise to verify programming fluency. ##### Question Write a Python function that takes a list of daily energy consumption values and returns a list containing the 7-day rolling average; handle cases with fewer than 7 observations. ##### Hints Use sliding window or collections.deque; O(n) time.

Quick Answer: This question evaluates programming fluency in numerical data processing and algorithmic implementation for computing moving averages and handling edge cases in time-series sequences, and it applies to Data Scientist roles while falling under the Coding & Algorithms category.

Given a list of daily energy consumption values (non-negative numbers), return a list of the same length where each element i is the average of the last up to 7 values ending at position i. For the first few days with fewer than 7 observations, use all available observations so far. Return each average rounded to two decimal places. If the input list is empty, return an empty list.

Constraints

  • 0 <= len(consumption) <= 200000
  • 0 <= consumption[i] <= 1e9
  • Output list length equals input list length
  • Each average is computed over the last up to 7 elements ending at that index
  • Round each average to two decimal places
  • Time complexity O(n); extra space O(1) aside from output

Solution

from typing import List

def rolling_average_7(consumption: List[float]) -> List[float]:
    """
    Compute the 7-day trailing rolling average for energy consumption.
    For index i, average over consumption[max(0, i-6): i+1].
    Return each value rounded to two decimals.
    """
    result: List[float] = []
    window_sum = 0.0
    left = 0
    for i, val in enumerate(consumption):
        window_sum += val
        if i - left + 1 > 7:
            window_sum -= consumption[left]
            left += 1
        window_size = i - left + 1
        avg = window_sum / window_size
        result.append(round(avg, 2))
    return result
Explanation
Use a sliding window with a running sum. As you iterate, add the current value to the sum. If the window exceeds 7 elements, subtract the element at the left boundary and move the left boundary forward. The window size is then the number of elements between left and current index inclusive. Compute the average as running_sum/window_size and round to two decimals. This yields O(n) time with O(1) extra space (besides the output).

Time complexity: O(n). Space complexity: O(1) extra (O(n) including output).

Hints

  1. Maintain a running sum of the current window and update it in O(1) per step.
  2. When the window size exceeds 7, subtract the element that falls out from the left.
  3. Use round(value, 2) to round each average to two decimal places.
Last updated: Mar 29, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Implement Datacenter Router Commands - Amazon (hard)
  • Replace Delimited Tokens in a String - Amazon (medium)
  • Minimize Circular Redistribution Cost - Amazon (medium)
  • Find the Most Common Visit Pattern - Amazon (hard)
  • Maximize Value Under a Budget - Amazon (medium)