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
November 29, 20260
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
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- 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 ProblemsSample 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:
- Go right along row
top, thentop += 1. - Go down column
right, thenright -= 1. - If `t...