Sum Every Third Spiral Element
Reported by candidates from ZipRecruiter's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The mistake that sinks a first attempt on this ZipRecruiter OA, reported in September 2024, is counting the wrong thing. People sum every third value in the matrix instead of every third position along the spiral. The task is simple: walk a square matrix clockwise from the top-left, and add up values at spiral indices 0, 3, 6 and so on. It's a matrix traversal with a counter, nothing exotic. Where candidates lose it is boundary handling and off-by-one indexing. If you freeze on the layer shrinking logic during the live OA, StealthCoder is the safety net running invisibly on your screen.
The problem
Traverse a square integer matrix in clockwise spiral order starting at the top-left. Return the sum of elements at zero-based spiral indices divisible by three. Function sumEveryThirdSpiral(matrix: int[][]) → long Examples Example 1 matrix = [[1,2,3],[4,5,6],[7,8,9]] return = 14 The selected spiral values are 1, 6, and 7. Example 2 matrix = [[5]] return = 5 The only value has spiral index zero. Constraints 1 <= matrix.length == matrix[i].length <= 500
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is to keep a running spiral index and never build the spiral as a separate list unless you want to. Use four boundaries: top, bottom, left, right. Walk the top row left to right, the right column top to bottom, the bottom row right to left, then the left column bottom to top, shrinking a boundary after each pass. For every cell you visit, check if the counter mod 3 equals 0, add the value, then increment the counter. The classic pitfall is the odd-sized square. On a 3x3 or a 1x1, the last pass can double-visit the center or a row you already finished. Guard the bottom row and left column passes with top <= bottom and left <= right checks. Use a 64-bit sum since the return type is long. With n up to 500, O(n^2) is fine. If the boundary logic slips under pressure, StealthCoder can hand you the clean version during the live OA.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Sum Every Third Spiral Element 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.
Sum Every Third Spiral Element FAQ
What's the trick in the ZipRecruiter spiral sum problem?+
Track a single counter for spiral order, not matrix position. Every cell you visit increments it, and you add the value when counter % 3 == 0. Example 1 proves it: 1, 6, 7 sit at spiral indices 0, 3, 6, which sum to 14.
How hard is this one really?+
Easy to medium. The idea is short, but the boundary updates trip people up. If you've written a standard spiral matrix traversal before, you're basically done. The only addition is the counter and the modulo check.
Where do most solutions break?+
Odd-sized matrices. The center cell or the last row can get visited twice if you skip the top <= bottom and left <= right checks before the reverse passes. Test the 1x1 case, which should return the single value, and the 3x3 example.
Do I need extra space for this?+
No. Walk the matrix in place with four boundary variables and a counter. Building a full spiral list works but wastes O(n^2) memory. With n up to 500, either passes, but the counter approach is cleaner and less error-prone.
How do I prepare for this in 48 hours?+
Write the spiral traversal from scratch twice without looking. Then add the counter and modulo. Test on 1x1, 2x2, and 3x3. Make sure your sum is a 64-bit type. That covers nearly everything this problem can throw at you.