Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Word Search in 2D Matrix

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Word Search in 2D Matrix. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

The Fanatics app represents a compact merchandising or personalization surface as a 2D grid of characters. Given a character grid and a target word, determine whether the word can be formed by moving between horizontally or vertically adjacent cells.

Each cell may be used at most once in a single word path. The word must use consecutive characters in order, and diagonal movement is not allowed.

Formal Specification

Implement word_exists(board, word).

  • board is a non-empty rectangular list of lists of single-character strings.
  • word is a string containing one or more characters.
  • Return True if word can be formed under the movement rules; otherwise return False.
  • Matching is case-sensitive.

Constraints

  • 1 <= rows, cols <= 6
  • 1 <= len(word) <= rows * cols
  • board is rectangular
  • Each board cell contains exactly one character
  • Matching is case-sensitive

Function Signature

def word_exists(board, word):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output