Reported October 2023
ZipRecruiterprefix sum

Distinct Values in Maximum-Sum Square Windows

Reported by candidates from ZipRecruiter's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live ZipRecruiter OA. Under 2s to a working solution.
Founder's read

The ZipRecruiter OA reported in October 2023 looks like a plain sliding-window sum on a grid, and then a tie-breaking rule ruins the lazy version. You need the max size by size window sum, then the sum of distinct values across every window that hits that max. Miss the ties and you return the wrong number on example 1. It's a matrix plus prefix-sum problem with a counting twist. If you blank during the live OA, StealthCoder runs invisibly on your desktop and gives you a working solution as a safety net.

The problem

Enumerate every size by size submatrix. Find the maximum window sum, collect each distinct value appearing in any tied maximum window, and return their sum.

Function
sumDistinctMaxWindowValues(matrix: int[][], size: int) → long

Examples
Example 1
matrix = [[1,2,1],[2,1,2],[1,2,1]]
size = 2
return = 3
All windows tie and the distinct union is {1,2}.
Example 2
matrix = [[1,2],[3,4]]
size = 1
return = 4
The maximum one-cell window contains 4.

Constraints
1 <= rows,columns <= 200
1 <= size <= min(rows,columns)

Reported by candidates. Source: FastPrep

Pattern and pitfall

Build a 2D prefix sum so every window sum is O(1). Pass one: scan all top-left corners, track the max sum. Pass two: scan again, and for every window whose sum equals the max, add each cell value to a set. Return the sum of the set. The trap is stopping at the first max window, or resetting the set when you find a bigger sum midway. Two passes avoids that cleanly. Another pitfall is overflow, so use a 64-bit type for sums. Cost: collecting values naively is up to 200x200 windows times size squared cells, which can get heavy when size is about 100 and many windows tie. Mitigate with a boolean seen array or a set and early skip when the set already covers all distinct values in the grid. If the brute force feels shaky under pressure, StealthCoder is the hedge that gets you a clean version during the live OA.

Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.

If this hits your live OA

You can drill Distinct Values in Maximum-Sum Square Windows cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass ZipRecruiter's OA.

ZipRecruiter reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Distinct Values in Maximum-Sum Square Windows FAQ

What's the trick in the ZipRecruiter distinct max window problem?+

Use a 2D prefix sum to get any window sum in constant time, find the maximum, then do a second pass collecting values from every window that ties it. The tie handling is the whole point. Stopping at one max window gives wrong answers.

Why do I need two passes?+

You can't know the true maximum until you've seen every window. If you collect values in one pass, you have to clear and rebuild the set whenever a bigger sum appears. Two passes is simpler and less bug-prone, and the cost is still fine for 200 by 200.

Will brute force time out with a 200 by 200 grid?+

Computing sums with prefix sums is fast. The risky part is iterating cells inside every tied window, which can be large when many windows tie. Use a seen array or set, and stop early if every distinct grid value has already been collected.

What edge cases should I test?+

Test size equal to 1, size equal to the full grid, a grid where all windows tie like example 1, and negative-looking sums if values could be negative. Also confirm the answer uses a 64-bit type since the distinct sum can exceed 32-bit range.

How do I prepare for this in 48 hours?+

Practice writing a 2D prefix sum from memory, including the inclusion-exclusion formula for a window. Then rehearse the two-pass structure with a set. That's about an hour of work. Skip fancy optimizations unless the simple version is clearly too slow.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with ZipRecruiter.

OA at ZipRecruiter?
Invisible during screen share
Get it