FastPrepCount Straight-Line Word Occurrences in a Grid
Problems/Count Straight-Line Word Occurrences in a Grid

Count Straight-Line Word Occurrences in a Grid

Visa logoVisa● MediumFULLTIMEOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: An August 7, 2024 Visa OA report gives the square-grid alphabet, all eight search directions, the rule that a palindrome counts twice, and a full sample. It gives only a lower limit for the grid and an interface that reads text input. We added upper limits and the function name countGridWordOccurrences, without changing the counting task.

Given a square grid of characters and a nonempty word, count every straight-line occurrence of the word.

An occurrence may run horizontally, vertically, or diagonally in either direction, for a total of eight directions. Matches may overlap, and characters are compared exactly, including case.

Opposite directions are counted separately. Therefore, every palindromic occurrence is counted twice. For a one-character palindrome, count each matching cell twice.

Function

countGridWordOccurrences(grid: String[], word: String) → int

Examples

Example 1

grid = ["ctt","cat","cct"]word = "cat"return = 4
grid
0120ctt1cat2cct

The word appears once across the middle row, once vertically, and twice diagonally, for 4 occurrences.

Example 2

grid = ["ABA","BAB","ABA"]word = "ABA"return = 8
grid
0120ABA1BAB2ABA

Two rows and two columns spell the palindrome. Each of those four geometric lines is counted in both directions, for 8 occurrences.

Example 3

grid = ["A0","aA"]word = "A"return = 4
grid
010A01aA

Two cells contain uppercase A; each one-character palindromic occurrence counts twice.

Constraints

  • 3 <= grid.length <= 200
  • Every row has exactly grid.length characters.
  • 1 <= word.length <= grid.length
  • The grid and word contain uppercase or lowercase English letters or decimal digits.
  • The answer fits in a 32-bit signed integer.

More Visa Online Assessment questions

  • Count Prime Values in SubtreesOA · Seen Nov 2026
  • Lock CodeOA · Seen Nov 2026
  • Minimum-Sum Tree Fill with Unit StepsOA · Seen Nov 2026
  • Longest Non-Decreasing Substring After Pair SwapsOA · Seen Oct 2026
  • Count Attack Vectors by Divisor RangeOA · Seen Oct 2026
  • Minimum Operations to Reduce a Transaction LedgerOA · Seen Oct 2026
  • Count Valid Space-Delimited WordsOA · Seen Oct 2026
  • Earliest Common Meeting StartOA · Seen Oct 2026
  • Maximum Index-Weighted Sum with Disjoint SwapsOA · Seen Oct 2026
  • Maximum Equalized Substrings After One Adjacent SwapOA · Seen Sep 2026

Similar questions at other companies

  • Repair One Movement InstructionOpenAI · OAOpenAI interview questions
  • Count Straight-Line Word Occurrences in a GridOptiver · OAOptiver interview questions
  • Flip Rows and Columns, Count ZerosNutanix · OANutanix interview questions
  • Hidden ArtifactsIMC Trading · OAIMC Trading interview questions
  • Simulate a Deterministic Finite AutomatonPoint72 · OAPoint72 interview questions
  • Collect Letters Along a Unique Grid PathApple · OAApple interview questions
  • Cyclic Fuel Command SimulationWaymo · ONSITE INTERVIEWWaymo interview questions
  • BitFont Part 2 - Render a WordStripe · ONSITE INTERVIEWStripe interview questions
  • Excel Training Data and Model Response AuditMicrosoft · PHONE SCREENMicrosoft interview questions
  • Alphabet Board PathZillow · ONSITE INTERVIEWZillow interview questions
All Visa interview questionsSee Visa hiring insights
Saved
class Solution {
    public int countGridWordOccurrences(String[] grid, String word) {
        // Write your solution here.
    }
}
Checking account…