Center-Outward Number Spiral
Updated
Problem statement
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 = 3return = [[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 = 1return = [[1]]A one-cell matrix contains only the center value 1.
Constraints
1 <= n <= 99nis odd.
More ZipRecruiter Phone Screen questions
- Neighboring Record IDsPHONE SCREEN · Seen May 2026
- Restaurant Vote LeadersPHONE SCREEN · ONSITE INTERVIEW · Seen Feb 2026
- Most Frequent Word across Grouped ArraysPHONE SCREEN · Seen Sep 2024
- Starting Value with the Longest Collatz ChainPHONE SCREEN · Seen Apr 2022
- Classify an Alien Dictionary OrderingPHONE SCREEN · ONSITE INTERVIEW · Seen Mar 2022
- Multiset Intersection of Three ListsPHONE SCREEN · Seen Feb 2022
- Find the Smallest Magic IndexPHONE SCREEN · Seen Dec 2021
- Website Pair with the Most Common VisitorsPHONE SCREEN · Seen Nov 2021
- Missing Ranges within Inclusive BoundsPHONE SCREEN · Seen Feb 2021
- Fibonacci Number with Constant Extra SpacePHONE SCREEN · Seen Mar 2017
Similar questions at other companies
- Grid Infection with Recovery After D DaysOpenAI · PHONE SCREENOpenAI interview questions
- Days Until Poisoned Plants StabilizeApple · PHONE SCREENApple interview questions
- Apply a 2048 Board MoveGlean · PHONE SCREENGlean interview questions
- Dense Matrix MultiplicationAmazon · PHONE SCREEN
- File-Backed Dense Game of LifeApplied Intuition · PHONE SCREENApplied Intuition interview questions
- Tilting Maze BallHudson River Trading · PHONE SCREENHudson River Trading interview questions
- Spiral Order with ObstaclesTemu · PHONE SCREENTemu interview questions
- Logistic Regression DiagnosticsLinkedIn · PHONE SCREENLinkedIn interview questions
- Spiral Matrix TraversalByteDance · PHONE SCREENByteDance interview questions
- S-Shaped Matrix TraversalFox Corporation · PHONE SCREEN