Skip to content
Search lessons, topics, tests…
Esc

    ↑ ↓ moveEnter openEsc close

    Coding problem

    Trace a word through a letter grid

    MediumAbout 25 minFull AccessLearn it: N-Queens: the famous one, briefly

    The task

    A word puzzle app checks a player's answer: the word must be traced through the letter grid, moving up, down, left or right from letter to letter.

    Complete WordInGrid(char[][] grid, string word). Return true when word can be traced through the grid by starting at any cell and moving up, down, left or right from each letter to the next, never using a cell twice. Matching is case-sensitive, and every character, '#' included, is ordinary data. The empty word is always found. All rows have the same length; the grid is never null.

    Signature

    public static bool WordInGrid(char[][] grid, string word)

    Examples

    • grid ["DOTN", "ESAE", "CORT"], "DOTNET" → true. Along the top row, then down the right column.
    • grid ["DOTN", "ESAE", "CORT"], "TNT" → false. It would need the same T twice.

    Limits: Grids of up to 6 × 6 cells and words of up to 12 characters. Grids are shown as rows of text.

    The tests check: words along a path and with turns, words that are not there, a cell used twice, the empty word, a word longer than the grid, letter case, '#' as data, and paths that need a cell a failed branch visited.

    Hints

      Your code

      C#Solution.csLoading the editor…

      Already included: using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Http; using System.Threading; using System.Threading.Tasks;

      public static class Solution
      {
          // True when word can be traced through neighbouring cells (up, down, left, right) without reusing a cell.
          public static bool WordInGrid(char[][] grid, string word)
          {
              throw new NotImplementedException();
          }
      }

      This problem comes with TechInterviewNotes Full Access

      Unlock all paid content on TechInterviewNotes.