Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Trie Word Search on Grid

Hard
CodingRecursionTreesGraphsAsked 2 times

Problem

Meta wants to scan a character grid for valid keywords that may appear in surfaces like Messenger game boards. Given a 2D board of lowercase letters and a list of lowercase words, return all words that can be formed by traversing adjacent cells.

A word can be constructed from letters of sequentially adjacent cells, where adjacency is horizontal or vertical. The same cell may not be used more than once in a single word.

Formal Specification

Implement a function that takes:

  • board: a list of m rows, each row a list of n lowercase characters
  • words: a list of distinct lowercase strings

Return a list of all words from words that appear in the board. The output may be in any order.

Constraints

  • 1 <= board.length <= 12
  • 1 <= board[0].length <= 12
  • 1 <= words.length <= 3 * 10^4
  • 1 <= words[i].length <= 10
  • board[i][j] consists of lowercase English letters
  • words[i] consists of lowercase English letters
  • All words in words are unique

Function Signature

def find_words(board, words):

You are practicing as a guest. Sign up free to run your code against the sample data. Your draft stays right here.

Sign up freeI have an account
def solve(rows):
    counts = {}
    for row in rows:
        ...
    return result
Sign up to unlock solutions
Snap QA Engineer Interview QuestionsMeta QA Engineer Interview QuestionsSnap Interview Questions
Next questions
An applied AIAnagram Search in 2D GridHardSnapImplement Word Search in a GridMediumHeyGenWord Search Grid SolutionHard
Python 3.10