Find a Submatrix Matching a Pattern

Reported by candidates from Boston Consulting Group's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Boston Consulting Group OA. Under 2s to a working solution.
Founder's read

Boston Consulting Group reportedly served this one in July 2026, and it looks scarier than it is. Strip the letters and digits away and it's a brute-force window scan with a bijection check inside each window. You slide a pattern-sized square over the board, and for every position you verify literals and build a letter-to-digit map. That's matrix scanning plus hash tables. The stated complexity budget tells you the intended answer is plain nested loops, no clever tricks. If you blank on the mapping logic mid-assessment, StealthCoder runs invisibly on your desktop as a safety net and hands you the structure in real time.

The problem

You are given a rectangular integer matrix board and a square pattern represented by the string array pattern. Each entry of board is a digit from 0 through 9. Each character of pattern is either a decimal digit or a lowercase English letter.
A submatrix matches pattern when it has the same dimensions and follows all of these rules:
If pattern[r][c] is a digit, then the corresponding submatrix entry must equal that digit.
Every occurrence of the same letter must correspond to the same digit.
Any two distinct letters must correspond to different digits.
Return an integer array [row, column] containing the zero-based coordinates of the upper-left corner of a matching submatrix. If several submatrices match, return the one with the smallest row index, breaking a remaining tie by the smallest column index. If no submatrix matches, return [-1, -1].
Expected complexity
A solution with time complexity no worse than O(board.length * board[0].length * pattern.length^2) is sufficient.

Function
findMatchingSubmatrix(board: int[][], pattern: String[]) → int[]

Examples
Example 1
board = [[1,2,1],[2,1,2]]
pattern = ["ab","ba"]
return = [0,0]
The submatrix starting at [0, 0] maps a to 1 and b to 2. The submatrix starting at [0, 1] also matches with the two mappings reversed, so the smaller column index determines the result.
Example 2
board = [[0,1,2,3],[4,5,6,7],[8,9,5,1]]
pattern = ["a6","9a"]
return = [1,1]
At [1, 1], both occurrences of a correspond to 5, while the literal characters 6 and 9 match their board entries. Every earlier candidate fails a literal-digit check.
Example 3
board = [[1,1],[1,1]]
pattern = ["ab","ba"]
return = [-1,-1]
The only candidate would map both a and b to 1. Distinct letters must map to distinct digits, so there is no match.

Constraints
board is a non-empty rectangular matrix.
Every board[r][c] is an integer from 0 through 9.
pattern is non-empty, and every pattern[r] has length pattern.length.
Every character in pattern is a decimal digit or a lowercase English letter.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The reduction: for each top-left corner (r, c) where the window fits, walk all k*k cells. If the pattern cell is a digit, compare it directly to the board. If it's a letter, check a letter-to-digit map. If the letter is already mapped, the digit must agree. If it's new, the digit must not already be claimed by a different letter, so keep a reverse map digit-to-letter as well. Any failure breaks out early. Scan rows then columns in order and return the first hit, which handles the tie-break for free. The classic pitfall is forgetting the distinctness rule, which is exactly what Example 3 tests. Another is letting a letter map to a digit that a literal digit in the pattern already uses. Reset both maps per window. Total cost is O(m*n*k^2), which matches the budget. StealthCoder is your hedge if the two-way mapping slips your mind live.

StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.

If this hits your live OA

You can drill Find a Submatrix Matching a Pattern cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. If you're reading this with an OA window open, you're who this was built for.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Boston Consulting Group's OA.

Boston Consulting Group reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Find a Submatrix Matching a Pattern FAQ

What's the trick in the Boston Consulting Group submatrix pattern problem?+

Brute force every window, and inside each one keep two maps: letter to digit and digit to letter. The second map enforces that distinct letters get distinct digits. Return the first window that survives, scanning row-major.

How hard is this problem really?+

Easy-to-medium. There's no fancy algorithm, just careful bookkeeping. The complexity hint of O(m*n*k^2) confirms nested loops are expected. Most failures come from missing the distinct-letters rule, not from performance.

Do I need to handle literal digits in the pattern differently?+

Yes. A digit in the pattern must equal the board cell exactly. Compare directly and skip the maps for those cells. Decide whether a literal digit also blocks letters from mapping to it, and check the examples against your choice.

How do I handle the tie-break for multiple matches?+

Iterate rows from top to bottom and columns from left to right, and return immediately on the first valid window. That order gives the smallest row, then smallest column, so you need no extra comparison logic.

How do I prepare for this in 48 hours?+

Write the window scan with the two-map check from scratch twice. Test Example 3 where both letters collide on one digit. Practice early exit on mismatch and resetting maps per window. That covers nearly every bug this problem produces.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Boston Consulting Group.

OA at Boston Consulting Group?
Invisible during screen share
Get it