Fill a matrix in clockwise spiral order with Fibonacci numbers

Quick Overview

Build a matrix of a given size whose cells, read in clockwise spiral order from the top-left corner, hold consecutive Fibonacci numbers starting at zero. It tests boundary-based spiral traversal, handling a leftover single row or column, and generating a sequence iteratively.

Fill a matrix in clockwise spiral order with Fibonacci numbers

Company: Zoho

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given two integers `rows` and `columns`, return a `rows x columns` matrix filled in clockwise spiral order with the first `rows * columns` Fibonacci numbers. The Fibonacci sequence is defined as `F(0) = 0`, `F(1) = 1`, and `F(n) = F(n - 1) + F(n - 2)` for `n >= 2`. Start at the top-left corner moving right, then move down, left, and up, moving inward after each completed boundary. Place `F(0)`, `F(1)`, `F(2)`, ... in the order the cells are visited. ### Function Signature ```python def fibonacci_spiral_matrix(rows: int, columns: int) -> list[list[int]]: pass ``` ### Rules - Return the matrix as a list of `rows` lists, row 0 first, each holding `columns` integers. - The spiral visits every cell exactly once, so the cell visited at step `k` (counting from 0) holds `F(k)`. ### Constraints - `1 <= rows, columns <= 46` - `rows * columns <= 46` ### Examples ```text Input: rows = 2, columns = 3 Output: [[0,1,1],[5,3,2]] ``` The first 6 Fibonacci numbers are 0, 1, 1, 2, 3, 5. They fill the top row left to right, go down the right column, then fill the bottom row right to left. ```text Input: rows = 1, columns = 1 Output: [[0]] ``` A single cell holds `F(0) = 0`. ```text Input: rows = 3, columns = 4 Output: [[0,1,1,2],[34,55,89,3],[21,13,8,5]] ``` The outer ring takes `F(0)` through `F(9)` (0 to 34) and ends on the left cell of the middle row. The two inner cells of the middle row then take 55 and 89, moving right.

Overview: Build a matrix of a given size whose cells, read in clockwise spiral order from the top-left corner, hold consecutive Fibonacci numbers starting at zero. It tests boundary-based spiral traversal, handling a leftover single row or column, and generating a sequence iteratively.

Zoho logo
Zoho
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given two integers rows and columns, return a rows x columns matrix filled in clockwise spiral order with the first rows * columns Fibonacci numbers.

The Fibonacci sequence is defined as F(0) = 0, F(1) = 1, and F(n) = F(n - 1) + F(n - 2) for n >= 2.

Start at the top-left corner moving right, then move down, left, and up, moving inward after each completed boundary. Place F(0), F(1), F(2), ... in the order the cells are visited.

Function Signature

def fibonacci_spiral_matrix(rows: int, columns: int) -> list[list[int]]:
    pass

Rules

  • Return the matrix as a list of rows lists, row 0 first, each holding columns integers.
  • The spiral visits every cell exactly once, so the cell visited at step k (counting from 0) holds F(k) .

Constraints

  • 1 <= rows, columns <= 46
  • rows * columns <= 46

Examples

Input: rows = 2, columns = 3
Output: [[0,1,1],[5,3,2]]

The first 6 Fibonacci numbers are 0, 1, 1, 2, 3, 5. They fill the top row left to right, go down the right column, then fill the bottom row right to left.

Input: rows = 1, columns = 1
Output: [[0]]

A single cell holds F(0) = 0.

Input: rows = 3, columns = 4
Output: [[0,1,1,2],[34,55,89,3],[21,13,8,5]]

The outer ring takes F(0) through F(9) (0 to 34) and ends on the left cell of the middle row. The two inner cells of the middle row then take 55 and 89, moving right.