Traverse Interior Matrix Cells in Row-Major Order
Updated
Problem statement
You are given a non-empty rectangular integer matrix matrix. Traverse its interior cells in row-major order and return their values.
A cell is on the boundary when it belongs to the first row, last row, first column, or last column. Interior cells belong to none of those boundary lines.
Visit interior rows from top to bottom and, within each row, visit interior columns from left to right. If the matrix has fewer than three rows or fewer than three columns, return an empty array.
Function
traverseInteriorCells(matrix: int[][]) → int[]Examples
Example 1
matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]return = [6,7,10,11]The interior cells are (1,1), (1,2), (2,1), and (2,2). Row-major traversal returns their values in that order.
Example 2
matrix = [[1,2,3],[4,5,6],[7,8,9]]return = [5]A three-by-three matrix has exactly one interior cell, the center.
Example 3
matrix = [[1,2,3,4],[5,6,7,8]]return = []With only two rows, every cell lies on the first or last row, so there are no interior cells.
Constraints
1 <= matrix.length, matrix[0].length <= 1000.matrix.length * matrix[0].length <= 10^6.- Every row has the same length.
-2^31 <= matrix[row][col] <= 2^31 - 1.
More Citadel Onsite Interview questions
- Maximize Workday EarningsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Minimum Changes for Palindromic BlocksOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Removal PoolONSITE INTERVIEW · Seen Sep 2026
- Centered K-by-K Submatrix SumsONSITE INTERVIEW · Seen Aug 2026
- Search Suggestions with a Prefix TreeONSITE INTERVIEW · Seen Aug 2026
- Limit Order Book Matching EnginePHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2026
- Fixed-Capacity Circular BufferONSITE INTERVIEW · Seen Jun 2026
- Stock Trading with a Fee and a CooldownONSITE INTERVIEW · Seen May 2026
- Time-Based Key-Value StorePHONE SCREEN · ONSITE INTERVIEW · Seen Feb 2026
- 2048 Simulation and Board CodecONSITE INTERVIEW · Seen Jan 2026
Similar questions at other companies
- Detect a Winner on a 3x3 Tic-Tac-Toe BoardHebbia · ONSITE INTERVIEW
- Spiral MatrixAthenahealth · ONSITE INTERVIEW
- Spiral MatrixNike, Inc. · ONSITE INTERVIEWNike, Inc. interview questions
- Incremental Hard Attention with a KV CacheOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Stochastic Gradient Descent with MomentumMeta · ONSITE INTERVIEWMeta interview questions
- Cache Shared Image Pipeline PrefixesAnthropic · ONSITE INTERVIEWAnthropic interview questions
- Minesweeper Board UpdateUber · ONSITE INTERVIEWUber interview questions
- Cats, Rabbits, and Snails Board GameAsana · ONSITE INTERVIEW
- Transformer Attention and SwiGLU Forward PassGoogle · ONSITE INTERVIEWGoogle interview questions
- Diagonal Matrix IteratorMicrosoft · ONSITE INTERVIEWMicrosoft interview questions