Google Oa question

google logo
google
May 20, 20252 reads

Summary

This post details a specific question encountered during a Google Online Assessment (OA).

Full Experience

Given an integer array nums, a window size windowSize, and an integer k, return a list of averages for each sliding window of size windowSize as the window moves from left to right across the array. When calculating the average for each window, ignore the largest k numbers within that window.

Constraints:

1 <= windowSize <= nums.length <=1e5

0 <= k < windowSize

-1e4<= nums[i] <= 1e4

Example 1:

Input: nums = [10, 20, 30, 40, 50, 60], windowSize = 3, k = 1 Output: [15.0, 25.0, 35.0, 45.0] Explanation:

Window [10, 20, 30]: Ignoring the largest number 30, the average is (10 + 20) / 2 = 15.0 Window [20, 30, 40]: Ignoring the largest number 40, the average is (20 + 30) / 2 = 25.0 Window [30, 40, 50]: Ignoring the largest number 50, the average is (30 + 40) / 2 = 35.0 Window [40, 50, 60]: Ignoring the largest number 60, the average is (40 + 50) / 2 = 45.0 Example 2:

Input: nums = [5, 1, 3, 8, 7], windowSize = 2, k = 0 Output: [3.0, 2.0, 5.5, 7.5]

Example 3:

Input: nums = [4, 4, 4, 4], windowSize = 2, k = 1 Output: [4.0, 4.0, 4.0]

Interview Questions (1)

Q1
Sliding Window Average Excluding Largest K Numbers
Data Structures & AlgorithmsMedium

Given an integer array nums, a window size windowSize, and an integer k, return a list of averages for each sliding window of size windowSize as the window moves from left to right across the array. When calculating the average for each window, ignore the largest k numbers within that window.

Constraints:

1 <= windowSize <= nums.length <=1e5

0 <= k < windowSize

-1e4<= nums[i] <= 1e4

Example 1:

Input: nums = [10, 20, 30, 40, 50, 60], windowSize = 3, k = 1 Output: [15.0, 25.0, 35.0, 45.0] Explanation:

Window [10, 20, 30]: Ignoring the largest number 30, the average is (10 + 20) / 2 = 15.0 Window [20, 30, 40]: Ignoring the largest number 40, the average is (20 + 30) / 2 = 25.0 Window [30, 40, 50]: Ignoring the largest number 50, the average is (30 + 40) / 2 = 35.0 Window [40, 50, 60]: Ignoring the largest number 60, the average is (40 + 50) / 2 = 45.0 Example 2:

Input: nums = [5, 1, 3, 8, 7], windowSize = 2, k = 0 Output: [3.0, 2.0, 5.5, 7.5]

Example 3:

Input: nums = [4, 4, 4, 4], windowSize = 2, k = 1 Output: [4.0, 4.0, 4.0]

Discussion (0)

Share your thoughts and ask questions

Join the Discussion

Sign in with Google to share your thoughts and ask questions

No comments yet

Be the first to share your thoughts and start the discussion!