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.

Google
Coding & Algorithms
Frontend Engineer
Google
November 29, 2026
Frontend Engineer
Phone Screen
Coding & Algorithms
Easy

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
matrix traversal
simulation
boundary handling
arrays
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
  • 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 Problems
Sample 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...


Submit Your Answer
Markdown supported