Calculate Compressed Mean

Calculate Compressed Mean is a Easy Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Sliding Window pattern, in the Fixed Size (Subarray Calculation) subpattern.

Problem statement

You are given a 0-indexed integer array nums. To create the compressed array, repeatedly remove every pair of adjacent equal elements from nums until no such pairs remain. After compression, calculate the mean (average) of the elements in the resulting array.

Return the mean of the compressed array as a floating-point number. If the compressed array is empty, return 0.0.

Topics and companies

Topics: Database.

Reported in interviews at Google, Amazon, Facebook, Microsoft.

How to practise Calculate Compressed Mean on Thita.ai

Starter code is provided in C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(N), where N is the number of elements in the input vector. We iterate through the vector once to compute the sum. time and O(1), as we only use a constant amount of extra space for variables (sum, n), regardless of input size. space.

Open Calculate Compressed Mean in the code editor, or read the Calculate Compressed Mean editorial for a worked solution with its approach and complexity analysis.

Where Calculate Compressed Mean sits in the DSA pattern sheet

Problems related to Calculate Compressed Mean

Other problems that use the same Fixed Size (Subarray Calculation) and Sliding Window techniques:

Preparing your workspace

Progress is saved as you study.