Reported September 2026
Optiverstring

Count Straight-Line Word Occurrences in a Grid

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

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

Optiver reportedly served this one in September 2026, and the one-character rule in the statement is the detail that sinks people. You count occurrences of a word in a grid, but only in straight rows and columns, in all four directions. It looks like a search problem, and the hinted tag says BFS, but there's no path to explore. It's plain scanning with careful counting. If you blank on the palindrome and single-letter edge cases during the live OA, StealthCoder runs invisibly as a safety net. Here's the actual shape of the problem.

The problem

Given a rectangular grid of characters and a nonempty word, count every occurrence that lies entirely in one row or one column.
Search left to right, right to left, top to bottom, and bottom to top. Matches may overlap and characters are compared exactly. When the word has length one, count each matching cell once rather than once per direction.

Function
countStraightWords(grid: String[], word: String) → int

Examples
Example 1
grid = ["ABCA","BCAB","CABC"]
word = "ABC"
return = 4
There are two left-to-right row matches and two top-to-bottom column matches.
Example 2
grid = ["AAAA"]
word = "AAA"
return = 4
Two overlapping matches appear in each horizontal direction.
Example 3
grid = ["AX","YA"]
word = "A"
return = 2
Each matching cell is counted once for a one-character word.

Constraints
1 <= grid.length, grid[i].length <= 500.
All rows have equal length.
1 <= word.length <= 500.
The grid and word contain visible ASCII characters.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Skip BFS. Nothing branches, so there's no graph to traverse. Treat each row and each column as a string. For every one of them, count overlapping matches of the word going forward. Then reverse the word and count again, which covers right to left and bottom to top. Check Example 2: AAAA with AAA gives 2 forward and 2 backward, so 4. The pitfall is double counting. If the word is a palindrome, the forward and reverse scans find the same positions, but the statement still counts each direction, as Example 2 shows. The special case is length one, where you count matching cells once. With a 500 by 500 grid and a 500-length word, naive comparison costs about 500*500*500*4 character checks, which is fine. Use KMP only if you want to be safe. Slide the start index by one so overlaps count. If you freeze under the timer, StealthCoder is the hedge that hands you the loop structure live.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill Count Straight-Line Word Occurrences in a Grid 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. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Optiver's OA.

Optiver reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Count Straight-Line Word Occurrences in a Grid FAQ

Is this really a BFS problem?+

No. The BFS hint is misleading. Matches lie in a single row or column, so there's nothing to expand or explore. It's a nested loop scan over strings. Reaching for BFS or DFS here wastes time and invites bugs.

What's the trick to this Optiver question?+

Handle the four directions by scanning each line forward, then scanning it against the reversed word. Count overlapping matches by moving the start index one step at a time. Then special-case word length one so each cell counts once.

How do I handle the single-character word?+

Return the number of cells equal to that character, once each. Without the special case, the forward and reverse scans in rows and columns count each cell four times. Example 3 shows the expected answer of 2.

Will brute force pass the constraints?+

Usually yes. The grid is at most 500 by 500 and the word at most 500, so naive comparison across rows and columns in both directions stays manageable. If you want extra safety, use KMP or a built-in substring search that finds overlaps.

How do I prepare for this in 48 hours?+

Write the solution once from scratch. Test all three examples, plus a palindrome word, a word longer than the grid dimension, and a 1x1 grid. Practice extracting columns into strings. Those edge cases are where points disappear.

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

OA at Optiver?
Invisible during screen share
Get it