Detect permutation in matrix
Last updated: November 8, 2025
Quick Overview
Given a matrix of integers, determine if any row or column contains a permutation of a given target array. The input consists of the matrix and the target array, and the output should be a boolean indicating whether such a permutation exists in the matrix.
Shopify
November 8, 20252
12
1,679 solved
Given a matrix of integers, determine if any row or column contains a permutation of a given target array. The input consists of the matrix and the target array, and the output should be a boolean indicating whether such a permutation exists in the matrix.
This coding problem is frequently asked during Onsite at Shopify. The interviewer is testing your ability to translate a problem into clean, working code while discussing time and space complexity. Shopify expects candidates to write production-quality code, not just solve the puzzle.
What the Interviewer Expects
- Quickly identify the optimal approach and its theoretical basis
- Handle complex algorithm design with multiple interacting components
- Write concise, elegant code under time pressure
- Prove correctness of your approach and discuss alternative solutions
- Optimize beyond the obvious: discuss constant factor improvements
- Address follow-up variations and explain how the solution generalizes
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
- Can you solve this in a single pass?
- Can you optimize the space complexity of your solution?
- What if the input doesn't fit in memory?
- How would you test this solution thoroughly?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Problem Analysis
To determine if any row or column in the matrix contains a permutation of the target array, we can leverage a hashmap (or dictionary in Python) to count the occurrences of each integer in the targ...
Approach
- Count Frequencies of Target Array: Create a frequency dictionary for the integers in the target array.
- Example: for target = [1, 2, 2], freq = {1: 1, 2: 2}.
- Check Rows: For each row...