Detect anagram in matrix

Last updated: October 16, 2025

Quick Overview

Given a 2D matrix of characters and a string, write a function to detect all occurrences of the string as an anagram within the matrix. An anagram can be formed by rearranging the characters in the string, and the function should return the starting coordinates of each occurrence in the matrix. The search should consider all possible horizontal and vertical placements of the anagram.

Scale AI
Coding & Algorithms
Software Engineer
Scale AI
October 16, 2025
Software Engineer
Onsite
Coding & Algorithms
Hard

185

2

4,984 solved


Given a 2D matrix of characters and a string, write a function to detect all occurrences of the string as an anagram within the matrix. An anagram can be formed by rearranging the characters in the string, and the function should return the starting coordinates of each occurrence in the matrix. The search should consider all possible horizontal and vertical placements of the anagram.

This coding problem is frequently asked during Onsite at Scale AI. The interviewer is testing your ability to translate a problem into clean, working code while discussing time and space complexity. Scale AI 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
Common algorithm patterns (sliding window, two pointers, BFS/DFS)
Sorting and searching
Graph algorithms and traversal
Edge cases and input validation
Tree structures and recursion
Hash maps and frequency 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 input doesn't fit in memory?
  • What happens if the input contains duplicates?
  • Can you solve this iteratively instead of recursively (or vice versa)?
  • 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 anagrams in a 2D matrix of characters, we can leverage the concept of frequency counting. An anagram consists of the same characters in different orders, so the frequency of each character i...

Approach
  1. Frequency Count: Start by creating a frequency count of the characters in the target string. For example, for the string 'abc', the frequency map would be { 'a': 1, 'b': 1, 'c': 1 }.

  2. **Ho...


Submit Your Answer
Markdown supported

Related Questions