Read a Photo Tile Grid in Clockwise Spiral Order

Last updated: November 29, 2026

Quick Overview

An Instagram collage is stored as an m x n grid of tile IDs. Return the tile IDs in clockwise spiral order starting from the top-left corner.

Meta
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Medium

0

0

0 solved


An Instagram collage is stored as an m x n grid of tile IDs. Return the tile IDs in clockwise spiral order starting from the top-left corner.

A simulation question common in Meta phone screens. The interviewer cares about clean boundary management and the non-square cases, where a single leftover row or column is easy to emit twice.

What the Interviewer Expects
  • Maintain top, bottom, left and right boundaries
  • Traverse one side at a time and shrink the boundary
  • Guard the bottom row and left column passes for thin grids
  • Handle empty input
  • State O(mn) time and O(1) extra space
Key Topics to Cover
Matrix traversal
Simulation
Boundary handling
Off-by-one errors
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Generate an n x n grid filled 1..n^2 in spiral order.
  • Return the spiral counterclockwise from a given starting corner.
  • Walk the spiral outward from a center cell.
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: list of m rows of n values each (m, n >= 0). Output: list of all m * n values in clockwise spiral order. Example: [[1,2,3],[4,5,6],[7,8,9]] -> [1,2,3,6,9,8,7,4,5]....

High-Level Design

Keep four boundaries: top, bottom, left, right. While top <= bottom and left <= right:

  1. Go right along row top, then top += 1.
  2. Go down column right, then right -= 1.
  3. If `t...

Submit Your Answer
Markdown supported