Read a Photo Tile's Pixels in Clockwise Spiral Order
Last updated: November 29, 2026
Quick Overview
Given an m by n matrix of pixel values, return all values in clockwise spiral order starting from the top-left corner, as a progressive image loader might fetch them from the outside in.
0
0
0 solved
Given an m by n matrix of pixel values, return all values in clockwise spiral order starting from the top-left corner, as a progressive image loader might fetch them from the outside in.
A Google screen classic dressed as a Google Photos progressive loader. The interviewer is checking boundary bookkeeping: shrinking four walls without double counting the middle row or column of non-square tiles.
What the Interviewer Expects
- Use four shrinking boundaries: top, bottom, left, right
- Guard the bottom row and left column passes for non-square matrices
- Produce exactly m * n values with no duplicates
- Handle an empty matrix
- Explain O(mn) time
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
- How would you write the matrix in spiral order instead of reading it?
- Produce counter-clockwise order starting from the center.
- How would you stream this for a matrix too large to hold in memory?
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: a list of m rows, each of length n (possibly zero rows). Output a flat list of m * n values visiting the outer ring clockwise from top-left, then the next ring, and so on....
High-Level Design
Maintain top, bottom, left, right. Each loop iteration walks the top row left to right and increments top, walks the right column top to bottom and decrements right, then, only if `top <= bottom...