Google: sum of k small subarray size m

Difficulty: MEDIUMCategory: InterviewExperienceFrequency: 1Asked On 2025-10-11Added By Anonymous
Source: Link

Company: Google
Role: L3 / L4
Interview Stage: Phone Screen
Location: unknown
Year: unknown

Original Post Content:

Find the sum of K smallest elements in all subarrays of size M.
Given: arr = [3, 1, 4, 5, 7, 8, 11]; M=4, k=3
Expected sum (example output): [8, 10, 16, 20]
I initially described a correct brute force solution. However, when trying to optimize, I was stuck using a single multiset. Then, I got the intuition of using two multisets: one to store the smallest k elements and the other for the rest of the elements.

This was Question 1 from the interview. The problem description was repeated: Find the sum of K smallest elements in all subarrays of size M.
Example: arr = [3, 1, 4, 5, 7, 8, 11]; M=4, k=3
Partial sum output: [8, ]
(Interviewer/context note: //google dev)

Comments