Count Matching Pairs of Bin Rows and Bin Columns

Last updated: November 29, 2026

Quick Overview

A warehouse shelf grid stores item counts in an n x n matrix. Count the pairs (r, c) such that row r and column c contain exactly the same sequence of counts, which flags rack layouts that mirror each other.

Amazon
Coding & Algorithms
Software Engineer
Amazon
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


A warehouse shelf grid stores item counts in an n x n matrix. Count the pairs (r, c) such that row r and column c contain exactly the same sequence of counts, which flags rack layouts that mirror each other.

An Amazon technical screen question framed around inventory audits of square shelf grids. The interviewer checks whether you hash entire rows as tuples so that each column lookup is O(n) instead of comparing every row with every column element by element.

What the Interviewer Expects
  • Note the brute force O(n^3) comparison
  • Hash each row as a tuple and count occurrences
  • Build each column tuple and look it up
  • Handle repeated identical rows by counting, not storing booleans
  • State O(n^2) time
Key Topics to Cover
Hash Map
Matrices
Tuple Hashing
Counting
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
  • What if the grid is m x n with m != n?
  • How would you do this if the grid is streamed row by row and too big for memory?
  • Could rolling hashes reduce memory at the cost of collision risk?
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: square matrix grid of integers, n up to 200. Output: number of (row index, column index) pairs where the row read left to right equals the column read top to bottom....

High-Level Design

Count every row as a tuple in a Counter. Then for each column, build the tuple of grid[r][c] for all r and add the count of matching rows. Duplicate rows contribute once per copy, which is what pa...


Submit Your Answer
Markdown supported