PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates a candidate's ability to simulate array traversal and manage boundary conditions when filling a matrix in a specific geometric pattern. It tests spatial reasoning, index tracking, and careful loop control, skills commonly assessed in coding interviews focused on array and matrix manipulation. The task requires practical implementation rather than abstract algorithmic theory.

  • medium
  • Pinduoduo
  • Coding & Algorithms
  • Software Engineer

Generate a Matrix in Spiral Order

Company: Pinduoduo

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

# Generate a Matrix in Spiral Order Given two positive integers `m` (the number of rows) and `n` (the number of columns), generate an `m x n` matrix filled with the integers from `1` to `m * n` placed in **spiral order**. The spiral starts at the top-left corner of the matrix holding the value `1`, then moves **right** along the top row, **down** the right column, **left** along the bottom row, and **up** the left column, continuing to spiral inward — each value one greater than the previous — until every cell has been filled. Return the completed matrix as a 2D array. ## Example 1 ``` Input: m = 3, n = 3 Output: [[1, 2, 3], [8, 9, 4], [7, 6, 5]] ``` ## Example 2 ``` Input: m = 3, n = 4 Output: [[ 1, 2, 3, 4], [10, 11, 12, 5], [ 9, 8, 7, 6]] ``` ## Example 3 ``` Input: m = 1, n = 4 Output: [[1, 2, 3, 4]] ``` ## Constraints - `1 <= m, n <= 100` - The matrix is filled with exactly the integers `1, 2, ..., m * n`, each appearing once. - The traversal order is: right across the top, down the right side, left across the bottom, up the left side, repeating while spiraling inward. - Return the matrix as a list of `m` rows, each a list of `n` integers.

Quick Answer: This question evaluates a candidate's ability to simulate array traversal and manage boundary conditions when filling a matrix in a specific geometric pattern. It tests spatial reasoning, index tracking, and careful loop control, skills commonly assessed in coding interviews focused on array and matrix manipulation. The task requires practical implementation rather than abstract algorithmic theory.

Given two positive integers `m` (the number of rows) and `n` (the number of columns), generate an `m x n` matrix filled with the integers from `1` to `m * n` placed in **spiral order**. The spiral starts at the top-left corner holding the value `1`, then moves **right** along the top row, **down** the right column, **left** along the bottom row, and **up** the left column, continuing to spiral inward — each value one greater than the previous — until every cell has been filled. Return the completed matrix as a 2D array (a list of `m` rows, each a list of `n` integers). ### Example 1 ``` Input: m = 3, n = 3 Output: [[1, 2, 3], [8, 9, 4], [7, 6, 5]] ``` ### Example 2 ``` Input: m = 3, n = 4 Output: [[1, 2, 3, 4], [10, 11, 12, 5], [9, 8, 7, 6]] ``` ### Example 3 ``` Input: m = 1, n = 4 Output: [[1, 2, 3, 4]] ```

Constraints

  • 1 <= m, n <= 100
  • The matrix is filled with exactly the integers 1, 2, ..., m * n, each appearing once.
  • Traversal order: right across the top, down the right side, left across the bottom, up the left side, repeating while spiraling inward.
  • Return the matrix as a list of m rows, each a list of n integers.

Examples

Input: (3, 3)

Expected Output: [[1, 2, 3], [8, 9, 4], [7, 6, 5]]

Explanation: Standard square spiral: 1-3 across the top, 4-5 down the right, 6-7 across the bottom, 8 up the left, 9 in the center.

Input: (3, 4)

Expected Output: [[1, 2, 3, 4], [10, 11, 12, 5], [9, 8, 7, 6]]

Explanation: Rectangular (more columns than rows). After the outer ring, the inner 1x2 strip is filled 11, 12.

Input: (1, 4)

Expected Output: [[1, 2, 3, 4]]

Explanation: Single row — only the top-row pass runs; the guards prevent the bottom/left passes from re-filling cells.

Input: (4, 1)

Expected Output: [[1], [2], [3], [4]]

Explanation: Single column — top-row fills [0][0], then the right-column pass fills the rest going down.

Input: (1, 1)

Expected Output: [[1]]

Explanation: Minimum matrix — a single cell gets value 1.

Input: (2, 2)

Expected Output: [[1, 2], [4, 3]]

Explanation: Smallest full ring exercising all four directions: right (1,2), down (3), left (4).

Hints

  1. Maintain four boundaries — top, bottom, left, right — and shrink them inward after completing each edge of the current layer.
  2. Fill one direction at a time in the loop: top row left→right, right column top→bottom, bottom row right→left, left column bottom→top.
  3. After walking the top row, increment top; after the right column, decrement right; and so on. Re-check the guards (`if top <= bottom`, `if left <= right`) before the bottom row and left column to avoid double-filling on thin (single-row or single-column) matrices.
  4. The outer loop condition `top <= bottom and left <= right` stops once every cell is filled.
Last updated: Jul 1, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Find Shortest Square-Sum Path - Pinduoduo (hard)
  • Compute User Login Streaks - Pinduoduo (easy)
  • Determine Straight Flush - Pinduoduo (easy)
  • Design a Recency-Eviction Cache - Pinduoduo (medium)
  • Find next greater element for subset - Pinduoduo (easy)