Distinct Values on Maximum-Sum Frames
Reported by candidates from ZipRecruiter's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
ZipRecruiter reported this one in September 2024, and the title makes it sound scarier than it is. Strip the wording and it's a matrix scan: slide a frameSize by frameSize window over the grid, compute each border sum, find the max, then union the values on every tied border. It's prefix sums plus a set. If you've got an OA invite and 48 hours, this is learnable tonight. StealthCoder sits invisibly on your screen as a safety net if you blank during the live assessment, but the logic below should get you most of the way there.
The problem
For this exercise, use the callable contract below.
Given an integer matrix and frameSize, consider every contiguous frameSize × frameSize submatrix. Its border contains each cell on its top row, bottom row, left column, or right column exactly once.
Find the maximum border sum. Among every frame tied for that maximum, collect all integer values that occur on a winning border. Return the sum of those distinct values.
Function
sumDistinctWinningBorderValues(matrix: int[][], frameSize: int) → long
Examples
Example 1
matrix = [[1,2,3],[4,5,6],[7,8,9]]
frameSize = 2
return = 28
The bottom-right 2-by-2 frame has the unique maximum border sum, and 5+6+8+9=28.
Example 2
matrix = [[1,1,1],[1,0,1],[1,1,1]]
frameSize = 2
return = 1
All four frames tie; the union of their border values is {0,1}.
Constraints
1 <= rows, columns <= 200.
1 <= frameSize <= min(rows, columns).
Matrix values fit in a 32-bit signed integer; the result fits in a 64-bit signed integer.Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is computing each border sum in O(1). Build row prefix sums and column prefix sums. For a frame at (r, c) with size k, border = top row segment + bottom row segment + left column segment + right column segment, minus the four corners you counted twice. Watch k=1, where the border is a single cell, and k=2, where the overlap handling changes. Easiest fix: special-case k=1, and otherwise subtract corners carefully, or sum the left and right columns excluding the top and bottom rows. Pass one finds the max sum. Pass two walks the tied frames and adds border values into a hash set, then sums the set into a 64-bit long. Pitfall: overflow from using int, and re-walking borders for non-tied frames. Worst case is about 200x200 frames times 4k cells for the set pass, which is fine. If you blank mid-assessment, StealthCoder is the hedge, but know the corner subtraction cold.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Distinct Values on Maximum-Sum Frames 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 for the candidate who got the OA invite this morning and has 72 hours, not six months.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass ZipRecruiter's OA.
ZipRecruiter reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Distinct Values on Maximum-Sum Frames FAQ
What's the trick in the ZipRecruiter distinct winning border values problem?+
Use row and column prefix sums so each frame's border sum is O(1). Find the max in one pass. In a second pass, collect border values from only the frames that tie the max into a set, then sum the set. Use a 64-bit type for the sums.
How hard is this OA question really?+
Medium. There's no exotic algorithm. The difficulty is careful indexing, double-counted corners, and the k=1 edge case. If you're comfortable with 2D prefix sums, you can code it in 20 to 30 minutes.
How do I avoid double-counting corners?+
Sum the full top and bottom row segments of length k. Then add the left and right column segments only for the rows strictly between the top and bottom. For k=1, return the single cell. For k=2 the middle range is empty, which works naturally.
Do I need to recompute borders for every frame in the second pass?+
Yes, but only for frames whose sum equals the max. Store the sums from pass one, or recompute them. Walking the 4k border cells of tied frames is cheap at 200 by 200 limits. Insert each value into a hash set.
How should I prepare for this in 48 hours?+
Write 2D prefix sum code from scratch twice. Then hand-trace both examples, especially the tie case where the answer is 1 from the set {0,1}. Test k=1, k equal to the full matrix, and negative values. That covers nearly every bug.