Detect permutation in matrix

Last updated: August 30, 2025

Quick Overview

Given a 2D matrix and a target permutation, determine if any row or column in the matrix contains a permutation of the target. The function should return true if such a permutation exists, and false otherwise. The input will consist of the matrix and the target permutation as an array of integers.

Grafana Labs
Coding & Algorithms
Software Engineer
Grafana Labs
August 30, 2025
Software Engineer
Take-home Project
Coding & Algorithms
Medium

7

11

4,821 solved


Given a 2D matrix and a target permutation, determine if any row or column in the matrix contains a permutation of the target. The function should return true if such a permutation exists, and false otherwise. The input will consist of the matrix and the target permutation as an array of integers.

This coding problem is frequently asked during Take-home Project at Grafana Labs. The interviewer is testing your ability to translate a problem into clean, working code while discussing time and space complexity. Grafana Labs expects candidates to write production-quality code, not just solve the puzzle.

What the Interviewer Expects
  • Recognize the underlying problem pattern (sliding window, two pointers, BFS/DFS, etc.)
  • Discuss multiple approaches and trade-offs before coding
  • Implement an optimal solution with clean, production-quality code
  • Handle all edge cases including boundary conditions and invalid input
  • Optimize both time and space complexity with clear justification
  • Test your solution systematically with well-chosen examples
Key Topics to Cover
Tree structures and recursion
Edge cases and input validation
Graph algorithms and traversal
Hash maps and frequency counting
Binary search and divide and conquer
Dynamic programming and memoization
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
  • Can you solve this iteratively instead of recursively (or vice versa)?
  • How would you modify your solution to handle streaming input?
  • What if the input doesn't fit in memory?
  • How would your solution change if the input was sorted?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Problem Analysis

To detect if any row or column in the matrix contains a permutation of the target, we can utilize the frequency counting method. The underlying pattern here is similar to using a hash map to count occ...

Approach
  1. Frequency Count of Target: Create a frequency count of the elements in the target permutation using a hash map (or dictionary). For example, for the target [1, 2, 2], the frequency map would be...

Submit Your Answer
Markdown supported

Related Questions