Count Row and Column Pairs That Read Identically in a Grid
Last updated: November 29, 2026
Quick Overview
Given an n x n grid of integers, count the pairs (r, c) such that row r and column c contain the same values in the same order.
0
0
0 solved
Given an n x n grid of integers, count the pairs (r, c) such that row r and column c contain the same values in the same order.
A Google phone screen warm-up about hashing sequences. The interviewer wants you to move from the O(n^3) compare-everything approach to hashing rows as tuples and looking up columns, and to discuss hash collisions if you use a rolling hash instead.
What the Interviewer Expects
- States the O(n^3) brute force
- Hashes each row as a tuple and counts duplicates
- Looks up each column tuple in the row counter
- Counts duplicate rows with multiplicity
- States O(n^2) time and 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
- Rows are very long; use a rolling hash and discuss collisions.
- The grid is m x n with m != n; when can a row equal a column?
- Return the pairs themselves, not just the count.
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:
grid, n x n ints. - Output: the number of
(r, c)withgrid[r] == [grid[i][c] for i in range(n)]. - Duplicate rows or columns each count separately....
High-Level Design
Build a counter from each row converted to a tuple, since lists are not hashable. Then transpose with zip(*grid), which yields each column as a tuple, and add the counter value for each column. If r...