Reported June 2021
ZipRecruitersimulation

Center-Outward Number Spiral

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 data structure here is just a 2D grid plus a direction table, and that's the whole game. ZipRecruiter reported this one in June 2021: build an n by n matrix for odd n, drop 1 in the center, then spiral outward starting by moving down. It's a simulation problem dressed up as a matrix problem. No clever math needed, just clean bookkeeping on position, direction, and leg length. If you blank on the turn order mid-assessment, StealthCoder is the invisible safety net that can hand you the working loop.

The problem

Given a positive odd integer n, construct an n by n matrix containing every integer from 1 through n * n.
Place 1 in the center cell. Continue with consecutive integers while moving in an outward spiral:
Move down first.
Turn right relative to the current movement after completing each leg, so the directions repeat as down, left, up, right.
Increase the leg length after every two legs.
Stop after placing n * n.
Return the completed matrix.

Function
centerOutwardSpiral(n: int) → int[][]

Examples
Example 1
n = 3
return = [[5,6,7],[4,1,8],[3,2,9]]
The spiral starts at the center with 1, moves down to 2, then expands through left, up, and right turns until 9 reaches the bottom-right corner.
Example 2
n = 1
return = [[1]]
A one-cell matrix contains only the center value 1.

Constraints
1 <= n <= 99
n is odd.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is leg lengths. Start at the center (n/2, n/2) with 1. Directions cycle down, left, up, right, which is a clockwise turn from the heading. Leg length starts at 1 and grows by 1 after every two legs, so you walk 1 down, 1 left, 2 up, 2 right, 3 down, 3 left, and so on. Keep a direction array like [(1,0),(0,-1),(-1,0),(0,1)], a counter, and fill until you hit n*n. Check example 1: 1 center, 2 below, 3 left of that, then 4 and 5 going up, 6 and 7 going right. That matches. The common pitfall is incrementing the leg length every leg instead of every two, or writing out of bounds on the last leg. The final leg can be cut off by the n*n stop condition, so check the count inside the inner step loop. StealthCoder is your hedge if the live OA makes you freeze on this indexing.

StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.

If this hits your live OA

You can drill Center-Outward Number Spiral 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. If you're reading this with an OA window open, you're who this was built for.

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. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Center-Outward Number Spiral FAQ

How hard is the Center-Outward Number Spiral really?+

Easy to medium. There's no hard algorithm, only careful simulation. Most failures come from off-by-one errors in leg length or direction order. If you trace n = 3 by hand first, you'll catch nearly every bug before you submit.

What's the trick to getting the leg lengths right?+

Start the length at 1 and increase it after every second leg. The sequence is 1,1,2,2,3,3 and so on. Use a leg counter, and when it's even after finishing a leg, bump the length. Stop the instant you place n*n.

Should I fill from the center or from the outside in?+

Fill from the center, since that's what the problem specifies and the examples show. The center is index n/2 using integer division, which works because n is always odd. Starting outside would reverse the numbering and fail the examples.

Is this spiral pattern still asked in assessments?+

ZipRecruiter candidates reported it in June 2021, and spiral matrix variants keep showing up because they test careful simulation. Expect twists in start direction, turn direction, or starting cell rather than a brand new technique.

How do I prepare for this in 48 hours?+

Write the direction-array spiral once from memory, then change the start direction and turn direction to see what breaks. Test n = 1, 3, and 5 by hand. Those cases cover the edge and the growth pattern, which is all this problem checks.

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